Jump to content

inputing data into drop down


rvinikof

Recommended Posts

I'm trying to input data from a field called subject to insert the different subjects into a drop down on a form to be selected.

 

When I do this:

 

$sqlsubject = "SELECT DISTINCT subject FROM courseinfo";

echo '<b>Subject:</b><select name = "subject"><option value = "subject">$sqlsubject</option></select>';

 

I just get the drop down saying $sqlsubject, instead of the list of subjects from the database.

 

Link to comment
https://forums.phpfreaks.com/topic/60248-inputing-data-into-drop-down/
Share on other sites

You were right obviously.

 

But now I've done this:

 

$sqlsubject = "SELECT DISTINCT subject FROM courseinfo";

$subjectquery = mysql_query($sqlsubject) or die('Query failed: ' . mysql_error());

 

while ($subjectrow = mysql_fetch_row($subjectquery)) {

echo '<b>Subject:</b><select name = "subject"><option value = "subject">';

foreach ($subjectrow as $subject)

{ echo $subjectrow; }

echo "</option></select>"; }

 

and the drop down just says Array. What does that mean?

try this

 

$sqlsubject = "SELECT DISTINCT subject FROM courseinfo";
$subjectquery = mysql_query($sqlsubject) or die('Query failed: ' . mysql_error());
//start the select box off
echo 'Subject:<select name = "subject">';
//create the select box items from the query
while($row = mysql_fetch_assoc($subjectquery))
{
$subject = $row['subject'];
echo "<option value = '$subject'>$subject</option>";
}
//close the select box
echo "</select>";

....and the drop down just says Array. What does that mean?

 

Paul's got the right solution.  mysql_fetch_row() returns an array, so instead of echoing the array, you need to echo the element(s) of the array you really want.  mysq_fetch_row() returns a numerically indexed array; mysql_fetch_assoc() returns an array indexed by the column names.

 

Oh, and in a foreach($foo as $bar) {...} loop, you refer to $bar, not $foo within the loop.

Thanks. One last question... once I have the data in the drop down, is there a way I can put a blank field before all the data (at index 0) so that it will be blank instead of data from the table appearing? Can I do this without having to add null values or something like that in my table?

Well, if you add a row to your table and don't have any value to put in a particular field, then it will have to be NULL or 0 or the empty string ("").  In the code that inserts data you should also have some data validation (check that numeric data really is numeric, VARCHAR(255) is really =< 255 chars, etc), and you can set blank fields in that code.

as wildbug said,

 

$sqlsubject = "SELECT DISTINCT subject FROM courseinfo";
$subjectquery = mysql_query($sqlsubject) or die('Query failed: ' . mysql_error());
//start the select box off
echo 'Subject:<select name = "subject">';
//put in the first select part saying please select
echo "<option value = ''>Please Select</option>";
//create the select box items from the query
while($row = mysql_fetch_assoc($subjectquery))
{
$subject = $row['subject'];
echo "<option value = '$subject'>$subject</option>";
}
//close the select box
echo "</select>";

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.