Dropdown Select Box loaded from DB

Hi all,

I’m new to the forum and hoping someone maybe able to help. I am making a parts database for my office. I have 2 tables I’m using on the page. One is “Parts” which has a field called “part_cat” which has the category id number its a member of and a table called “Categories” which has 2 fields “cat_id” & “cat_name”. Im trying to may a page to update the parts records and I am stuck on the select drop down box. If anyone has any ideas or can give me some help I would be grateful. The page has already loaded * from the parts table.

[php]

    $sql = "SELECT * FROM categories ORDER BY cat_name";
    $result = mysql_query($sql);

    if(!$result)
    {
      echo 'Error';
    }

    else
        echo '<td><select name="part_cat">';
        while ($row = mysql_fetch_assoc($result))
    {
                            echo '<option value="' . $row['cat_id'] . '"';

                            if ($part_cat=' . $row[cat_id] . ')
                            {
                              echo 'selected>' . $row['cat_name'] . '</option>\n';
                            }
                            else
                            { 
                              echo '>' . $row['cat_name'] . '</option>\n';
                            }
    }
                            echo '</select></td>';

[/php]

Thank you. :slight_smile:

what would you want to update the parts records with? info from the categories table?

The select form shows the information from the categories table, but I can not get it to select the correct category that already set in the parts table. ( The Parts table only has the category ID that the part of been assigned to.)

dont know if this will do…
but if part_cat is not getting to correct cat_id
try to change this:
[php]
if ($part_cat=’ . $row[cat_id] . ‘) //this $row does not seem to have " ’ " around the [cat_id] ?\
[/php]
To:
[php]
if ($part_cat=’ . $row[‘cat_id’] . ')
[/php]

There was an error in your if statement

[php]if ($part_cat=’ . $row[cat_id] . ')[/php]

should be changed to…
[php]if ($part_cat = $row[‘cat_id’])[/php]

It also seems like $part_cat is undefined unless it is defined elsewhere and you did not include that in the code you provided?

AWHA! i was on the right track with this one! would my code have worked too ? or would it just not work with the

 ". ______ ." 

…?

you are also missing your curly braces for your else statement

Hi,

Thanks again for all your help. I made the changes but I still not getting the select box to select the correct option. The result I’m getting is it just has selected the last category listed and not the category that already added to the part. Hope that makes sense. Please find below all the code from the page. (The page does go on after this but I did not think I needed to display all that.)

[php]
require (“header.php”);
require (“connect.php”);

$id=$_GET[‘id’];

$part_sql=“SELECT * FROM parts WHERE part_id = ‘$id’”;
$part_result=mysql_query($part_sql);

    if(!$part_result)
    {
      echo 'Error';
    }

    else
    {
      	while($part_row = mysql_fetch_array($part_result))
    {

      echo '<table border="0" width="700" align="center">
       <tr>
       <td><form name="form1" method="post" action="update_ac9.php"><td>
                 <table width="100% border="0" cellspacing="1" cellpadding="3">
                		<td>Part Number : </td>
                  		<td><input name="part_number" type="text" id="part_number" size="28" value="' . $part_row['part_number'] . '"></td>
                            <td>Description : </td>
                        <td><input name="part_description" type="text" id="part_description" size="28" value="' . $part_row['part_description'] . '"></td>
                        </tr>

                        <tr>
	                <td>Category : </td>';


	                

    $part_cat = $part_row['part_cat'];
    $sql = "SELECT * FROM categories ORDER BY cat_name";
    $result = mysql_query($sql);

    if(!$result)
    {
      echo 'Error';
    }

    else
        echo '<td><select name="part_cat">';
        while ($row = mysql_fetch_assoc($result))
    {
                            echo '<option value="' . $row['cat_id'] . '"';

                            if ($part_cat= $row['cat_id'])
                            {
                              echo 'selected>' . $row['cat_name'] . '</option>\n';
                            }
                            else
                            {
                              echo '>' . $row['cat_name'] . '</option>\n';
                            }
    }
                            echo '</select></td>';

    }

[/php]

change this…
[php] if ($part_cat= $row[‘cat_id’])[/php]
to…
[php] if ($part_cat === $row[‘cat_id’])[/php]

Thank you. That got it working great. :slight_smile:

Sponsor our Newsletter | Privacy Policy | Terms of Service