Jump to content

Use multiple values from mysql database for dropdown menu


frshjb373

Recommended Posts

Basically I'd like to echo "name1" in the browser, which is working fine! But I'd also like to bring over the associated "name2" field value without the user being able to see this value until the form has been processed. I am using "field_Name" to process both these variables at once on the following page so I'd also like to separate these variables as well.

 

I'm sure it's an extremely simple fix, but I can't figure it out.  Any help is much appreciated!

 

<?php
mysql_connect($DB_host,$DB_user,$DB_pass) or die("Connection Failed");
mysql_select_db ($DB_name) or die("Connection Failed");
$query = "SELECT * FROM table";
$result = mysql_query($query);
?>
<select name="field_Name">
<option value="">Select one...</option>
<?php
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
?>
<?php $row['name']; ?>
<option value="<?php echo $row['name1'];?>  <?php $row['name2'];?>"> <?php echo $row['name1'];?> </option>
<?php
}
?>
</select>

Link to comment
Share on other sites

Ok...I think I'm getting there, but I can't figure quite out how to associate "field_Name" variable on the next page. 

 

I think basically what I'm trying to say in my code is..."If "field_Phone" is equal to any value in "$row['name1']" then echo the associated "$row['name2"] value."

 

I'd like to think I'm close, but I haven't nailed it yet.  The code I came up with is below. Am I even close?

 

Thanks again for all the help!

 

<?php
mysql_connect($DB_host,$DB_user,$DB_pass) or die("Connection Failed");
mysql_select_db ($DB_name) or die("Connection Failed");
$query = "SELECT * FROM cell_brands";
$result = mysql_query($query);
?>

<?php
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
if ($field_Phone =  ($row['name1'])) {
	echo $row['name2'];
}
}

?>

Link to comment
Share on other sites

Awesome!  Think I figured it out.  Here's what I came up with.  Honestly, not sure if I even need "LIMIT 0,1" at the end of the query.

 

<?php
mysql_connect($DB_host,$DB_user,$DB_pass) or die("Connection Failed");
mysql_select_db ($DB_name) or die("Connection Failed");
$query = "SELECT name2 FROM cell_brands WHERE name = '{$field_Phone}' LIMIT 0,1";


$result = mysql_query($query);
?>

<?php
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
	$price = $row['name2'];
}

?>

Link to comment
Share on other sites

It can be nice to have, but not necessary, no.

However, this following part isn't necessary at all:

?>

<?php
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
	$price = $row['name2'];
}

 

First of all there's no need to close and then re-open the PHP parsing like that, you're only wasting resources. Secondly, you don't need to loop the result set, as you have only one row anyway. mysql_result () should be more than adequate for this particular need.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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