1.

Solve : How to insert the data from the DropDown Menu?

Answer»

hello guys, i need some HELP regarding on php. i am a beginner on this thing.
my problem is that how can i insert my data from my dropdown menu into my database?

please help me to solve this problem.
here is my code:

Code: [Select]$sql_select = "SELECT name FROM topic ORDER BY id";

$result = mysql_query($sql_select,$db);
$frm = "topic";

echo '<select name="'.$frm.'" id="'.$frm.'"><option value="">Select one…</option>';


while($topic_array = mysql_fetch_array($result)){

echo "<option value=\"".$topic_array[0]."\" >".$topic_array[0]."</option>\n";


}

echo '</select>';


?>




I will really appreciate some help/tips on how to solve this..

Thanks in advance.
I think you mean getting the data from the dB and into a dropdown menu?

Code: [Select]$sql_select = "SELECT name FROM topic ORDER BY id";

$result = mysql_query($sql_select);
$frm = "topic";

echo "<select name=\"$frm\" id=\"$frm\"><option value="">Select one…</option>";


while ($data = mysql_fetch_assoc( $result ) ) {
$topic=$data['name'];
echo "<option value=\"$topic\">$topic</option>\n";


}

echo '</select>';


?>
um, my problem is that how to insert my data into my database... since i've already manage to get my data from my database.. I guess I don't get what you want.
You create a pulldown from data PULLED from the database, and now you want to insert it again?Quote from: Astoria on April 29, 2008, 11:19:36 PM

I guess I don't get what you want.
You create a pulldown from data pulled from the database, and now you want to insert it again?


yes! that's exactly i want to do. how to insert it again.OK., well here is a form that will insert the SELECTED option into a dB.

Code: [Select]$sql_select = "SELECT name FROM topic ORDER BY id";
$result = mysql_query($sql_select);

echo "<form action=\"\" name=\"testform\" method=\"POST\">
<select name=\"topic\" id=\"$frm\" onChange=\"document.testform.submit\">

<option value="">Select one…</option>";

while ($data = mysql_fetch_assoc( $result ) ) {
$topic=$data['name'];
echo "<option value=\"$topic\">$topic</option>\n";
}

echo "</select>
</form>";

if(isset($_POST['topic'])) {

$SelectedValue=$_POST['topic'];

mysql_query("INSERT INTO table
(fieldname1) VALUES('$SelectedValue')")
or die(mysql_error());

echo 'Data inserted';
}

?>

In the last part, the INSERT part you need to change the values ofcourse to the names you use.ok i will try the codes that you gave me.

thanks a lot!


Discussion

No Comment Found