我有一个表单应该将数据添加到两个mysql表食谱和recipe_ingredients。后者是一个链接实体。其中一个字段是多选下拉框。当我使用CTRL选择多个entrys时,结果只会在下拉框中返回一个选定的选项...我已经在这里工作了几个小时..没有任何建议......我的代码是
HTML表格
<table class="form_table">
<tr><form method="POST" onSubmit action="thankyou.php">
</tr><tr>
<td class="ctable_row">Recipe Name:</td>
<td class="ctable_row"><input type="text" name="recipename"></td></tr>
<tr>
<td class="ctable_row">Recipe Author:</td>
<td class="ctable_row"><input type="text" name="recipeauthor"></td>
</tr>
<tr>
<td class="ctable_row">Cook Time (hh:mm:ss): </td>
<td class="ctable_row"><input type="text" name="cooktime"></td>
</tr>
<tr>
<td class="ctable_row">Select From A List of Ingredients<br/>(**hold CTRL to ``select<br/> multiple values**)</td>
<td class="ctable_row"><select multiple="multiple" name="Ingredients[]">
<option value="Broccoli">Broccoli</option>
<option value="Carrots">Carrots</option>
<option value="Cheese">Cheese</option>
<option value="Macaroni">Macaroni</option>
<option value="Oil">Oil</option>
<option value="Flour">Flour</option>
<option value="Eggs">Eggs</option>
<option value="Eggs">Chicken</option>
</select>
</select></td></tr>
<tr>
<td class="ctable_row">Recipe Stages</td>
<td colspan="2"><textarea rows="20" cols="65" name="stages">
Input Recipe Stages here...
</textarea>
<input type="submit" value="Submit">
</td></tr>
</form>
</div>
</td>
PHP代码
session_start(); $q=mysql_connect("localhost",'root','');
$db=mysql_select_db("tasteofhome");
$recipename=$_POST['recipename'];
$recipeauthor=$_POST['recipeauthor'];
$cooktime=$_POST['cooktime'];
$stages=$_POST['stages'];
$name=$_SESSION['login_user'];
$sql2="SELECT user_id from users where user_name='$name'";
$q1=mysql_query($sql2);
while ($row=mysql_fetch_array($q1)){
$s="insert into recipes (recipe_name,recipe_author,cook_time,stages_description,user_id)values('$recipename','$recipeauthor','$cooktime','$stages','$row[0]');
";
$sql5=mysql_query($s);
}
if($sql5 === FALSE) {
die(mysql_error()); // TODO: better error handling
}
if($sql5)
{
}
else {
echo "data not inserted";
}
foreach ($_POST['Ingredients'] as $selectedOption){
$sql="select ingredients_id from ingredients where ingredients_name='$selectedOption' ";
$q=mysql_query($sql); }
while ($rows = mysql_fetch_array ($q))
{
$sql3="select recipe_id from recipes where recipe_name='$recipename' ";
$s=mysql_query($sql3);
echo $rows[0] ;
while ($table2=mysql_fetch_array($s))
{
$sql2="insert into recipe_ingredients (ingredients_id,recipe_id)values('$rows[0]','$table2[0]')";
$w=mysql_query($sql2);
}}
if($w)
{
echo "Data succesfully entered in the database
";
}
if($w == true){
header ('refresh:2;url=myrecipes.php');
}
?>
Back