gpt4 book ai didi

php - 使用 PHP 通过表单向 MySQL 添加多行

转载 作者:行者123 更新时间:2023-11-29 23:34:11 26 4
gpt4 key购买 nike

我已经在网上搜索了好几天了,但我就是无法弄清楚这一点。

我正在尝试将表单数据插入到 MySQL 数据库中的多行中,但它无法按我想要的方式工作。有谁可以帮助我吗?

我想要做的是将表单发送到两个单独的表。我希望一些信息进入名为“菜肴”的表,其他信息进入名为“成分”的表。发送到“菜肴”表的内容正常工作,“配料”表不包含配料数组。我想要多行的成分,具体取决于您输入的数量。

到目前为止,成分表正在为每个条目在表中创建多行,但它不会在行中输入任何数据...

这是我的 HTML:

<form method="post" action="insertrecipe.php">
<table>
<tr>
<td class="name_of_dish_heading">Name of dish</td>
</tr>
<tr>
<td><input type="text" name="dish_name"></td>
</tr>
<tr>
<td class="table_text">Short description:</td>
</tr>
<tr>
<td><textarea name="dish_short_description" rows="3" cols="45"></textarea></td>
</tr>
</table>
<table>
<tr>
<td class="table_text">Ingredient:</td>
<td class="table_text">Amount:</td>
<td class="table_text">Type:</td>
</tr>
<tr>
<td><input type="text" name="ingredient[]"></td>
<td><input type="text" name="ingred_amount[]" size="5"></td>
<td>
<select name="ingred_amount_type[]">
<option name="Milliliter" value="Milliliter">Milliliter (ML)</option>
<option name="Centiliter" value="Centiliter">Centiliter (CL)</option>
<option name="Deciliter" value="Deciliter">Deciliter (DL)</option>
<option name="Liter" value="Liter">Liter (L)</option>
</select>
</td>
</tr>
<tr>
<td class="table_text">Ingredient:</td>
<td class="table_text">Amount:</td>
<td class="table_text">Type:</td>
</tr>
<tr>
<td><input type="text" name="ingredient[]"></td>
<td><input type="text" name="ingred_amount[]" size="5"></td>
<td>
<select name="ingred_amount_type[]">
<option name="Milliliter" value="Milliliter">Milliliter (ML)</option>
<option name="Centiliter" value="Centiliter">Centiliter (CL)</option>
<option name="Deciliter" value="Deciliter">Deciliter (DL)</option>
<option name="Liter" value="Liter">Liter (L)</option>
</select>
</td>
</tr>
</table>
<input type="submit" name="submit" value="Add recipe">
</form>

这是我的 PHP:

<?php
require_once('config.php');

// Connect to the database
$con = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

$dish_name = mysqli_real_escape_string($con, $_POST['dish_name']);
$dish_short_description = mysqli_real_escape_string($con, $_POST['dish_short_description']);

$sql1="INSERT INTO dishes (dish_name, dish_short_description)
VALUES ('$dish_name', '$dish_short_description')";

if (!mysqli_query($con,$sql1)) {
die('[Error: '.__LINE__.']: '.mysqli_error($con));
}
else {
echo "Added to the database.<br /><br />";
}

foreach($_POST['ingredient'] as $row=>$ingred) {
$ingredient = mysqli_real_escape_string($ingred);
$ingred_amount = mysqli_real_escape_string($_POST['ingred_amount'][$row]);
$ingred_amount_type = mysqli_real_escape_string($_POST['ingred_amount_type'][$row]);

$query = "INSERT INTO ingredients (ingredient, ingred_amount, ingred_amount_type) VALUES ('$ingredient', '$ingred_amount', '$ingred_amount_type')";

if (!mysqli_query($con,$query)) {
die('[Error: '.__LINE__.']: '.mysqli_error($con));
}
else {
echo "Added to the database.";
}
}

mysqli_close($con);
?>

非常感谢任何帮助!

最佳答案

设置值时缺少$conn

mysqli_real_escape_string (); 需要两个参数。一是连接

第一个是$connection,第二个是您想要转义的string

这就是为什么你无法插入任何东西。即使您的查询没问题。

在设置值时尝试此代码。

$ingredient = mysqli_real_escape_string($con,$ingred); //added $con in every line
$ingred_amount = mysqli_real_escape_string($con,$_POST['ingred_amount'][$row]);
$ingred_amount_type = mysqli_real_escape_string($con,$_POST['ingred_amount_type'][$row]);

关于php - 使用 PHP 通过表单向 MySQL 添加多行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26439648/

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