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

标签 php mysql arrays

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

我正在尝试将表单数据插入到 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/

相关文章:

java - 双重增强 for 循环以在 boolean 值 [] [] 中计数?

c - 以下是什么意思?

php - cURL + Laravel Valet + dnsmasq 不工作

php - Windows 7 中 mysql php 连接问题

java - 如何将数组中的文件连接到新文件夹中?

mysql - 在 mac 上使用 rails3 安装 mysql2 gem 的问题

mysql - 相同的 FULLTEXT、REGEXP 和 LIKE 搜索返回不同的结果

php - 如何使用 img 标签设置背景图像

PHP 使用 preg_replace : "Delimiter must not be alphanumeric or backslash" error

php - 根据用户的国家显示 jquery 价目表