Note that there are some explanatory texts on larger screens.

plurals
  1. POPHP/mySQL Dropdown box issues
    primarykey
    data
    text
    <p>I'm a newbie at php&amp;mySQL and this is for a school project. </p> <p>Im trying to add a drop down to the food_group column but having issues. As it stands right now I have the dropdown "working" however it only lists the first item in the list for all record it retrieves. Not what they are currently set to. Im not sure if its the while within the while that doing it but I'm at a point that I look at my code and dont see the issue. You can see the page Im talking about at: <a href="http://web.nmsu.edu/~jrortiz/ICT458/FINAL/updateFood.php" rel="nofollow">http://web.nmsu.edu/~jrortiz/ICT458/FINAL/updateFood.php</a> to get a visual. </p> <p>I need the drop down to show the friendly name from the food_group.food_group table. But it needs to save the food_group.id value. So when I pull a basic report it shows up like: <a href="http://web.nmsu.edu/~jrortiz/ICT458/FINAL/food.php" rel="nofollow">http://web.nmsu.edu/~jrortiz/ICT458/FINAL/food.php</a></p> <p>I'm not sure what I'm doing wrong in this section of code: (Or really if it is even in this part of it)</p> <pre><code> while ($row = mysqli_fetch_array($myData2)) { $options.= '&lt;option value="'.$row['food_group'].'"&gt;'.$row['food_group'].'&lt;/option&gt;'; }; echo "&lt;td&gt;&lt;SELECT NAME=Food_group&gt;"; echo $options; echo "&lt;/SELECT&gt;&lt;/td&gt;"; </code></pre> <p>Here is the full block of code:</p> <pre><code>&lt;html&gt; &lt;head&gt; &lt;/head&gt; &lt;body&gt; &lt;?php $con = mysqli_connect("localhost","user","pw","db"); if (!$con){ die("Can not connect: " . mysql_error()); } if(isset($_POST['update'])){ $UpdateQuery = "UPDATE food SET food_group='$_POST[Food_group]', food='$_POST[Food]', ph='$_POST[PH]' WHERE food='$_POST[hidden]'"; mysql_query($UpdateQuery, $con); }; if(isset($_POST['delete'])){ $DeleteQuery = "DELETE FROM food WHERE Food='$_POST[hidden]'"; mysql_query($DeleteQuery, $con); }; if(isset($_POST['add'])){ $AddQuery = "INSERT INTO food (Food_group, Food, PH) VALUES ('$_POST[addGroup]','$_POST[addFood]','$_POST[addPH]')"; mysql_query($AddQuery, $con); }; $sql = "SELECT * FROM food"; $myData = mysqli_query($con,$sql); $sql2="select * from food_group"; $myData2 = mysqli_query($con,$sql2) or die(mysql_error()); echo "&lt;table border=1&gt; &lt;tr&gt; &lt;th&gt;Food Group&lt;/th&gt; &lt;th&gt;Food&lt;/th&gt; &lt;th&gt;PH&lt;/th&gt; &lt;th&gt;Update/Add&lt;/th&gt; &lt;th&gt;Delete&lt;/th&gt; &lt;/tr&gt;"; while($record = mysqli_fetch_array($myData)){ echo "&lt;form action=updateFood.php method=post&gt;"; echo "&lt;tr&gt;"; while ($row = mysqli_fetch_array($myData2)) { $options.= '&lt;option value="'.$row['food_group'].'"&gt;'.$row['food_group'].'&lt;/option&gt;'; }; echo "&lt;td&gt;&lt;SELECT NAME=Food_group&gt;"; echo $options; echo "&lt;/SELECT&gt;&lt;/td&gt;"; echo "&lt;td&gt;&lt;input type='text' name='Food' value='$record[food]'/&gt;&lt;/td&gt;"; echo "&lt;td&gt;&lt;input type='text' name='PH' value='$record[ph]'/&gt;&lt;/td&gt;"; echo "&lt;td&gt;&lt;input type='submit' name='update' value='update'/&gt;&lt;/td&gt;"; echo "&lt;td&gt;&lt;input type='submit' name='delete' value='delete'/&gt;&lt;/td&gt;"; echo "&lt;td&gt;&lt;input type='hidden' name='hidden' value='$record[food]'/&gt;&lt;/td&gt;"; echo "&lt;/tr&gt;"; echo "&lt;/form&gt;"; } echo "&lt;form action=updateFood.php method=post&gt;"; echo "&lt;tr&gt;"; echo "&lt;td&gt;&lt;input type='text' name='addGroup'&gt;&lt;/td&gt;"; echo "&lt;td&gt;&lt;input type='text' name='addFood'&gt;&lt;/td&gt;"; echo "&lt;td&gt;&lt;input type='text' name='addPH'&gt;&lt;/td&gt;"; echo "&lt;td&gt;&lt;input type='submit' name='add' value='add'/&gt;&lt;/td&gt;"; echo "&lt;/tr&gt;"; echo "&lt;/form&gt;"; echo "&lt;/table&gt;"; mysqli_close($con); ?&gt; &lt;/body&gt; &lt;/html&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload