简体   繁体   English

MySQLi-插入多行-空的POST数组

[英]MySQLi - Insert multiple rows - empty POST array

Update 更新

Thanks to Martin, I have been able to solve the issue. 多亏了马丁,我才得以解决问题。 Changed the php code to reflect the solution. 更改了php代码以反映解决方案。

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. 解决方案:使用!empty()仅在单击“提交”按钮后执行插入。


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. 我被要求创建一个数据库,以通过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: 我的表单看起来像这样,表格中包裹了20多个输入字段:

<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. 在添加多行语句之前,我有两个用于包装/产品的SQL语句运行良好。

The error message is: 错误消息是:

Error : (1048) Column 'batchCo' cannot be null 错误:(1048)列“ batchCo”不能为空

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 编辑:整个PHP代码

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);
}
}

In your MySQL statement you need to encase the PHP variables in quote marks, to define them as input values for the SQL call. 在MySQL语句中,您需要将PHP变量括在引号中,以将它们定义为SQL调用的输入值。

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. 这是SQL查询的更好格式。

Where is this bCode field value? bCode字段值在哪里? I can not see it in your code? 我在您的代码中看不到它? But this $bCode variable needs to be given a value. 但是需要为该$bCode变量赋值。

You can also wrap the whole SQL query in a check to ensure only initialised if there is a valid $bCode value: 您还可以将整个SQL查询包装在检查中,以确保仅在存在有效的$bCode值时进行初始化:

updated: 更新:

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

UPDATE UPDATE

The issue comes from the value of the input field named 'bCode'. 问题来自名为“ bCode”的输入字段的值。 At the top of your PHP page please enter: 在PHP页面的顶部,输入:

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

And see that the value for $_POST['bCode'] is valid value. 并且看到$_POST['bCode']的值是有效值。 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. 另请注意, value="1234"/></td>在最后一个引号和斜杠之间应有空格,因此: value="1234" /></td>是正确的。

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 :) 请给我更新您得到的错误输出或已更改的内容的信息:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM