gpt4 book ai didi

php - 将多个复选框值添加到数据库

转载 作者:行者123 更新时间:2023-11-30 23:00:18 25 4
gpt4 key购买 nike

我正在尝试制作表格,让用户勾选他们吃过的食物的方框,并将碳水化合物值发送到另一个表格。表的内容在我用以下代码调用它的数据库中:

while($row = mysqli_fetch_array($q))
{

echo "<form action='add.php' method='post' id='add'><tr>";
echo "<td>".$row['carb_id']."</td>";
echo "<td>".$row['food_item']."</td>";
echo "<td>".$row['serving_size']."</td>";
echo "<td>".$row['carbs_per_serving']."</td>";
echo "<td><input type='checkbox' value='".$row['carbs_per_serving']."'
name='food[]'></td>";
echo "</tr></form>";

}
echo "<input type='submit' class='add'form='add'>";

添加到另一个表的 PHP 是:

<?php
/*blog.php
process a basic form saving data
*/

error_reporting ('E_all');

//Create a connection to the database
$link = mysqli_connect('localhost','root','','login')
or die('Error' . mysqli_error($link));

//If there is no connection trhows up an error message
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

$food = $_POST['food'];


//Insert values into the database
//mysqli_query($link,"INSERT INTO blog(post)
// VALUES ('".$post."')"
// );

//echos out the information put in apart from the password
mysqli_query($link,"INSERT INTO carbsummary(cpp)
VALUES ('".$food."')"
);
echo $food;
?>

我希望我已经在这里正确地解释了自己。简而言之,我希望让用户选中复选框,然后将食物的碳水化合物值提交到下一个表格。

我不太擅长编码,因为我刚开始不久,但我似乎无法在任何地方找到解决方案。

最佳答案

首先:

当您创建表单时,您的循环将创建许多表单。所以你应该改变这段代码:

    while($row = mysqli_fetch_array($q)){
echo "<form action='add.php' method='post' id='add'><tr>";
echo "<td>".$row['carb_id']."</td>";
echo "<td>".$row['food_item']."</td>";
echo "<td>".$row['serving_size']."</td>";
echo "<td>".$row['carbs_per_serving']."</td>";
echo "<td><input type='checkbox' value='".$row['carbs_per_serving']."'
name='food[]'></td>";
echo "</tr></form>";
}
echo "<input type='submit' class='add'form='add'>";

有了这个:

    while($row = mysqli_fetch_array($q)){
echo "<form action='add.php' method='post' id='add'>";
echo "<tr><td>".$row['carb_id']."</td>";
echo "<td>".$row['food_item']."</td>";
echo "<td>".$row['serving_size']."</td>";
echo "<td>".$row['carbs_per_serving']."</td>";
echo "<td><input type='checkbox' value='".$row['carbs_per_serving']."'
name='food[]'></td>"; }
echo "<input type='submit' class='add'form='add'>";
echo "</tr></form>";

第二个:

$_POST['food'] 包含一个数组,因此它将返回一个数组,因此您应该调用该值的索引。

$food = $_POST['food'][0];

第三

如果你想同时发送很多插入你应该改变你的形式:

echo "<form action='add.php' method='post' id='add'>"; 
while($row = mysqli_fetch_array($q)){
echo "<tr><td>".$row['carb_id']."</td>";
echo "<td>".$row['food_item']."</td>";
echo "<td>".$row['serving_size']."</td>";
echo "<td>".$row['carbs_per_serving']."</td>";
echo "<td><input type='checkbox' value='".$row['carbs_per_serving']."'
name='food[]'></td>"; }
echo "</tr>";
}
echo "<input type='submit' class='add'form='add'></form>";

然后更改您的查询执行:

foreach($_POST["food"] AS $food){
mysqli_query($link,"INSERT INTO carbsummary (cpp) VALUES ('{$food}')";
echo $food;
}

关于php - 将多个复选框值添加到数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24374859/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com