shalli Posted March 20, 2010 Share Posted March 20, 2010 Hi there I am trying to pre-populate two drop down menus from a mysql database but I keep getting a blank drop down menu for the franchise name. Please can some one help?!?!?!?!? I am pasting my code below. Any help will be greatly appreciated thanks <?php $host="localhost"; // Host name $username="xxxx"; // Mysql username $password="xxxx"; // Mysql password $db_name="xxxx"; // Database name // Connect to server and select databse. mysql_connect("$host", "$username", "$password")or die("cannot connect"); mysql_select_db("$db_name")or die("cannot select DB"); //See if there is a table that you require... $tbl_name = 'franchises'; if( mysql_num_rows( mysql_query("SHOW TABLES LIKE '".$tbl_name."'"))) { //print("Found Table. Ready to proceed!<br>"); }else{ print("Table was not found, I will create it now.<br>"); } //select franchise so ID is auto populated $franchiseSQL = "SELECT id, name FROM franchises"; $result = mysql_query($franchiseSQL); //while($row = mysql_fetch_array($result)) //{ //echo "Franchise Id :{$row['id']} <br>" ; //echo "Franchise Name :{$row['name']} <br>" ; //} ?> <table width="300" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC"> <tr> <form name="form1" method="post" action="checklogin.php"> <td> <table width="100%" border="0" cellpadding="3" cellspacing="1" bgcolor="#FFFFFF"> <tr> <td colspan="3"><strong>Member Login </strong></td> </tr> <tr> <td width="78">Username</td> <td width="6">:</td> <td width="294"><input name="myusername" type="text" id="myusername"></td> </tr> <tr> <td>Password</td> <td>:</td> <td><input name="mypassword" type="text" id="mypassword"></td> </tr> <tr> <td>Name</td> <td>:</td> <td><input name="myname" type="text" id="myname"></td> </tr> <tr> <td>Website</td> <td>:</td> <td><input name="mywebsite" type="text" id="mywebsite"></td> </tr> <tr> <td>HeaderBGColour</td> <td>:</td> <td><input name="headerBGColour" type="text" id="headerBGColour"></td> </tr> <tr> <td>Logo</td> <td>:</td> <td><input name="logolocation" type="text" id="logolocation"></td> </tr> <tr> <tr> <td>Franchise ID</td> <select name="id" id="id" /> <? while($row = mysql_fetch_array($result)) { ?> echo "<option value=\"".$row['id']."\">" .$row['id']."</option>"; //<option value="<? echo $row['id']; ?>"><?php echo $row['id']; ?></option> <? } ?> </select> <br /> </tr> </tr> <tr> <tr> <td>Franchise Name</td> <select name="name" id="name" /> <? while ($row = mysql_fetch_array($result)) { ?> echo "<option value=\"".$row['name']."\">" .$row['name']."</option>"; //<option value="<? echo $row['name']; ?>"><?php echo $row['name']; ?></option> <? } ?> </select> <br /> </tr> <tr> <td> </td> <td> </td> <td><input type="submit" name="Submit" value="Login"></td> </tr> </table> </td> </form> </tr> </table> Quote Link to comment https://forums.phpfreaks.com/topic/195965-help-needed-with-populating-a-drop-down-menu-from-mysql-database/ Share on other sites More sharing options...
teamatomic Posted March 21, 2010 Share Posted March 21, 2010 In two places in the form you break php then try to echo <? while($row = mysql_fetch_array($result)) { ?> echo "<option value=\"".$row['id']."\">" .$row['id']."</option>"; //<option value="<? echo $row['id']; ?>"><?php echo $row['id']; ?></option> <? } ?> HTH Teamatomic Quote Link to comment https://forums.phpfreaks.com/topic/195965-help-needed-with-populating-a-drop-down-menu-from-mysql-database/#findComment-1029351 Share on other sites More sharing options...
TapeGun007 Posted March 21, 2010 Share Posted March 21, 2010 <? while ($row = mysql_fetch_array($result)) { ?> echo "<option value=\"".$row['name']."\">" .$row['name']."</option>"; //<option value="<? echo $row['name']; ?>"><?php echo $row['name']; ?></option> <? } ?> Should be: (take spaces out between single quotes and double quotes, I put them there only so you could see better) <? while ($row = mysql_fetch_array($result)) { echo "<option value=' ".$row['name']." '>".$row['name']."</option>"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/195965-help-needed-with-populating-a-drop-down-menu-from-mysql-database/#findComment-1029383 Share on other sites More sharing options...
shalli Posted March 21, 2010 Author Share Posted March 21, 2010 Hi Thanks teamatomic and TapeGun007 for your responses to my php problem but it hasnt made a difference. I am still getting a blank drop down menu?!?!?!?!?! any ideas Thanks for your time greatly appreciated cheers shalli Quote Link to comment https://forums.phpfreaks.com/topic/195965-help-needed-with-populating-a-drop-down-menu-from-mysql-database/#findComment-1029768 Share on other sites More sharing options...
PFMaBiSmAd Posted March 21, 2010 Share Posted March 21, 2010 You are attempting to reuse the same result set from your query. You must reset the result pointer before the second while(){} loop. See the mysql_data_seek function. Quote Link to comment https://forums.phpfreaks.com/topic/195965-help-needed-with-populating-a-drop-down-menu-from-mysql-database/#findComment-1029769 Share on other sites More sharing options...
shalli Posted March 21, 2010 Author Share Posted March 21, 2010 HI PFMaBiSmAd Thanks for your response I have now solved the problem thanks for your help everyone thanks shalli Quote Link to comment https://forums.phpfreaks.com/topic/195965-help-needed-with-populating-a-drop-down-menu-from-mysql-database/#findComment-1029779 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.