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
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.
?>

Link to comment
Share on other sites

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.
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.