Php multselect框仅更新mysql表中第一个选定字段的输出

Php multselect框仅更新mysql表中第一个选定字段的输出,php,html,mysql,Php,Html,Mysql,我有一个表单,应该向两个mysql表recipes和recipe_Components添加数据。后者是链接实体。表单中的一个字段是多选下拉框。当我使用CTRL键选择多个条目时,结果只会在下拉框中返回一个选择的选项…我已经在这里呆了几个小时了..任何建议..这是我的代码 HTML表单 <table class="form_table"> <tr><form method="POST" onSubmit action="thankyou.php"> </tr

我有一个表单,应该向两个mysql表recipes和recipe_Components添加数据。后者是链接实体。表单中的一个字段是多选下拉框。当我使用CTRL键选择多个条目时,结果只会在下拉框中返回一个选择的选项…我已经在这里呆了几个小时了..任何建议..这是我的代码

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>

什么为每个选项返回此查询<代码>$sql=“从配料中选择配料”\u id,配料名称=“$selectedOption”你是否检查了$_POST['Components'],这是一个数组?该查询基本上是来自会话的一个值..它是登录的用户的名称,php使用它将正在创建的配方分配给创建它的用户…我是php新手,所以我有一些问题,但是这个查询的结果是什么?可能您的数据库中没有数据,而此查询返回0行的某些igElement?
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