Jump to content

id value from drop down


diasansley

Recommended Posts

hi

how do i get the id value from the dropdown selection from the database

 

Here is my code. i need the id to store it another table to refer it in my application. its like a categories and subcategories table.

<html>
<form id="name" action="<?php $_POST['SERVER_SELF'] ?>" method="POST" >
<input type="text" name="name1">  <br>
  <select name="select">
  <?php mysql_connect('localhost','root','') or                                                              
                                  die('Could not connect to mysql ' . mysql_error()); // error message
                                                                 
                                 mysql_select_db("dbtest") or die(mysql_error()); 
                                 $query = "select name,id from main"; 
                                   $result = mysql_query($query);
                                    if ($query) 
                                    {  
                             while ($row = mysql_fetch_array($result))
                                  { $strB=$row['name'];
                                   ?>
                                    <option value="<?php $stra ?>"><?php echo $strB ?></option> <br>
                                      
                                   <?php
                                  
                                   }
                                   }
       ?>
</select>
<input type="text" name="name2">     <br>
<input type="submit" name="submit1">
    </form>
     <?php if(isset($_POST['submit']))
     { //$name=$_POST['val'];
        $strB=$stra;
        mysql_connect('localhost','root','') or                                                              
                                  die('Could not connect to mysql ' . mysql_error()); // error message
                                                                 
                                 mysql_select_db("dbtest") or die(mysql_error());
        $query = "insert into sub (sub) values('$strB')";
            $result = mysql_query($query);
             if ($query) 
                                    {                       
                                    
                                    
                            echo "query executed";         
       }
                                     }
       ?>
   
</html>

thanks

Link to comment
Share on other sites

OK, If I understand your question the first thing you need to do is replace this

while ($row = mysql_fetch_array($result)) { 
    $strB=$row['name'];
?>
    <option value="<?php $stra ?>"><?php echo $strB ?></option> <br>
                                      
<?php
                                  
}

with this

while ($row = mysql_fetch_assoc($result)) { 
   $stra = $row['id'];
$strB=$row['name'];
?>
    <option value="<?php echo $stra; ?>"><?php echo $strB; ?></option>
                                      
<?php
                                  
}

 

the mysql_fetch_assoc returns an associative array with the column names so you can reference them  $row['id'] and $row['name']

then in the value attribute you need to echo the id, and after each <option> tag you don't need to add a <br> tag.

 

 

as for the processing section you need to retrieve the id trough the $_POST['select'] to get the value selected instead of what you are using now $strB=$stra.

 

And also I would move the processing section to the top of the page.

 

Hope this Helps.

Link to comment
Share on other sites

Is this similar to what you are trying to do?

 

<html>
<?php

	mysql_connect('localhost','root','') or die('Could not connect to mysql ' . mysql_error());
        mysql_select_db("dbtest") or die(mysql_error());

	if(isset($_POST['submit'])) { //$name=$_POST['val'];
		$id = $_POST['select'];

		$query = "INSERT INTO `sub` (`sub`) values('" . $id . "')";
            $result = mysql_query($query);
             
		if($query) {                       
			echo "query executed";         
		}
	} else {
    ?>

<form id="name" action="<?php $_POST['SERVER_SELF'] ?>" method="POST" >
	<input type="text" name="name1"><br />
	<select name="select" id="select">
	<?php
		$query = "SELECT `name`, `id` FROM `main`"; 
            $result = mysql_query($query);
            
		if($query) {  
                while($row = mysql_fetch_array($result)) {
				$options .= '<option value="' . $row['id'] . '">' . $row['name'] . '</option>';
        
			}
		}

		echo $options;
	?>

	</select>
	<input type="text" name="name2"><br />
	<input type="submit" name="submit1">
</form>
<?php } ?>
</html>

 

James.

Link to comment
Share on other sites

what i am tryin to do is

i have two tables one with categories and one with subcategories.

i am giving the users a drop down from the categories table before they insert into the subcategories table. Now incase i want to search sub-categories based on categories i need the ID values from the category table to be in the subcategory.

The above method was one of the way!! any other more efficient then even better

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.