Jump to content

filling a drop down select box from MYSQL table


stonebergftw

Recommended Posts

Is there any way to fill a <select> box with entries from a mysql table, and then to have the entries POST to fill in another mysql table.

 

Example.

 

MYSQL table 1

id name

1Tim

2Joe

3Bob

 

Have a drop down select box from table 1, and then fill in the selections in the auto-incrementing table 2, formatted as follows:

 

MYSQL table 2

id

name

 

Is this even possible?

Rules noted.

 

Ok, to specifics.  Can you explain why this is not working?  I think there is something wrong with the following:

echo "<option value='<?php echo $donorname;?>'>$donorname</option>";

 

Out of the following code:

 

<?php $donor=@mysql_query('SELECT id, locationname FROM donors'); ?>

<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
    <label>Donor:
    <select name="donor">
<?php
while ($donors=mysql_fetch_array($donor)) {
$donorname=$donors['locationname'];
echo "<option value='<?php echo $donorname;?>'>$donorname</option>";
}
?>
</select>
<input type="submit" value="SUBMIT"/>
</form>

 

There could be something wrong with this code block, but I'm really not sure:

<?php
$locationname = $_POST['donor'];
$donorid = mysql_query("SELECT id FROM donors WHERE locationname='$locationname'");

$sql = "INSERT INTO donations SET donorid='$donorid'";
mysql_query($sql);
?>

 

The select list reads the data just fine, but the

INSERT INTO donations SET donorid='$donorid'"

is not executing properly.  Lets assume that all MYSQL tables are correct and the format of the tables is irrelevant.  Any ideas?  Help is really really appreciated.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.