Jump to content

Trying to make a drop down menu


adamjones

Recommended Posts

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]

Link to comment
https://forums.phpfreaks.com/topic/188706-trying-to-make-a-drop-down-menu/
Share on other sites

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...

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?

$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?

} $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]';

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>

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.