使用PHP将多行添加到带有表单的MySQL中


Using PHP to add multiple rows to MySQL with form

我已经在网上搜索了好几天了,就是弄不明白。

我正试图将表单数据插入MySQL数据库的多行中,但它无法按我的意愿工作。有人能帮我吗?

我想做的是把一张表格送到两张不同的桌子上。我想把一些信息放到一张名为Dishes的桌子上,把其他信息放到一个名为Ingredients的桌子上。发送到Dishes表的内容正在正常工作,配料表没有包含一系列配料。我想要多行配料,这取决于你输入的数量。

到目前为止,Ingredient表正在为每个条目在表中创建多行,但它没有向这些行输入任何数据。。。

这是我的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]);
Because ingredient is array so first you need to count how many entry.

<?php
$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 />";
}
$no_of_entrys = count($_POST['ingredient']);
for ($i=0; $i < $no_of_entrys; $i++) { 
    $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);
?>