1

Update

Thanks to Martin, I have been able to solve the issue. Changed the php code to reflect the solution.

The issue: Insert statements executed before entering data in the form and submitting it

Solution: use !empty() to only execute inserts after hitting "submit" button.


I have been asked to create a database for keeping track of roasting, packaging and ingredients used in the production of chocolate with data inserts through a web UI.

There are three types of ingredients used in the process of creating one batch. I want to add all three at once. There is only one table for all three.

My form looks like this with 20+ input fields wrapped in a table:

<form action="index.php" method="post">
<input type="text" id="bCode" name="bCode" value="1234" />
</form>

I have two SQL statements for packaging/products that worked fine until I added the multiple rows statement.

The error message is:

Error : (1048) Column 'batchCo' cannot be null

Edit: I also get undefined index notices for all variables, but I suppose that's because there's no data in the form yet.

I followed an example for this from a basic tutorial.

I'm sorry if this has been answered before. I may not have understood those answers, I'm a terrible programmer...

Edit: the entire php code

session_start();

// product vars
$bCode = $_POST["bCode"];
$proName = $_POST["proName"];
$proGrindDate = $_POST["proGrindDate"];
$proBeanBatch = $_POST["proBeanBatch"];
$proSugarBatch = $_POST["proSugarBatch"];
$proButterBatch = $_POST["proButterBatch"];
$proQTY = $_POST["proQTY"];  
$proLabel = true;
$proFinish = true;        

// ingredient vars
$beanBatch = $_POST["beanBatch"];
$beanWeight = $_POST["beanWeight"];
$beanRoastDate = $_POST["beanRoastDate"];
$beanWinnowYield = $_POST["beanWinnowYield"];
$beanWinnowDate = $_POST["beanWinnowDate"];
$beanCarryOver = $_POST["beanCarryOver"];
$sugarBatch = $_POST["sugarBatch"];
$sugarWeight = $_POST["sugarWeight"];
$butterBatch = $_POST["butterBatch"];
$butterWeight = $_POST["butterWeight"];

// package vars
$packBagDate = $_POST["packBagDate"];
$packMouldDate = $_POST["packMouldDate"];
$packWrapDate = $_POST["packWrapDate"];
$packType = $_POST["packType"];
$packQTY = $_POST["packQTY"];

$mandatory = ["bCode", "proName", "proGrindDate", "proQTY", "beanBatch", "beanWeight", "sugarBatch", "sugarWeight", "butterBatch", "butterWeight", "packType", "packQTY"];  

echo($bCode);

// connection vars
$servername = "...";
$username = "...";
$password = "...";
$database ="traceability";       

// Create connection
$conn = new mysqli($servername, $username, $password, $database);

if(!empty($_POST["submit"])){ 

// Insert into database - single rows
// table product
$queryProduct = "INSERT INTO product (batchCo, name, dateGrind, quantity, ingBeansBatch, ingSugarBatch, ingButterBatch, label, finished) VALUES(?,?,?,?,?,?,?,?,?)";
$stmtProduct = $conn->prepare($queryProduct);

//bind parameters for markers, where (s = string, i = integer, d = double,  b = blob)
$stmtProduct->bind_param('issdsssbb', $bCode, $proName, $proGrindDate, $proQTY, $proBeanBatch, $proSugarBatch, $proButterBatch, $proLabel, $proFinish);

if($stmtProduct->execute())
    {
        echo "Everything is working"; 
    }
else
    {
        die('Error : ('. $conn->errno .') '. $conn->error);
    }
$stmtProduct->close();

// table packaging
$queryPackaging = "INSERT INTO packaging (dateBag, dateMould, dateWrap, Qty, batchCo, type) VALUES(?,?,?,?,?,?)";
$stmtPackaging = $conn->prepare($queryPackaging);
$stmtPackaging->bind_param('sssiis', $packBagDate, $packMouldDate, $packWrapDate, $packQTY, $bCode, $packType);

if($stmtPackaging->execute())
    {
        echo "Everything is working"; 
    }
else
    {
        die('Error : ('. $conn->errno .') '. $conn->error);
    }

$stmtPackaging->close();

// Insert into database - multiple rows
// table ingredients

    if (!empty($bCode) && is_numeric($bCode)){
$insert = $conn->query("INSERT INTO ingredients (batchNo, type, weight, roastDate, winnowDate, winnowYield, winnowCarryOver, batchCo) VALUES
    ('$beanBatch', 'Beans', '$beanWeight', '$beanRoastDate', '$beanWinnowDate', '$beanWinnowYield', '$beanCarryOver', '$bCode'),
    ('$sugarBatch', 'Sugar', '$sugarWeight', NULL, NULL, NULL, NULL, '$bCode'),
    ('$butterBatch', 'Butter', '$butterWeight', NULL, NULL, NULL, NULL, '$bCode')");
}

if($insert){
    //return total inserted records using mysqli_affected_rows
    print 'Success! Total ' .$conn->affected_rows .' rows added.<br />'; 
}else{
    die('Error : ('. $conn->errno .') '. $conn->error);
}
}
4
  • It would seem the value is NULL: The error message is pretty clear and you try to print it out and you don't see it. Commented Apr 20, 2015 at 15:34
  • How did you give the bcode field a value? Commented Apr 20, 2015 at 15:35
  • @jeroen the question is why is it NULL at all, it didn't seem to be before I added the portion for multiple rows. Commented Apr 21, 2015 at 10:33
  • @fry3993 added it to the original post. Commented Apr 21, 2015 at 10:51

1 Answer 1

1

In your MySQL statement you need to encase the PHP variables in quote marks, to define them as input values for the SQL call.

see:

$insert = $conn->query("INSERT INTO ingredients (batchNo, type, weight, roastDate, winnowDate, winnowYield, winnowCarryOver, batchCo) VALUES
('$beanBatch', 'Beans', '$beanWeight', '$beanRoastDate', '$beanWinnowDate', '$beanWinnowYield', '$beanCarryOver', '$bCode'),
('$sugarBatch', 'Sugar', '$sugarWeight', NULL, NULL, NULL, NULL, '$bCode'),
('$butterBatch', 'Butter', '$butterWeight', NULL, NULL, NULL, NULL, '$bCode')");

This is a better formatting for the SQL query.

Where is this bCode field value? I can not see it in your code? But this $bCode variable needs to be given a value.

You can also wrap the whole SQL query in a check to ensure only initialised if there is a valid $bCode value:

updated:

if ( !empty($bCode) && is_numeric($bCode)){
//run insert SQL here.
}

UPDATE

The issue comes from the value of the input field named 'bCode'. At the top of your PHP page please enter:

print "<pre>";
print_r($_POST);
print "</pre>";

And see that the value for $_POST['bCode'] is valid value. Let me know what this value is.

Please also note that value="1234"/></td> should have space between the last quote and the slash so: value="1234" /></td> is correct.

I reaffirm you need to encase all your variables inside quotes as per the top part of my answer. You have yet to do that on your quoted code in your question.

Please give me an update on what error outputs you get or what has changed :)

Sign up to request clarification or add additional context in comments.

12 Comments

added the value to my initial post as well as more code. I tried to wrap the insert statement like this, but there's no change.
viewing your code - where is $bCode from? It is a post value but there is no input with the name bCode @zuhani
I added it to the bottom of the post. all input field look like that. I'll edit the post again to make it more visible.
@zuhani I have updated my anwser for you - please give me feedback on your code status etc :)
@zuhani is your BatchCo value is an autoincrement value? If it is then the code does not need to be given to the SQL statement at all but is auto-generated when the data is saved into the database row.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.