Page 1 of 1

Posting data to a dbase from a dropdown box

Posted: Wed Jan 05, 2005 2:42 pm
by meander
ive got a dropdown box, that takes a column from a database and inserts the values:

Code: Select all

<?php
$query = "SELECT name FROM names";
$result = @mysql_query ($query);

if ($result) {
	echo '<form><select name="names">';
	
	while ($row = mysql_fetch_array($result, MYSQL_NUM)) {
	
		echo "<option value="$rowї0]">$rowї0]\n";
	}
	
	echo '</select></form>';
	mysql_free_result ($result);
	
} else {
	echo '<p>The box could not be created due to a system error. Please try again later.</p>';
}

mysql_close();

?>
But i want to have the information posted to the database when i click the submit button. so then instead of typing the name of the person every time you can select it out of the box. i cant figure out how to do it though. help is appreciated.

Posted: Wed Jan 05, 2005 3:10 pm
by feyd
once you get the form set up correctly:

Code: Select all

if(!empty($_POST))
{
  $name = mysql_escape_string($_POST['names']);
  $sql = 'SELECT name FROM names where name = ''' . $name . '''';
  $query = mysql_query( $sql ) or die(mysql_error());
  if( mysql_num_rows( $query ) > 0 )
  {
    // user was found in the database, do whatever you want here
  }
  else
  {
    // user wasn't in the database, hacking attempt.
  }
}

Posted: Wed Jan 05, 2005 5:22 pm
by meander
well, i dont need to check if a certain name is selected, because the list of names in the box just came from the dbase. on the form, it will have that box with all the names in it, and a box for the date, and a text area for a comment. you would select which name you want the comment under, enter the date, and type the comment. when you hit submit, the name date and comment are added to a different table. i know how to go and post textboxes to the dbase but not those menus.

Posted: Wed Jan 05, 2005 5:37 pm
by feyd
I do those to make sure someone isn't trying to add random garbage or hack into the server.

inserting a select works exactly like inserting a textbox. If you have multiple selection allowed, then it can get tricky, (mildly) as you'll have to name the select tag with an array note:

Code: Select all

<select name="something&#1111;]">
the brackets [] are what need to be added for multple select to allow php to know all the values selected.