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);
}
}
NULL: The error message is pretty clear and you try to print it out and you don't see it.