Jump to content

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.