Full Version: PHP Help
From: Slaves & Bulldozers (SHIELDSIT) [#1]
5 Mar 15:31
To: ALL
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) [#2]
5 Mar 16:23
To: Slaves & Bulldozers (SHIELDSIT) [#1] 5 Mar 16:28
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. :)
From: Slaves & Bulldozers (SHIELDSIT) [#3]
5 Mar 16:31
To: Peter (BOUGHTONP) [#2] 5 Mar 17:17