Kathy Posted July 13, 2009 Share Posted July 13, 2009 I need to populate a drop down box with a table named categories and a field called Category, how do I go about doing this?? ??? Quote Link to comment https://forums.phpfreaks.com/topic/165774-drop-down/ Share on other sites More sharing options...
ignace Posted July 13, 2009 Share Posted July 13, 2009 $query = 'SELECT id, category FROM categories'; $result = mysql_query($query, $db); if ($result) { $htmlSelect = '<select name="category-select">'; while (list($id, $category) = mysql_fetch_array($result, MYSQL_NUM)) { $htmlSelect .= "<option value=\"$id\">$category</option>"; } $htmlSelect .= '</select>'; } <form action="" method="post"> ... <label>Category: <?php print $htmlSelect; ?></label> ... </form> Quote Link to comment https://forums.phpfreaks.com/topic/165774-drop-down/#findComment-874446 Share on other sites More sharing options...
Kathy Posted July 13, 2009 Author Share Posted July 13, 2009 Warning: mysql_query(): supplied argument is not a valid MySQL-Link resource I get this when I run it The problem is over here : $result = mysql_query($query, $db); ? Quote Link to comment https://forums.phpfreaks.com/topic/165774-drop-down/#findComment-874453 Share on other sites More sharing options...
trq Posted July 13, 2009 Share Posted July 13, 2009 Does $db store your connection? Quote Link to comment https://forums.phpfreaks.com/topic/165774-drop-down/#findComment-874454 Share on other sites More sharing options...
Kathy Posted July 13, 2009 Author Share Posted July 13, 2009 Is this right? function select_db(){ mysql_connect("localhost","login","pass")or die(mysql_error()); mysql_select_db("dbname")or die(mysql_error()); } $query = 'SELECT id, Category FROM categories'; $result = mysql_query($query, $db); if ($result) { $htmlSelect = '<select name="category-select">'; while (list($id, $category) = mysql_fetch_array($result, MYSQL_NUM)) { $htmlSelect .= "<option value=\"$id\">$category</option>"; } $htmlSelect .= '</select>'; } Quote Link to comment https://forums.phpfreaks.com/topic/165774-drop-down/#findComment-874462 Share on other sites More sharing options...
trq Posted July 13, 2009 Share Posted July 13, 2009 No, you never call you function to make the connection, nor do you pass your connection back out to where it can be useful. Should always check your queries actually return records before trying to use them too. function select_db(){ $db = mysql_connect("localhost","login","pass")or die(mysql_error()); mysql_select_db("dbname")or die(mysql_error()); return $db; } $db = select_db(); $query = 'SELECT id, Category FROM categories'; $result = mysql_query($query, $db); if ($result) { if (mysql_num_rows($result)) { $htmlSelect = '<select name="category-select">'; while (list($id, $category) = mysql_fetch_array($result, MYSQL_NUM)) { $htmlSelect .= "<option value=\"$id\">$category</option>"; } $htmlSelect .= '</select>'; } } Quote Link to comment https://forums.phpfreaks.com/topic/165774-drop-down/#findComment-874479 Share on other sites More sharing options...
Kathy Posted July 13, 2009 Author Share Posted July 13, 2009 I'm being a total newbie here, I've tried it like this, but it's obviously still wrong: <?PHP mysql_connect("dedi536.nur4.host-h.net","ivif09","ivifpa55")or die(mysql_error()); mysql_select_db("web_cat09")or die(mysql_error()); ?> getting the same mysql error I appreciate any help you can give me Quote Link to comment https://forums.phpfreaks.com/topic/165774-drop-down/#findComment-874501 Share on other sites More sharing options...
Kathy Posted July 13, 2009 Author Share Posted July 13, 2009 Okay great, I've managed to populate my drop down: <?PHP $link = mysql_connect("host","login","pass"); mysql_select_db("dbname", $link); $query = "select Category FROM categories"; $results = mysql_query($query, $link) or die("Error performing query"); if(mysql_num_rows($results) > 0){ echo("<select name=\"Category\">"); while($row = mysql_fetch_object($results)){ echo("<option value=\"$row->record_id\">$row->Category</option>"); } echo("</select>"); } else{ echo("<i>No values found</i>"); }?> I now want to populate another drop down from a choice in this drop down using the WHERE query, can anyone offer me guidance? Quote Link to comment https://forums.phpfreaks.com/topic/165774-drop-down/#findComment-874519 Share on other sites More sharing options...
ignace Posted July 13, 2009 Share Posted July 13, 2009 Assuming you want to select a subcategory: $link = mysql_connect("host","login","pass"); mysql_select_db("dbname", $link); $query = "select Category FROM categories"; $results = mysql_query($query, $link) or die("Error performing query"); if(mysql_num_rows($results) > 0){ echo("<select name=\"Category\">"); while($row = mysql_fetch_object($results)){ echo("<option value=\"$row->record_id\">$row->Category</option>"); } echo("</select>"); } else{ echo("<i>No values found</i>"); } if (!empty($_POST)) { if (empty($_POST['subcategory'])) { $query = 'SELECT id, subcategory FROM subcategories'; $result = mysql_query($query, $link); if (mysql_num_rows($result)) {// no need for > 0; if mysql_num_rows() == 0 then the if body won't be executed as 0 == false echo('<select name="subcategory">'); while (list($id, $subcategory) = mysql_fetch_array($result, MYSQL_NUM)) { echo("<option value=\"$id\">$subcategory</option>"); } echo('</select>'); } } else if (!empty($_POST['category'] && !empty($_POST['subcategory'])) { $cat = $_POST['category']; $subcat = $_POST['subcategory']; ..process.. } } Quote Link to comment https://forums.phpfreaks.com/topic/165774-drop-down/#findComment-874531 Share on other sites More sharing options...
Kathy Posted July 14, 2009 Author Share Posted July 14, 2009 Something is wrong with this piece of code here: <?PHP while (list($id, $coName) = mysql_fetch_array($result, MYSQL_NUM)) { echo("<option value=\"$id\">$coName</option>"); } echo('</select>'); } } else if (!empty($_POST['category'] && !empty($_POST['coName']))) { $cat = $_POST['category']; $subcat = $_POST['coName']; ..process.. } } ?> I've added another ) after ['coName'], but I'm still receiving this error when I run the script? Parse error: syntax error, unexpected T_BOOLEAN_AND, expecting ')' Quote Link to comment https://forums.phpfreaks.com/topic/165774-drop-down/#findComment-875023 Share on other sites More sharing options...
ignace Posted July 15, 2009 Share Posted July 15, 2009 Their is alot missing from this code: ..something here.. ..something here.. ..something here.. while (list($id, $coName) = mysql_fetch_array($result, MYSQL_NUM)) { echo("<option value=\"$id\">$coName</option>"); } echo('</select>'); } } else if (!empty($_POST['category'] && !empty($_POST['coName'])) { $cat = $_POST['category']; $subcat = $_POST['coName']; ..process.. } } Quote Link to comment https://forums.phpfreaks.com/topic/165774-drop-down/#findComment-875740 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.