I am trying to populate a select box from my database. The value chosen must be the id of the item, and the text shown the name
of the item so that once selected it will insert the id into the table column and not the name so that the relationships work.
Will the below do the job?
<?php
if(isset($_POST['add']))
{
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = 'password';
$dbbase = 'database';
$conn = mysql_connect($dbhost, $dbuser, $dbpass, $dbbase);
if(! $conn )
{
die('Could not connect: ' . mysql_error());
}
$sql = "SELECT company_id, company_name FROM company ORDER BY company_name DESC";
$retval = mysql_query( $sql, $conn );
$company_id = $_POST['company_id'];
$company_name = $_POST['company_name'];
}
else
{
?>
<form method="post" action="<?php $_PHP_SELF ?>">
<table width="100%" align="left" border="0" cellspacing="1" cellpadding="2">
<tr>
<select name='company'>
<option value="">--- Select ---</option>
<?php
while($row_list=mysql_fetch_assoc($retval)){
?>
<option name="'.$company_name.'" id="'.$company_id.'">'."\n";
</option>
<?php
// The rest of the form code goes in here.
</form>
<?php
}
?>
I need to then use the company_id value from the form to be saved into the database using my insert sql. but when I need to reference the selected value of the select box in my sql, how do I do that?
I am slowly, very slowly, getting there with this little project of mine. Might be up and running in about two decades or so!!