Page 1 of 1

3 dynamic dropdown boxes

Posted: Wed Jan 12, 2011 4:54 pm
by merchhaus
I'm pretty new to PHP and I am having trouble with 3 dynamic dropdown boxes for selecting the heirarchy for a product database. I am using onchange to reload the form and passing the variables through the URL. All three dropdowns are populating correctly and the first 2 dropdowns are posting to the database, but the 3rd dropdown is not. I'm assuming there is an easy fix but I've been looking at it for hours and can't figure it out. Thanks for any help. Here is the code for populating the dropdown boxes...

Code: Select all

<?php

///////// Getting the data from Mysql table for first list box//////////
$catquer=mysql_query("SELECT DISTINCT category,cat_id FROM category order by category"); 	
///////////// End of query for first list box////////////

/////// for second drop down list - check if category is selected else display all the subcategory///// 
$cat=$_GET['cat'];
if(isset($cat) and strlen($cat) > 0){
$subcatquer=mysql_query("SELECT DISTINCT subcategory,subcat_id FROM subcategory where cat_id=$cat order by subcategory"); 
}else{$subcatquer=mysql_query("SELECT DISTINCT subcategory,subcat_id FROM subcategory order by subcategory"); } 
////////// end of query for second subcategory drop down list box ///////////////////////////

/////// third drop down list ///// 
$subcat=$_GET['subcat'];
if(isset($subcat) and strlen($subcat) > 0){
$prodtypequer=mysql_query("SELECT DISTINCT product_type FROM product_type where subcat_id=$subcat order by product_type"); 
}else{$prodtypequer=mysql_query("SELECT DISTINCT product_type FROM product_type order by product_type"); } 
////////// end of query for third subcategory drop down list box ///////////////////////////

and here is the code for the form...

Code: Select all

echo "<form method=post name=f1 action='item.php'>";

//////////        Starting of first drop downlist /////////
echo "<select name='cat' onchange=\"reload(this.form)\"><option value=''>Select Category</option>";
while($row = mysql_fetch_array($catquer)) { 
if($row['cat_id']==@$cat){echo "<option selected value='$row[cat_id]'>$row[category]</option>"."<BR>";}
else{echo  "<option value='$row[cat_id]'>$row[category]</option>";}
}
echo "</select>";
//////////////////  This will end the first drop down list ///////////
?>
<br>
<br>
<?php

//////////        Starting of second drop downlist /////////
echo "<select name='subcat' onchange=\"reload3(this.form)\"><option value=''>Select Subcategory</option>";
while($row2 = mysql_fetch_array($subcatquer)) { 
if($row2['subcat_id']==@$subcat){echo "<option selected value='$row2[subcat_id]'>$row2[subcategory]</option>"."<BR>";}
else{echo  "<option value='$row2[subcat_id]'>$row2[subcategory]</option>";}
}
echo "</select>";
//////////////////  This will end the second drop down list ///////////
?>
  <br>
  <br>
<?php

//////////        Starting of third drop downlist /////////
echo "<select name='product_type'><option value=''>Select Product Type</option>";
while($row3 = mysql_fetch_array($prodtypequer)) { 
{echo "<option value='$row3[type_id]'>$row3[product_type]</option>";}
}
echo "</select>";
//////////////////  This will end the third drop down list ///////////
?>

Re: 3 dynamic dropdown boxes

Posted: Fri Jan 14, 2011 10:12 am
by Jade
Where's the code where you're doing the post to the database? This only shows how you're generating the drop down boxes.

Re: 3 dynamic dropdown boxes

Posted: Fri Jan 14, 2011 2:48 pm
by merchhaus
Thanks for the response, but I actually figured it out.

Now I am trying to attach the user id of the person posting the product to the product id. When the user logs in I set the username Session variable. I'm thinking the best solution for getting the user_id is to do a query like

$query = 'SELECT user_id FROM site_user WHERE username = $_SESSION['username'];
$result = mysql_query($query, $db) or die(mysql_error());

but then can I insert the user_id into the product table by doing

$query = 'INSERT INTO user_product
(user_id, prod_id)
VALUES
(user_id, $prod_id);
$result = mysql_query($query, $db) or die(mysql_error());

I am really new to PHP and I'm not sure if this will work or if there is a better way to do this. thanks for any help you can give

Re: 3 dynamic dropdown boxes

Posted: Fri Jan 14, 2011 4:12 pm
by Jade
Are usernames unique? If you're always going to use the user_id as the key to the products then you should store the user_id in a session instead. You have a few errors in your code but the concept is correct. Also, please use the PHP code tags around your code, it makes reading it much easier.

Code: Select all

//store the user_id instead of the username so you don't have to query for the user_id every time you need it
$user_id = $_SESSION['user_id'];

mysql_query('INSERT INTO user_product (user_id, prod_id) VALUES ($user_id, $prod_id)') 
or die (mysql_error());

Re: 3 dynamic dropdown boxes

Posted: Sun Jan 16, 2011 2:50 pm
by merchhaus
user_id is always the key for products so I should set the user_id session. In my login page I am setting the session for username, but how do I set the session for user_id? Here is my login page

Code: Select all

    if (mysql_num_rows($result) > 0) {
        $row = mysql_fetch_assoc($result);
        $_SESSION['username'] = $username;
        $_SESSION['logged'] = 1;
        $_SESSION['admin_level'] = $row['admin_level'];
        header ('Refresh: 2; URL=' . $redirect);
        echo '<p>You will be redirected to your original page request.</p>';
        echo '<p>If your browser doesn\'t redirect you properly automatically, ' .
            '<a href="' . $redirect . '">click here</a>.</p>';
        mysql_free_result($result);
        mysql_close($db);
        die();
Can I query the database for user_id and then set it as a session?

Code: Select all

$query = 'SELECT user_id FROM site_user WHERE username = $username;

Re: 3 dynamic dropdown boxes

Posted: Fri Jan 21, 2011 1:09 pm
by Jade
You just add a variable to the $_SESSION array.

Code: Select all

    if (mysql_num_rows($result) > 0) {
        $row = mysql_fetch_assoc($result);
        $_SESSION['username'] = $username;
        $_SESSION['userid'] = $row['userid']; //pull their user ID from the database result
        $_SESSION['logged'] = 1;
        $_SESSION['admin_level'] = $row['admin_level'];

        //you should also close the session when you're done writing to it
        session_write_close();

        header ('Refresh: 2; URL=' . $redirect);
        echo '<p>You will be redirected to your original page request.</p>';
        echo '<p>If your browser doesn\'t redirect you properly automatically, ' .
            '<a href="' . $redirect . '">click here</a>.</p>';
        mysql_free_result($result);
        mysql_close($db);
        die();

Re: 3 dynamic dropdown boxes

Posted: Sat Jan 22, 2011 8:03 am
by merchhaus
Awesome. Thanks for your help