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

时间:2014-10-18 12:20:12

标签: php mysql arrays

我已经在网上搜索了好几天,我只是想不出来。

我正在尝试将表单数据插入MySQL数据库中的多行,但它并不像我想要的那样工作。有没有人可以帮助我?

我想做的是将表格发送到两个单独的表格。我想要一些信息去一个名为Dishes的表和其他信息到名为Ingredients的表中。发送到Dishes表的内容正在按原样运行,它是不包含成分数组的Ingredients表。我想要多行的成分,具体取决于你输入的数量。

截至目前,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);
?>

非常感谢任何帮助!

2 个答案:

答案 0 :(得分:1)

设置值时,您缺少$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]);

答案 1 :(得分:-1)

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);

?>