Jump to content

Populating Drop Down Menus


phpBegginer

Recommended Posts

Hi Everyone,

 

I am realtivly new to PHP and i am currently trying to create a website for a friends company, i am at the moment working on the admin side i.e adding categories and products.  I am currently trying to make a drop down list that will allow them to add a product to a category that already exsists, however i am having some problems with the code, could some please have a look at it and offer me some help? The code is below?

 

<?php // Retrieve all the categories and add to the pull-down menu.

$query = "SELECT category_id,category_name FROM category ORDER BY category_name";

$result = mysqli_query ($dbc, $query);

while ($row = mysqli_fetch_array ($result)) {

echo "<option value=\"{$row['category_name']}\">{$row['category_id']}</option>\n";

}

mysqli_close($dbc); // Close the database connection.

?>

Link to comment
https://forums.phpfreaks.com/topic/92768-populating-drop-down-menus/
Share on other sites

What is the problem with the code?

<?php 
// Retrieve all the categories and add to the pull-down menu.
$query = mysqli_query("SELECT `category_id`, `category_name` FROM `category` ORDER BY `category_name` ASC", $query); //Make sure it's being orderd b ASC or DESC. Also try using as little variables as possible to save memory. 
while ($row = mysqli_fetch_array ($result))
{
    echo "<option value=\"{$row['category_id']}\">{$row['category_name']}</option>\n"; // I think you meant to put catagory_id and catagory_name the other way round.
}
mysqli_close($dbc); // Close the database connection.
?>

Try this too and see if it returns an error:

<?php 
// Retrieve all the categories and add to the pull-down menu.
$query = mysqli_query("SELECT `category_id`, `category_name` FROM `category` ORDER BY `category_name` ASC", $dbc) or die(mysql_query()); //Make sure it's being orderd b ASC or DESC. Also try using as little variables as possible to save memory. 
while ($row = mysqli_fetch_array ($result))
{
    echo "<option value=\"{$row['category_id']}\">{$row['category_name']}</option>\n"; // I think you meant to put catagory_id and catagory_name the other way round.
}
mysqli_close($dbc); // Close the database connection.
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.