Jump to content

[SOLVED] Select error


june_c21

Recommended Posts

hi, can someone tell me what is the error of this code. i try it many times but in the drop down menu there is no data that i select from the database .

 

Select a User:
<select name="staff no " onChange="showUser(this.value)">

<?php

$host     = 'localhost';
$user     = 'root';
$password = '';
$dbase    = 'claim';

$dblink = mysql_connect($host, $user, $password);
mysql_select_db($dbase, $dblink);

$query = "SELECT staff_no
FROM user
ORDER BY staff_no
";

$result = mysql_query($query);
while($myrow = mysql_fetch_row($result)) {

echo "<option value=\"" . $myrow[0] . "\">" ;

echo "</option>";}

Link to comment
Share on other sites

Try this:

 

Select a User:
<select name="staff no " onChange="showUser(this.value)">

<?php

$host     = 'localhost';
$user     = 'root';
$password = '';
$dbase    = 'claim';

$dblink = mysql_connect($host, $user, $password);
mysql_select_db($dbase, $dblink);

$query = "SELECT staff_no
FROM user
ORDER BY staff_no
";

$result = mysql_query($query);
while($row = mysql_fetch_array($result)) {

echo "<option value=\"$row\">$row</option>";
     }

 

When using a while loop be sure to try to keep your HTML as clean looking as possible to avoid trouble reading the source when debugging.

Link to comment
Share on other sites

When a while loop outputs information, its outputs one right after another.

 

So when you use HTML like

<option value="1">1</option>

and you have more than one entry,

the browser will display

<option value="1">1</option><option value="1">1</option><option value="1">1</option>

 

You were using the while loop like this:

while($myrow = mysql_fetch_row($result)) {

echo "<option value=\"" . $myrow[0] . "\">" ;

echo "</option>";}

 

Therefore, when you view the source on your webpage, you would see this:

<option value="1">

</option>

<option value="1">

</option>

<option value="1">

</option>

 

its just looks messy and its difficult to find mistakes... that is just an example... If you don't follow good coding techniques, then you are likely to make mistakes and also have issue debuging your code.

 

Its also a good idea to comment in your code as much as you can... although it may seem redundant, it will help you find what does what and what goes with what eg:

 

if ($submit == "1") {      //checks if $submit is equal to 1

      if ($value == "1") {      // checks if $value is equal to 1

        $blah = "5";              // sets blah equal to 5

      }                        // ends if $value is equal to 1

  }                        //ends if $submit is equal to 1

 

this is obviously a very easy to follow set of if's but if you have a lot of code in those its very easy to forget }'s and end up with a non working code

 

:)

Link to comment
Share on other sites

oops! This:

 

$result = mysql_query($query);
while($row = mysql_fetch_array($result)) {

echo "<option value=\"$row\">$row</option>";
     }

 

is suppose to be:

 

 

$result = mysql_query($query);
while($row = mysql_fetch_array($result)) {
$staffno = $row['staff_no'];
echo "<option value=\"$staffno\">$staffno</option>";
     }

Link to comment
Share on other sites

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.