Get Selected Value from Database
Posted: Wed Aug 05, 2009 5:12 pm
Need some help again. It's been awhile...
I have a mySQL record of customers. And a form with a drop down. If there is no data in the database for this drop down (assigned customer), then I want it to let the user select a person to be assigned to that company. This part of the code works. However, if the data already exists in the database, I want to make the drop down already select that person as the assigned customer, however, I also want there to be an option to have that other person be selected at any given time. I also want this code to be as portable as possible, in regards to the fact that if I add another person to the database, then the drop down will populate with the new person as an option selection to assign a company to it.
So here's the code I have so far:
The actual form:
Let me know if additional information is required.
I have a mySQL record of customers. And a form with a drop down. If there is no data in the database for this drop down (assigned customer), then I want it to let the user select a person to be assigned to that company. This part of the code works. However, if the data already exists in the database, I want to make the drop down already select that person as the assigned customer, however, I also want there to be an option to have that other person be selected at any given time. I also want this code to be as portable as possible, in regards to the fact that if I add another person to the database, then the drop down will populate with the new person as an option selection to assign a company to it.
So here's the code I have so far:
Code: Select all
function getAssignedCompany() {
$custID = $_POST['customerID'];
$query = "SELECT AssignedCompanyID FROM customers WHERE CustomerID='$custID'";
$result = mysql_query($query);
$row = mysql_fetch_array( $result );
echo $row['AssignedCompanyID'];
if (!$row['AssignedCompanyID'] || $row['AssignedCompanyID'] == NULL) { ?>
<option>No Data Found, please select a company.</option>
<option>Person One</option>
<option>Person Two</option>
<?php
}
else { ?>
<option>Person One</option>
<option>Person Two</option>
<?php } ?>
<?php }Code: Select all
Assigned Company: <select>
<?php
echo getAssignedCompany();
?>
</select>