Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to make cascading drop-down lists using mysql and php
    primarykey
    data
    text
    <p>I have to make a form with multiple drop-down menus that modify one another. The menus/database tables are:</p> <p>Categories, Styles, Types, and Mechanisms</p> <p>I have attempted to do this with my limited knowledge of Ajax, but can only seem to access MySQL once (on the initial page) to populate the Categories table without being able to then update the Styles table by querying for the next set of results. I receive an error that claims the database is empty. </p> <p>I also tried having the drop-down populated through an option group to handle both the Categories and Styles with a looped query, but only the Category headings show up with all of the Style sub-values showing up blank. My code is as follows:</p> <pre><code> $query1="SELECT categories.category_id, categories.Category_Name "; $query1.="FROM categories "; $query1.="ORDER BY categories.Category_Name ASC"; $category_result=mysql_query($query1, $connection); if(!$category_result){ die("Database query failed: " . mysql_error()); } $options=""; $con=0; while ($category_row=mysql_fetch_array($category_result)) { $category_name=$category_row["Category_Name"]; $CategoryID=$category_row["category_id"]; $options.="&lt;OPTGROUP LABEL=\"$category_name\"&gt; &lt;br /&gt;"; $query2="SELECT categories.category_id, categories.Category_Name, "; $query2.="styles.style_id, styles.Style_Name "; $query2.="FROM categories, styles "; $query2.="WHERE styles.Category_ID = $CategoryID "; $style_result=mysql_query($query2, $connection); if(!$style_result){ die("Database query failed: " . mysql_error()); } while ($style_row=mysql_fetch_array($style_result)) { $style_name=$row["Style_Name"]; $id=$row["style_id"]; $options.="&lt;OPTION VALUE=\"$id\" &lt;a href=\"#\" onClick=\"javascript:swapContent('$style_name');\" &gt;".$style_name.'&lt;/OPTION&gt;'; } $options.='&lt;/OPTGROUP&gt; &lt;br /&gt;'; } ?&gt; &lt;SELECT NAME="category_id"&gt; &lt;OPTION VALUE=0&gt;&lt;/OPTION&gt; &lt;?php echo $options ?&gt;choose &lt;/SELECT&gt; </code></pre> <p>Any insight into what I am doing wrong would be greatly appreciated!</p>
    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.
 

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