CodingPHP Help

 

Press Ctrl+Enter to quickly submit your post
Quick Reply  
 
 
  
 From:  Ken (SHIELDSIT)  
 To:  ALL
37206.1 
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">


Sorry, I can't hear a word you're saying!
0/0
 Reply   Quote More 

 From:  Peter (BOUGHTONP)  
 To:  Ken (SHIELDSIT)     
37206.2 In reply to 37206.1 
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. :)
0/0
 Reply   Quote More 

 From:  Ken (SHIELDSIT)  
 To:  Peter (BOUGHTONP)     
37206.3 In reply to 37206.2 
That's what it was! You are my new hero Pete!


Sorry, I can't hear a word you're saying!
0/0
 Reply   Quote More 

Reply to All    
 

1–3

Rate my interest:

Adjust text size : Smaller 10 Larger

Beehive Forum 1.5.2 |  FAQ |  Docs |  Support |  Donate! ©2002 - 2024 Project Beehive Forum

Forum Stats