2012-01-13 106 views
0

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

<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 

我只是无法弄清楚这一点。我需要帮助。如果不是存在的表单条目的数量,我怀疑我必须有一种方法来告诉我实际发送了多少条记录。

回答

2

您需要测试数组中的值是否在查询之前被填入。此外,您必须对逃逸SQL注入的所有插入值与mysql_real_escape_string()

$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()); 
    } 
} 
+0

这应该工作,但它不会插入除食谱名称之外的任何东西。我尝试了一个var_dump来查看配方步骤是什么,并且显示为0.表单仅发送步骤和配料的最后一个条目。我不知道如何克服这一点。 – attentionjay 2012-01-13 05:14:38

+0

我通过重新载入所有内容,然后重新尝试了一遍,现在似乎可以正常工作。当我第一次尝试时,我不知道问题出在哪里。真心谢谢你帮助我解决这个问题。 – attentionjay 2012-01-13 18:03:50

+0

@ user1137742很高兴认识它 - 很乐意帮助 – 2012-01-13 18:53:33

0

事实是,即使用户没有在信息填写表格的食谱[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()); 
    } 
} 

注意,这是最低的验证,你应该更彻底地检查一切。

+0

我试过了,它似乎工作。我仍在测试,但目前看起来不错。感谢您花时间帮助我。 – attentionjay 2012-01-13 18:04:26

相关问题