PHP Help

From: Ken (SHIELDSIT) 5 Mar 2010 15:31
To: ALL1 of 3
Can someone tell me why this code won't work? I'm new to PHP. When I click submit it doesn't do anything, other than that it pulls the info I want it to.

code:
<select name="user">
<FORM METHOD="POST" ACTION="form.php">
<?php 
 
$db_name = "dsm";
$table_name = "dsm_names";
$connection = mysql_connect("localhost", "dsm", "1234")
	or die(mysql_error());
 
$db = @mysql_select_db($db_name, $connection) or die(mysql_error());
 
 
$sql = "SELECT * FROM $table_name ORDER BY first";
 
$result = @mysql_query($sql,$connection) or die(mysql_error());
 
while ($row = mysql_fetch_array($result)) { 
 
$user_id = $row['id'];
$first_name = $row['first'];
$last_name = $row['last'];
 
$user_name = trim("$first_name $last_name");
 
echo "<option value=\"".$row['id']."\">".$user_name."\n  ";
 
}
 
 
?>
</select>
<INPUT TYPE="Submit" Name="Submit" Value="Submit Form">
From: Peter (BOUGHTONP) 5 Mar 2010 16:23
To: Ken (SHIELDSIT) 2 of 3
Your HTML is broken - you can't put a FORM tag inside a SELECT tag.

Haven't spotted any other obvious problems (but then I haven't done much PHP recently, so could easily be missing something).

Also it's generally a bad idea to mix business logic with display logic - do all your database lookups first, then do the display stuff with minimal PHP/looping second.


For example:

PHP code:
<?php
 
	$db_name = "dsm";
	$table_name = "dsm_names";
	$connection = mysql_connect("localhost", "dsm", "1234")
		or die(mysql_error());
 
	$db = @mysql_select_db($db_name, $connection) or die(mysql_error());
 
 
	$sql = "SELECT id,first,last FROM $table_name ORDER BY first";
 
	$result = @mysql_query($sql,$connection) or die(mysql_error());
 
?>
 
<form method="post" action="form.php">
 
 
	<select name="user">
	<?php
 
		while ($row = mysql_fetch_array($result))
		{
			$user_name = trim("$row['first'] $row['last']");
 
			echo "<option value=\"".$row['id']."\">".$user_name."</option>\n  ";
 
		}
 
	?>
	</select>
 
 
	<input type="Submit" name="Submit" value="Submit Form" />
 
</form>


Still not perfect, but probably good enough. :)
EDITED: 5 Mar 2010 16:25 by BOUGHTONP
From: Ken (SHIELDSIT) 5 Mar 2010 16:31
To: Peter (BOUGHTONP) 3 of 3
That's what it was! You are my new hero Pete!