Forum Moderators: coopster
<?php
// connect to the db server
$link = mysql_connect('localhost', 'root', 'root');
if (!$link) {
die('Could not connect to database server. Died with this error: ' . mysql_error());
} else {
echo('Successfully connected to the database.<br />');
}
// select the db
mysql_select_db('cp2db');
// drop down box info
$sql="SELECT id, tblLastName, FROM tblCore";
$result=mysql_query($sql);
$options="";
while ($row=mysql_fetch_array($result)) {
$id=$row["id"];
$lastname=$row["tblLastName"];
$options.="<OPTION VALUE=\"$id\">".$lastname;
}
?>
Helloworld!
<SELECT NAME=thing>
<OPTION VALUE=0>Choose
<?=$options?>
</SELECT>
<?php
// May not need it here, but becomes handy and good habit.
// If there is a selected item, set a variable for it.
$selected = isset($_POST['id'] and is_numeric($_POST['id']) and ($_POST['id'] > 0))?$_POST['id']:null;
$options=null; // To squelch concatenation errors, = "" is fine too
// drop down box info
$sql="SELECT id, tblLastName, FROM tblCore";
$result=mysql_query($sql) or die("Could not query database for select list");
while ($row=mysql_fetch_array($result)) {
$id=$row["id"];
$lastname=$row["tblLastName"];
$options.="<option value=\"$id\"";
// HTML 4/5, ' selected="selected" ' for XHTML
if ($id==$selected) { $options .= ' selected'; }
$options .= ">$lastname</option>\n";
}
if ($options) {
echo "<p>Helloworld!</p>
<p>label for=\"thing\">Thing:</label> <select name=\"thing\" id=\"thing\">
<option value=\"0\">Choose</option>
$options
</select> </p>
";
}
else { echo "<p>No options to show.</p>"; }