Forum Moderators: coopster

Message Too Old, No Replies

Using a Combobox as part of a query

         

Loque

12:58 pm on Aug 22, 2011 (gmt 0)

10+ Year Member



Hi all,

I am a bit of a novice to php, but I've ended up building a database to report answers to a quiz that I have built. The problem is that I need to be able to select which person took which quiz, and then to be able to display their results.

The only real way I have thought of doing this is to use a dropdown box to select which people took the quiz, then run it through a query to output the results.

In essence I need a dropdown box that's connected to my database, and then I need to be able to store the result as a variable so that I can call on it in the query. How would I do this?

I have looked around Google and come up with a way of populating the combobox from my server, my code is:

<?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>


Unfortunately I have not be able to get this to work, can anybody point out the errors and possibly elaborate on how I could get the variable out?

Thanks for your help.

rocknbil

4:05 pm on Aug 22, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Welcome aboard Loque, "as is" I think all you need is

<?php echo $options; ?>

Short tags are deprecated.

A slightly improved version, though,


<?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>"; }


Notes:
- As mentioned, shorttags are deprecated, use <?php
- Scalar $variables will interpolate in double quotes, save debugging time by removing concatenations where you don't need them.
- Output validation is important, always quote attributes (value=\"0\") and note the usage of label
- Error trapping is your friend, note the if/else