我的表格提出了我为另一个项目所实践的对应措施的步骤和内容。 我已经建立了提交数据阵列的表格,但我无法正确将数据输入数据库。 我在此重复了形式。 表格是作为另一个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>
这是将数据输入数据库的购买力平价。 问题是,在我只给数据库增加两个记录时,最后的记录仍加进,但插入了一个空白线。 我只需要增加从表格中删除的数据,即使数据只是一线。 我进行了长时间的研究,这是我发现的答案之一。 但是,如果表格中的数据没有增加,它仍然不会停止输入数据库。
$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
我可以这样说。 我需要帮助。 我怀疑,如果我不是现有表格条目的数量,我必须设法说明我实际发送的记录有多少。