june_c21 Posted May 4, 2008 Share Posted May 4, 2008 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>";} Quote Link to comment Share on other sites More sharing options...
tronicsmasta Posted May 4, 2008 Share Posted May 4, 2008 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. Quote Link to comment Share on other sites More sharing options...
june_c21 Posted May 4, 2008 Author Share Posted May 4, 2008 what do you mean? sorry... i don't understand. Quote Link to comment Share on other sites More sharing options...
tronicsmasta Posted May 4, 2008 Share Posted May 4, 2008 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 Quote Link to comment Share on other sites More sharing options...
tronicsmasta Posted May 4, 2008 Share Posted May 4, 2008 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>"; } Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.