MYSQL表不会更新

时间:2013-04-13 06:29:46

标签: php mysql

我一直在努力解决这个问题几天,我正在打砖墙。我的表单提交没有问题,我的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');
?>

3 个答案:

答案 0 :(得分: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' );";

答案 1 :(得分:0)

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

我知道这是未经请求的,但我想我会抛出一些建议,我认为这些建议会在接近你未来问题中的迭代情况时帮助你:

而不是使用ingredient1,2,3 ..你可以在你的html中使用一组输入,并在你的PHP中迭代它们。您可以使用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代码(是的,所有这些)来说明如何循环它(另外,我使用myqsqli),如下所示。这样,您可以通过更改上面的计数器和您的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');
?>

答案 2 :(得分:-1)

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

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

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