topflight Posted January 16, 2009 Share Posted January 16, 2009 I am trying to create a simple option form and have it loop at the name in the database, so that all the names will show in the option menu. This is for a form I am working on here is my code <?php session_start(); if(isset($_SESSION['SESS_LOGGEDIN']) == FALSE){ echo "You must be logged in"; } else { include 'db.php'; $login = $_SESSION['login']; $lname = $_SESSION['SESS_LNAME']; $fname = $_SESSION['SESS_FNAME']; $connect = mysql_connect($db_host,$db_username,$db_password) or die("MySQL Said:".mysql_error()); $database = mysql_select_db($db_database,$connect) or die("MySQl Said:".mysql_error()); $checkl = mysql_query("SELECT * FROM `pilots` WHERE lname='$lname' AND fname='$fname'") or die("MySQL Said:".mysql_error()); $edata=mysql_fetch_assoc($checkl); $link = mysql_query("SELECT * FROM `pilots` WHERE status >'0' ORDER BY login ASC") or die("MySQL Said:".mysql_error()); if($bm=='1'){ ?> <form method="post" action="?page=addstaff_phrase" name="adds"/> <select name="pilots"> <?php while($data = mysql_fetch_assoc($link))?>{<option><?php echo"$fname";?> <? echo"$lname";?> - <?php echo"$pid"; ?> </option> </select> </form> <?php } } $pid = $data['login']; $fname = $data['fname']; $lname = $data['lname']; ?> I know this sounds simple but I am confuse on how to do it please help thanks. Quote Link to comment Share on other sites More sharing options...
flyhoney Posted January 16, 2009 Share Posted January 16, 2009 <?php $query = "SELECT * FROM `pilots` WHERE lname='$lname' AND fname='$fname'"; $result = mysql_query($query) or die("MySQL Said:".mysql_error()); $pilots = array(); while ($pilot = mysql_fetch_assoc($result)) { $pilots[$pilot['id']] = $pilot['lname'] . ', ' . $pilot['fname']; } ?> <select name="pilot"> <?php foreach ($pilots as $id => $name): ?> <option value="<?php echo $id ?>"><?php echo $name ?></option> <?php endforeach ?> </select> Quote Link to comment Share on other sites More sharing options...
topflight Posted January 16, 2009 Author Share Posted January 16, 2009 I have changed my code to this: <?php session_start(); if(isset($_SESSION['SESS_LOGGEDIN']) == FALSE){ echo "You must be logged in"; } else { include 'db.php'; $login = $_SESSION['login']; $lname = $_SESSION['SESS_LNAME']; $fname = $_SESSION['SESS_FNAME']; $connect = mysql_connect($db_host,$db_username,$db_password) or die("MySQL Said:".mysql_error()); $database = mysql_select_db($db_database,$connect) or die("MySQl Said:".mysql_error()); $checkl = mysql_query("SELECT * FROM `pilots` WHERE lname='$lname' AND fname='$fname'") or die("MySQL Said:".mysql_error()); $edata=mysql_fetch_assoc($checkl); $result = mysql_query("SELECT * FROM `pilots` ORDER BY login ASC") or die("MySQL Said:".mysql_error()); $bm = $edata["bm"]; if($bm=='1'){ $pilots = array(); while ($pilot = mysql_fetch_assoc($result)) { $pilots[$pilot['id']] = $pilot['lname'] . ', ' . $pilot['fname']; } ?> <select name="pilot"> <?php foreach ($pilots as $id => $name): ?> <option value="<?php echo $id ?>"><?php echo $name ?></option> <?php endforeach ?> </select> <? } else {echo'<font color="#FF0000"> Insufficient Rights </font>';}?> <? } ?> And it is still on displaying one name in the drop down menu. Quote Link to comment Share on other sites More sharing options...
flyhoney Posted January 16, 2009 Share Posted January 16, 2009 Are you sure you are getting more than one row from the query? Add this and make sure <?php $result = mysql_query("SELECT * FROM `pilots` ORDER BY login ASC") or die("MySQL Said:".mysql_error()); die("num results: " . mysql_num_rows($result)); Quote Link to comment Share on other sites More sharing options...
topflight Posted January 16, 2009 Author Share Posted January 16, 2009 Now my code look like this: <?php session_start(); if(isset($_SESSION['SESS_LOGGEDIN']) == FALSE){ echo "You must be logged in"; } else { include 'db.php'; $login = $_SESSION['login']; $lname = $_SESSION['SESS_LNAME']; $fname = $_SESSION['SESS_FNAME']; $connect = mysql_connect($db_host,$db_username,$db_password) or die("MySQL Said:".mysql_error()); $database = mysql_select_db($db_database,$connect) or die("MySQl Said:".mysql_error()); $checkl = mysql_query("SELECT * FROM `pilots` WHERE lname='$lname' AND fname='$fname'") or die("MySQL Said:".mysql_error()); $edata=mysql_fetch_assoc($checkl); $result = mysql_query("SELECT * FROM `pilots` ORDER BY login ASC") or die("MySQL Said:".mysql_error()); die("num results: " . mysql_num_rows($result)); $bm = $edata["bm"]; if($bm=='1'){ $pilots = array(); while ($pilot = mysql_fetch_assoc($result)) { $pilots[$pilot['id']] = $pilot['lname'] . ', ' . $pilot['fname']; } ?> <select name="pilot"> <?php foreach ($pilots as $id => $name): ?> <option value="<?php echo $id ?>"><?php echo $name ?></option> <?php endforeach ?> </select> <? } else {echo'<font color="#FF0000"> Insufficient Pilot Privileges </font>';}?> <? } ?> And on the page it says num rows three. I am trying to display all the member in the MySQL Database in an option menu. Quote Link to comment Share on other sites More sharing options...
topflight Posted January 16, 2009 Author Share Posted January 16, 2009 Also three is right becuase I currently have three members in the database. Quote Link to comment Share on other sites More sharing options...
flyhoney Posted January 16, 2009 Share Posted January 16, 2009 I dont know your database schema. What is the primary key for your pilots table? You need to use that in the line: $pilots[$pilot['PRIMARY_KEY']] = $pilot['lname'] . ', ' . $pilot['fname']; Quote Link to comment Share on other sites More sharing options...
topflight Posted January 16, 2009 Author Share Posted January 16, 2009 id I have my primay key set up as ID so what do I do now? Quote Link to comment Share on other sites More sharing options...
topflight Posted January 16, 2009 Author Share Posted January 16, 2009 any other help. Quote Link to comment Share on other sites More sharing options...
topflight Posted January 17, 2009 Author Share Posted January 17, 2009 ? Quote Link to comment Share on other sites More sharing options...
topflight Posted January 17, 2009 Author Share Posted January 17, 2009 Any Help would be appreicated Quote Link to comment Share on other sites More sharing options...
timmah1 Posted January 17, 2009 Share Posted January 17, 2009 Wouldn't this do it <select name="pilot"> <?php foreach ($pilots as $id => $name){ ?> <option value="<?php echo $id ?>"><?php echo $name ?></option> <?php } ?> </select> 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.