我有以下复选框项目,我想将它们插入数据库中。
<form action="test.php" method="post">
<input type="checkbox" name="check_list[]" value="rice:10">
<input type="checkbox" name="check_list[]" value="milk:8">
<input type="checkbox" name="check_list[]" value="orange:4">
<input type="submit" />
</form>
以下是php代码:
if(!empty($_POST['check_list'])) {
foreach($_POST['check_list'] as $check) {
$mealA = explode(":",$check);
$meal = $mealA[0];
$sql1 = "INSERT INTO Order (User, Meal, Total) VALUES ('$name' , '$meal', '$total')";
if (mysqli_query($mysqli,$sql1)) {
echo "You order has been recieved";
} else {
echo "Error: " . $sql1;
}
}
}
请注意,每个复选框的值表示项目名称:price。
问题是我收到错误"Error: " . $sql1;
答案 0 :(得分:2)
您的查询失败,因为您尚未为$total
设置值。因此,您必须从导致
Total
字段
$sql1 = "INSERT INTO Order (User, Meal) VALUES ('$name' , '$meal')";
为每个成功的新插入内容NULL
列提供Total
值
或强>
基于您提供的代码,我猜你想做类似的事情吗?
$mealA = explode(":",$check);
$meal = $mealA[0];
$total = $mealA[1]; // setting the value of $total otherwise query is going to fail
$sql1 = "INSERT INTO Order (User, Meal, Total) VALUES ('$name' , '$meal', '$total')";
答案 1 :(得分:0)
问题是插入声明:
INSERT INTO Order (User, Meal, Total) VALUES ('$name' , '$meal', '$total')
我将表格名称从Order
更改为Orders
,我删除了Total
,因为它是空的,并且没有存储值。
感谢您的建议。