adamjones Posted January 16, 2010 Share Posted January 16, 2010 Hey, So I have this code, and it's function is to show which domains belong to a certain user; <?php require_once('config.php'); $link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD); if(!$link) { die('Failed to connect to server: ' . mysql_error()); } $db = mysql_select_db(DB_DATABASE); if(!$db) { die("Unable to select database"); } $sql="SELECT domain FROM domains WHERE user=".$_SESSION['user'].""; $result=mysql_query($sql); echo "<select name=\"name\">"; echo "<option size =30 selected>Choose A Domain</option>"; if(mysql_num_rows($sql_result)) { while($row = mysql_fetch_assoc($sql_result)) { echo "<option>$row[domain]</option>"; } } ?> ...But it's just showing a blank dropdown. Any ideas? My database structure is attached. Thanks! [attachment deleted by admin] Quote Link to comment https://forums.phpfreaks.com/topic/188706-trying-to-make-a-drop-down-menu/ Share on other sites More sharing options...
mmarif4u Posted January 16, 2010 Share Posted January 16, 2010 These lines: if(mysql_num_rows($sql_result)) { while($row = mysql_fetch_assoc($sql_result)) should be: if(mysql_num_rows($result)) { while($row = mysql_fetch_assoc($result)) Because you dont have $sql_result... Quote Link to comment https://forums.phpfreaks.com/topic/188706-trying-to-make-a-drop-down-menu/#findComment-996165 Share on other sites More sharing options...
adamjones Posted January 16, 2010 Author Share Posted January 16, 2010 These lines: if(mysql_num_rows($sql_result)) { while($row = mysql_fetch_assoc($sql_result)) should be: if(mysql_num_rows($result)) { while($row = mysql_fetch_assoc($result)) Because you dont have $sql_result... Hi, thanks. So I've change my code to; <?php require_once('config.php'); $link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD); if(!$link) { die('Failed to connect to server: ' . mysql_error()); } $db = mysql_select_db(DB_DATABASE); if(!$db) { die("Unable to select database"); } $sql="SELECT domain FROM domains WHERE user=".$_SESSION['user'].""; $result=mysql_query($sql); echo "<select name=\"name\">"; echo "<option size =30 selected>Select</option>"; if(mysql_num_rows($result)) { while($row = mysql_fetch_assoc($result)) { echo "<option>$row[domain]</option>"; } } ?> But it's still not working? Quote Link to comment https://forums.phpfreaks.com/topic/188706-trying-to-make-a-drop-down-menu/#findComment-996170 Share on other sites More sharing options...
mmarif4u Posted January 16, 2010 Share Posted January 16, 2010 $result=mysql_query($sql) or die (mysql_error()); echo $sql; Quote Link to comment https://forums.phpfreaks.com/topic/188706-trying-to-make-a-drop-down-menu/#findComment-996173 Share on other sites More sharing options...
adamjones Posted January 16, 2010 Author Share Posted January 16, 2010 $result=mysql_query($sql) or die (mysql_error()); echo $sql; "You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '@live.com' at line 1" Hmm, usernames are e-mail addresses, I'm guessing that's a problem somehow? Quote Link to comment https://forums.phpfreaks.com/topic/188706-trying-to-make-a-drop-down-menu/#findComment-996181 Share on other sites More sharing options...
wildteen88 Posted January 16, 2010 Share Posted January 16, 2010 You need to wrap field values within quotes $sql="SELECT domain FROM domains WHERE user='".$_SESSION['user']."'"; Quote Link to comment https://forums.phpfreaks.com/topic/188706-trying-to-make-a-drop-down-menu/#findComment-996183 Share on other sites More sharing options...
jskywalker Posted January 16, 2010 Share Posted January 16, 2010 } $sql="SELECT domain FROM domains WHERE user=".$_SESSION['user'].""; change it to : } $sql="SELECT domain FROM domains WHERE user='".$_SESSION['user']."'"; (mark the extra single quotes that are added, it will give (i.e.): SELECT domain FROM domains WHERE user='[email protected]'; Quote Link to comment https://forums.phpfreaks.com/topic/188706-trying-to-make-a-drop-down-menu/#findComment-996184 Share on other sites More sharing options...
greatstar00 Posted January 16, 2010 Share Posted January 16, 2010 $sql="SELECT domain FROM domains WHERE user='".$_SESSION['user']."'"; i added single quote to surround $_SESSION['user'] Quote Link to comment https://forums.phpfreaks.com/topic/188706-trying-to-make-a-drop-down-menu/#findComment-996185 Share on other sites More sharing options...
mmarif4u Posted January 16, 2010 Share Posted January 16, 2010 Try this: $sql="SELECT domain FROM domains WHERE user='$_SESSION[user]'"; If that does not solve it, then try it this way: //$result=mysql_query($sql) or die (mysql_error()); echo $sql; Check what echo output. Quote Link to comment https://forums.phpfreaks.com/topic/188706-trying-to-make-a-drop-down-menu/#findComment-996186 Share on other sites More sharing options...
adamjones Posted January 16, 2010 Author Share Posted January 16, 2010 Thanks for all your help guys, it's working fine now Quote Link to comment https://forums.phpfreaks.com/topic/188706-trying-to-make-a-drop-down-menu/#findComment-996201 Share on other sites More sharing options...
adamjones Posted January 16, 2010 Author Share Posted January 16, 2010 Just another quick question; I added a button, and when pressed, it links to 'check.php'. How would I echo the option that was selected on the check.php page?? <form class="table" action="check.php" method="post"> <div class="inner-form"> <div class="msg msg-info"> <p>Please select which domain you wish to edit.</p> </div> <?php require_once('config.php'); $link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD); if(!$link) { die('Failed to connect to server: ' . mysql_error()); } $db = mysql_select_db(DB_DATABASE); if(!$db) { die("Unable to select database"); } $sql="SELECT domain FROM domains WHERE user='$_SESSION[user]'"; $result=mysql_query($sql) or die (mysql_error()); echo "<select name=\"name\">"; echo "<option size =30 selected>Select a domain</option>"; if(mysql_num_rows($result)) { while($row = mysql_fetch_assoc($result)) { echo "<option>$row[domain]</option>"; } } ?></div> </select> <input type="submit" VALUE="Edit"> </form> Quote Link to comment https://forums.phpfreaks.com/topic/188706-trying-to-make-a-drop-down-menu/#findComment-996215 Share on other sites More sharing options...
wildteen88 Posted January 16, 2010 Share Posted January 16, 2010 In check.php you'd use $_POST['name'] Quote Link to comment https://forums.phpfreaks.com/topic/188706-trying-to-make-a-drop-down-menu/#findComment-996216 Share on other sites More sharing options...
adamjones Posted January 16, 2010 Author Share Posted January 16, 2010 In check.php you'd use $_POST['name'] Fantastic, thanks! Quote Link to comment https://forums.phpfreaks.com/topic/188706-trying-to-make-a-drop-down-menu/#findComment-996217 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.