0

几天来,我一直在尝试解决这个问题,但我遇到了障碍。我的表单提交没有问题,我的 php 代码没有生成错误,但它没有更新 mysql 中的表。非常感谢任何帮助我提前为发布这么多代码道歉。

这是我的表单代码:

<form style="width:1000px" action="new_recipe.php" method="POST" name="frm_add_recipe">

<div id="home_wrapper">

<div id="add_recipe_box">
<h1>Add a New Recipe</h1>

<label for="recipe_name">Recipe Name:</label><br/>
<input type="text" name="recipe_name" class="add_recipe_field"/>

<label for="ingredient1">Ingredients:</label><br/>
<input type="text" name="ingredient1" class="add_recipe_field"/>


<input type="text" name="ingredient2" class="add_recipe_field"/>


<input type="text" name="ingredient3" class="add_recipe_field"/>


<input type="text" name="ingredient4" class="add_recipe_field"/>


<input type="text" name="ingredient5" class="add_recipe_field"/>


<input type="text" name="ingredient6" class="add_recipe_field"/>

<label for="ingredient7"></label>
<input type="text" name="ingredient7" class="add_recipe_field"/>


<input type="text" name="ingredient8" class="add_recipe_field"/>


<input type="text" name="ingredient9" class="add_recipe_field"/>


<input type="text" name="ingredient10" class="add_recipe_field"/><br/>

<label for="lst_meal">Select meal type:</label>
<select name="lst_meal" >
<option value="breakfast">Breakfast</option>
<option value="lunch">Lunch</option>
<option value="dinner">Dinner</option>
</select><br/>

<label>Recipe Ethnecity:</label>
<select name="lst_ethnicity">
<option value="blank">N/A</option>
<option value="American">American</option>
<option value="Asian">Asian</option>
<option value="Chineese">Chineese</option>
<option value="German">German</option>
<option value="Italian">Italian</option>
<option value="Indian">Indian</option>
<option value="Mexican">Mexican</option>
<option value="Thia">Thia</option>
</select><br/>


<label for="instructions">Cooking instructions:</label><br/>

<input name="instructions" type="text" maxlength="250" id="txt_instructions"/>

<input type="submit" value="Save" />

</div>
</div>
</form>

这是我的php:

 <?php
$recipe_name = $_POST['recipe_name'];
$ingredient1 = $_POST['ingredient1'];
$ingredient2 = $_POST['ingredient2'];
$ingredient3 = $_POST['ingredient3'];
$ingredient4 = $_POST['ingredient4'];
$ingredient5 = $_POST['ingredient5'];
$ingredient6 = $_POST['ingredient6'];
$ingredient7 = $_POST['ingredient7'];
$ingredient8 = $_POST['ingredient8'];
$ingredient9 = $_POST['ingredient9'];
$ingredient10 = $_POST['ingredient10'];
$lst_meal = $_POST['lst_meal'];
$lst_ethnicity = $_POST['lst_ethnicity'];
$instructions = $_POST['instructions'];

$dbhost = 'localhost' or die("cannot connect"); //Change to webserver info
$dbname = '*' or die("cannot connect"); //Change to webserver info
$dbuser = '*' or die("cannot connect"); //Change to webserver info
$dbpass = '*' or die("cannot connect"); //Change to webserver info
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
mysql_select_db($dbname, $conn);

$recipe_name = mysql_real_escape_string($recipe_name);
$ingredient1 = mysql_real_escape_string($ingredient1);
$ingredient2 = mysql_real_escape_string($ingredient2);
$ingredient3 = mysql_real_escape_string($ingredient3);
$ingredient4 = mysql_real_escape_string($ingredient4);
$ingredient5 = mysql_real_escape_string($ingredient5);
$ingredient6 = mysql_real_escape_string($ingredient6);
$ingredient7 = mysql_real_escape_string($ingredient7);
$ingredient8 = mysql_real_escape_string($ingredient8);
$ingredient9 = mysql_real_escape_string($ingredient9);
$ingredient10 = mysql_real_escape_string($ingredient10);
$instructions = mysql_real_escape_string($instructions);

$query = "INSERT INTO recipes ( recipe_name, ingredient1, ingredient2, ingredient3, ingredient4, ingredient5, ingredient6, ingredient7, ingredient8, ingredient9, ingredient10, meal, ethnicity, instructions )
        VALUES ( '' , '$recipe_name' , '$ingredient1' , '$ingredient2' , '$ingredient3' , '$ingredient4' , '$ingredient5' , '$ingredient6' , '$ingredient7' , '$ingredient8' , '$ingredient9' , '$ingredient10' , 'lst_meal' , '$lst_ethnicity' , '$instructions' );";
