几天来,我一直在尝试解决这个问题,但我遇到了障碍。我的表单提交没有问题,我的 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');
?>