如何使用数组将表单数据输入到mysql数据库中


How to enter form data into a mysql database using an array?

我有一个表格,它提交了我正在为另一个项目练习的食谱的步骤和配料。我已经设置了表单以提交数组中的数据,但我无法获得正确的PHP代码来将数据插入数据库。我已将表单布局粘贴到此处。该表单作为另一个PHP页面的一部分出现,当用户输入要添加到数据库中的配方名称时,会调用该页面。如果我能想出如何将它们正确插入数据库,我希望在这个表格上有10个单独的步骤条目。

<form action="add_recipe2.php" method="post">
    <fieldset>
        <legend>Add a Recipe</legend>
        <table>
            <tr>
                <td>Recipe Name:</td>
                <td><input type="text" name="recipename" value="$recipename"></td>
           </tr>
            <tr>
                <td>Step:</td>
                <td><input type="text" name="recipe[0][step]" placeholder="1"></td>
                <td>Ingredients:</td>
                <td><input type="text" name="recipe[0][ingredients]" placeholder="Ingredients"></td>
            </tr>
            <tr>
                <td>Step:</td>
                <td><input type="text" name="recipe[1][step]" placeholder="2"></td>
                <td>Ingredients:</td>
                <td><input type="text" name="recipe[1][ingredients]" placeholder="Ingredients"></td>
            </tr>
            <tr>
                <td>Step:</td>
                <td><input type="text" name="recipe[2][step]" placeholder="3"></td>
                <td>Ingredients:</td>
                <td><input type="text" name="recipe[2][ingredients]" placeholder="Ingredients"></td>
            </tr>
        </table>
        <button type="submit">Add a Recipe</button>
        <button type="reset">Reset</button>
    </fieldset>
</form>

这是将数据输入数据库的PHP。问题是,当我只向数据库中添加两条记录时,最后一条记录仍然会插入,但它会插入一行空行。我需要一种方法来只添加从表单传递的数据,即使它只有一行。我对此研究了很长时间,这是我找到的答案之一。然而,当表单中没有更多数据时,它仍然不会停止插入数据库

$recipename = $_REQUEST["recipename"];
$conn = mysql_connect("localhost","user","password") or die(mysql_error());
mysql_select_db("test");
foreach($_POST['recipe'] as $recipe) { 
// Add to database
$sql1 = "INSERT INTO `recipes` (recipe, step, ingredients) VALUES   ('".$_POST['recipename']."', '".$recipe['step']."', '".$recipe['ingredients']."')";
mysql_query($sql1, $conn) or die(mysql_error());
 } //end foreach

我就是想不通。我需要帮助。我怀疑,如果不是存在的表格条目数量,我必须有一种方法来判断我实际发送了多少记录。

在查询之前,您需要测试数组组件中的值是否已填充。此外,您必须使用mysql_real_escape_string():转义针对SQL注入的所有插入值

$recipename = mysql_real_escape_string($_POST['recipename']);
foreach($_POST['recipe'] as $recipe) { 
  // Only insert if step is non-empty.
  if (!empty($recipe['step']) {
    // Add to database
    // Escape against SQL injection
    $recipe['step'] = mysql_real_escape_string($recipe['step'];
    $recipe['ingredients'] = mysql_real_escape_string($recipe['ingredients'];
    $sql1 = "INSERT INTO `recipes` (recipe, step, ingredients) VALUES   ('".$recipename."', '".$recipe['step']."', '".$recipe['ingredients']."')";
    mysql_query($sql1, $conn) or die(mysql_error());
  }
} 

事实是,即使用户没有在表单中填写配方[3]的信息,空值仍在提交中。

在插入数据库之前,您必须验证您的数据:

function isValidRecipe($recipe){
    // returns true if ingredients and step are not empty
    return !(empty($recipe['ingredients']) || empty($recipe['step']));
}
foreach($_POST['recipe'] as $recipe) {
if (isValidRecipe($recipe)){
    // Add to database
    $sql1 = "INSERT INTO `recipes` (recipe, step, ingredients) VALUES ('".$_POST['recipename']."', '".$recipe['step']."', '".$recipe['ingredients']."')";
        mysql_query($sql1, $conn) or die(mysql_error());
    }
}

请注意,这是最低限度的验证,您可能应该更彻底地检查所有内容。