简体   繁体   English

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

[英]Using PHP to add multiple rows to MySQL with form

I have been searching the web for days and I just can't figure this out. 我已经在网上搜索了好几天,但我无法弄清楚。

I'm trying to insert form data into multiple rows in my MySQL database, but it doesn't work as I want it to. 我正在尝试将表格数据插入MySQL数据库中的多行中,但是它并不能按我的意愿工作。 Is there anyone that can help me? 有没有人可以帮助我?

What I wanna do is to send a form to two separate tables. 我想做的是将一个表单发送到两个单独的表。 I want some info to go to a table called Dishes and other information into a table called Ingredients. 我希望一些信息转到名为Dishes的表中,而其他信息则转到名为Ingredients的表中。 The content that's being sent to the Dishes table is working as it should, it's the Ingredients table that doesn't take in the array of ingredients. 发送到Dishes表的内容可以正常工作,它是Ingredients表,不包含配料数组。 I want the ingredients on multiple rows, depending on how many you enter. 我希望将成分放在多行中,具体取决于您输入的数量。

As of now the Ingredient table is creating multiple rows in the table for every entry, but it doesn't input any data into the rows... 到目前为止,成分表正在为每个条目在表中创建多个行,但是它没有在行中输入任何数据...

This is my HTML: 这是我的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>

This is my PHP: 这是我的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);
?>

Would greatly appreciate any help with this! 将不胜感激与此有关的任何帮助!

You are missing $conn when you are setting the value. 设置值时缺少$conn

mysqli_real_escape_string (); require two parameter. 需要两个参数。 one is connection 一是连接

First one $connection and second is string which you want to escape. 第一个是$connection ,第二个是您要转义的string

Thats why you are not able to insert any thing. 这就是为什么您不能插入任何东西的原因。 Even though your query is fine. 即使您的查询很好。

Try this code when you are setting the value. 设置值时,请尝试使用此代码。

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

?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM