Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Populate & update a drop down list box (PHP / MYSQL)

Status
Not open for further replies.

sallieann

Programmer
Sep 2, 2003
28
GB
I have looked how to do this everywhere but can't find an answer. Can anybody help?!

I have two mysql tables - store_customer & store_company. Each customer can belong to only one company, and there can be many companies. They are related via a unique company_id. When a customers details are edited in the admin area, I would like their company to be already selected in a drop down list box. If the company they belong to needs to be changed, a different one is selected & when all amendments have been made, a button is clicked and the database is updated. I hope that made sense - I'd appreciate any help.

My form currently updates all the other details. The drop down list box code I have written so far is as follows;

Code:
print "<select class=\"box\" name=\"company_id\">
<option value=\"all\" ";if($company_id=="all"){echo "selected";}echo">$company_name</option>";
$select = mysql_query("select * from ".$prefix."store_company");
	while ($row = mysql_fetch_array($select))
      {
          $company_id = $row["company_id"];
          $company_name = $row["company_name"];
	echo"<option value=\"$company_id\" ";
if($list_comp==$company_id){echo "selected";
}echo">";
		  $company_id = $row["company_id"];
		  print "$company_name";
	echo"</option>";}
 
Your code looks to do what you saud i.e. put "selected" an s an attribute when the customers compnay is found during the load, have I missed something here ?
 
If you don't amend the customer name, it updates the database with a company_id value of '0'.

For example, if the customer is company_id '1', the page loads with the following;

customer_1 -- this shows the ACTUAL company_id
customer_1
customer_2
customer_3

You need to actually select another company, even if you want it to stay the same, you have to select the "customer_1" below it.

I want to be able to not touch the drop down if I don't want to and no change will be made to the database record.
 
Code:
while ($row = mysql_fetch_array($select)){

	$company_id = $row["company_id"];

	$company_name = $row["company_name"];
	
	// start the select list

	echo"<option value=\"$company_id\" ";
	
	// identify the selected item

	if($list_comp==$company_id){

		echo "selected>$company_name</option>\n";

	}else{
	// if not selected 
	echo ">$company_name</option>\n";
	
	}
	
}

*untested but should give you the idea what youare missing.

______________________________________________________________________
There's no present like the time, they say. - Henry's Cat.
 
Thanks for your feedback, I have done as you suggested but the database isn't being updated at all now.
 
The posted code does not show anything about updating a database or how the page is processed. It is not possible to troubleshoot your problem because there are many unknown factors. However, here are some suggestions:
-) issue a print_r($_POST) in the receiving script to inspect the posted vars. If you use GET use $)GET instead.
-) display the SQL query statement and inspect it

Best would be if you posted more code here.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top