Posting data to a dbase from a dropdown box

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
meander
Forum Commoner
Posts: 26
Joined: Sun Oct 10, 2004 3:09 pm

Posting data to a dbase from a dropdown box

Post 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.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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.
  }
}
User avatar
meander
Forum Commoner
Posts: 26
Joined: Sun Oct 10, 2004 3:09 pm

Post 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.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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.
Post Reply