mysql_query($query);
mysql_close();
header('Location: home.php');
?>
4

3 回答 3

3

您的查询中的列数不匹配。

试试下面一个

$query = "INSERT INTO recipes ( recipe_name, ingredient1, ingredient2, ingredient3, ingredient4, ingredient5, ingredient6, ingredient7, ingredient8, ingredient9, ingredient10, meal, ethnicity, instructions )
        VALUES ('$recipe_name' , '$ingredient1' , '$ingredient2' , '$ingredient3' , '$ingredient4' , '$ingredient5' , '$ingredient6' , '$ingredient7' , '$ingredient8' , '$ingredient9' , '$ingredient10' , 'lst_meal' , '$lst_ethnicity' , '$instructions' );";
于 2013-04-13T06:38:49.393 回答
0

请不要为 offtopic 投票/标记,我只是想提供帮助(它直接处理此问题中提供的代码)。

我知道这是不请自来的,但我想我只是提出一些建议,我认为这些建议会在您将来处理像您的问题中那样的迭代情况时对您有所帮助:

您可以在 html 中使用输入数组,并在 PHP 中遍历它们,而不是使用成分 1、2、3..。您可以通过将name输入的 html 属性中的数字替换为[]. 您可以将所有十个成分输入
<input type="text" name="ingredient[]" class="add_recipe_field"/> 替换为,也可以使用一个简单的 php 循环:

<?php
for ($i=0; $i < 10; $i++)
{
    echo "<input type='text' name='ingredient[]' class='add_recipe_field'/>";
}
?>

这将$_POST['ingredient']作为一个数组提供给您的 PHP。我重写了您的 PHP 代码(是的,全部)来说明如何循环遍历它(另外,我使用了 mysqli),如下所示。这样,您只需更改上面的计数器和您的 mysql 表即可更改成分的数量(无需修改下面的代码!)。此外,(我自己会做的事情)您可以编写一些 javascript(或 jquery 等)以只有一个输入开始,并在每次用户输入成分时自动添加输入......只是一个想法。但是,是的,让我知道这是否有帮助,如果这一切都有意义。当然,我绝不是完美高效代码的典范,所以如果有人打扰阅读这个答案想要评论如何改进我写的内容,那就去吧。

<?php
$dbhost = 'localhost'; //Change to webserver info
$dbname = '*'; //Change to webserver info
$dbuser = '*'; //Change to webserver info
$dbpass = '*'; //Change to webserver info

//Connect to database
$mysqli = mysqli($dbhost, $dbuser, $dbpass, $dbname);
//Check for successful connection
if ($mysqli->connect_errno) {
    die("Failed to connect to MySQL: ".$mysqli->connect_error);
}

//Mysql table fields (besides ingredients)
$sqla = "`recipe_name`, `meal`, `ethnicity`, `instructions`";

//Manually add corresponding values (could also be done in a loop)
$sqlb = $_POST['recipe_name']; //You Probably want some sort of validation here
$sqlb .= ", ".$_POST['lst_meal'];
$sqlb .= ", ".$_POST['lst_ethnicity'];
$sqlb .= ", ".$_POST['instructions'];

//Loop through ingredients and add to query
$comma = ", "; //Leave empty if all mysql fields within loop
$ingredients = $_POST['ingredient'];
foreach(array_keys($ingredients) as $k)
{
    if ($ingredients[$k]!="") //If not blank
    {
        //Add mysql field name (+1 since index starts at 0)
        $sqla .= $comma."`ingredient".($k+1)."`";
        //Add value to other half of string
        $sqlb .= $comma."\"".$mysqli->real_escape_string($ingredients[$k])."\""; 
        $comma = ", "; //Insert comma in subsequent iterations
    }
}
//Put the query together
$query = "INSERT INTO `recipes` (".$sqla.") VALUES (".$sqlb.");";

//Try the query and die the error + sql on error
if (!$mysqli->query($query))
{
    die("MySQL Error: ".$mysqli->error."<br>Query: ".$query);
}
header('Location: home.php');
?>
于 2013-04-13T08:33:54.370 回答
-1

我认为你必须给出输入类型提交的名称。如下所示:

<?php
$save=$_POST['save'];
?>

<input type="submit" value="Save"  name="save"/>
于 2013-04-13T07:05:19.547 回答