how to insert value from drop down menu list into database?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • enrekan2011 lan
    New Member
    • Dec 2011
    • 3

    how to insert value from drop down menu list into database?

    haii...

    im new here and in php coding as well..

    sorry if im ask stupid question here..

    i just need some help from you all because i have try find at google but i still didn't found the solution.


    here is my question...

    actually im developing a website for my company association for the election of committee members. in one part the user must select the username to vote as a chairman and etc. and the value ( name have been selected) i want insert into database. is there any specific coding php need to use toinsert the menu list name into database?



    here is my coding ( in this code im take the data from my database to view all the list name - thats working good. but when i need to select the name and insert it...it dosen't working...and the database still empty...please help me...)



    Code:
    <form name="form1" method="post" action="">
      <table width="80%" border="1" align="center">
        <tr>
          <td colspan="5">my election.</td>
        </tr>
        <tr>
          <td width="2%">&nbsp;</td>
          <td width="3%">&nbsp;</td>
          <td colspan="3">&nbsp;</td>
        </tr>
        <tr>
          <td>&nbsp;</td>
          <td colspan="4">Who you want to vote as a ......</td>
        </tr>
        <tr>
          <td>&nbsp;</td>
          <td>&nbsp;</td>
          <td colspan="3">&nbsp;</td>
        </tr>
        <tr>
          <td height="26">&nbsp;</td>
          <td>&nbsp;</td>
          <td>Chairman</td>
          <td width="24%">
    <label>
    <select name="select1" id="select" >
    <?
    $sql = "select UserName from advlogin order by UserName ASC";
    $rs = mysql_query($sql) or die (mysql_error());
    
    if ($rs)
    {
    	while ($rw = mysql_fetch_object($rs))
    	{
    		$UserName=$rw->UserName;
    ?>
    <option><? echo $UserName; ?> </option>
        
    <?
    
    	}
    	
    }
    
    ?>
    </select> 
    </label>
          </td>
          <td width="39%">&nbsp;</td>
        </tr>
        <tr>
          <td height="26">&nbsp;</td>
          <td>&nbsp;</td>
          <td>Deputy Chairman</td>
          <td>
          
    <label>
    <select name="select2" id="select" >
    <?
    $sql = "select UserName from advlogin order by UserName ASC";
    $rs = mysql_query($sql) or die (mysql_error());
    
    if ($rs)
    {
    	while ($rw = mysql_fetch_object($rs))
    	{
    		$UserName=$rw->UserName;
    ?>
    <option><? echo $UserName; ?> </option>
        
    <?
    
    	}
    	
    }
    
    ?>
    </select> 
    </label>
          
          
          
          
          
          
          </td>
          <td>&nbsp;</td>
        </tr>
        <tr>
          <td height="26">&nbsp;</td>
          <td>&nbsp;</td>
          <td>Secretary</td>
          <td>
    <label>
    <select name="select3" id="select" >
    <?
    $sql = "select UserName from advlogin order by UserName ASC";
    $rs = mysql_query($sql) or die (mysql_error());
    
    if ($rs)
    {
    	while ($rw = mysql_fetch_object($rs))
    	{
    		$UserName=$rw->UserName;
    ?>
    <option><? echo $UserName; ?> </option>
        
    <?
    
    	}
    	
    }
    
    ?>
    </select> 
    </label>
          </td>
          <td>&nbsp;</td>
        </tr>
        <tr>
          <td>&nbsp;</td>
          <td>&nbsp;</td>
          <td>Treasurer</td>
          <td>
    <label>
    <select name="select4" id="select" >
    <?
    $sql = "select UserName from advlogin order by UserName ASC";
    $rs = mysql_query($sql) or die (mysql_error());
    
    if ($rs)
    {
    	while ($rw = mysql_fetch_object($rs))
    	{
    		$UserName=$rw->UserName;
    ?>
    <option><? echo $UserName; ?> </option>
        
    <?
    
    	}
    	
    }
    
    ?>
    </select> 
    </label>      
          
          
          </td>
          <td>&nbsp;</td>
        </tr>
        <tr>
          <td>&nbsp;</td>
          <td>&nbsp;</td>
          <td>Commitee Members</td>
          <td>
    <label>
    <select name="select5" id="select" >
    <?
    $sql = "select UserName from advlogin order by UserName ASC";
    $rs = mysql_query($sql) or die (mysql_error());
    
    if ($rs)
    {
    	while ($rw = mysql_fetch_object($rs))
    	{
    		$UserName=$rw->UserName;
    ?>
    <option selected><? echo $UserName; ?> </option>
        
    <?
    
    	}
    	
    }
    
    ?>
    </select> 
    </label>      
          </td>
          <td>&nbsp;</td>
        </tr>
        <tr>
          <td>&nbsp;</td>
          <td>&nbsp;</td>
          <td>&nbsp;</td>
          <td colspan="2"><label>
            <input type="submit" name="button" id="button" value="Submit">
          </label></td>
        </tr>
        <tr>
          <td height="23">&nbsp;</td>
          <td>&nbsp;</td>
          <td width="16%">&nbsp;</td>
          <td colspan="2">&nbsp;</td>
        </tr>
      </table>
    
    </form>
    Last edited by Niheel; Dec 7 '11, 11:25 PM.
  • zorgi
    Recognized Expert Contributor
    • Mar 2008
    • 431

    #2
    There is no magic. Have a look at this article: Using HTML Forms to pass data to PHP

    To insert data into the database use MySql INSERT statement. You can find loads of interesting examples in on-line php manual. Have a look at this page, just scroll down for examples.

    Comment

    • enrekan2011 lan
      New Member
      • Dec 2011
      • 3

      #3
      i need some help again....

      why my code can't send the value into database after i have selected the list name.?


      please help me again....


      firstly i take the data from mydatabase, afterthat, the data (UserName) i will select again and i want to insert to diffrent database again. but it dosent work...

      here is my fully coding...

      first page ( i rename it election.php)th e code bellow...


      Code:
      <?
      session_start ();
      include "connection.php";
      mysql_connect ("$host", "$username", "$password") or die ("cannot connect");
      mysql_select_db ("$db_name") or die ("cannot select DB");
      ?>
      <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
      <html>
      <head>
      <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
      <title>Untitled Document</title>
      </head>
      
      <body>
      <form name="form1" method="post" action="save.php">
        <table width="80%" border="1" align="center">
          <tr>
            <td colspan="5">my electon</td>
          </tr>
          <tr>
            <td width="2%">&nbsp;</td>
            <td width="3%">&nbsp;</td>
            <td colspan="3">&nbsp;</td>
          </tr>
          <tr>
            <td>&nbsp;</td>
            <td colspan="4">Who you want to vote as a ......</td>
          </tr>
          <tr>
            <td>&nbsp;</td>
            <td>&nbsp;</td>
            <td colspan="3">&nbsp;</td>
          </tr>
          <tr>
            <td height="26">&nbsp;</td>
            <td>&nbsp;</td>
            <td>Chairman</td>
            <td width="24%">
      <label>
      <select name="select1" id="select" >
      <?
      $sql = "select UserName from advlogin order by UserName ASC";
      $rs = mysql_query($sql) or die (mysql_error());
      
      if ($rs)
      {
      	while ($rw = mysql_fetch_object($rs))
      	{
      		$UserName=$rw->UserName;
      ?>
      <option value="<? echo $UserName; ?>"<?php if(!empty($row['option']) and ($row['option'] == "$UserName")) { echo(" selected=\"selected\""); }?>><? echo $UserName; ?> </option>
      <?
      }
      	
      }
      ?>
      </select> 
      </label>
            </td>
            <td width="39%">&nbsp;</td>
          </tr>
          <tr>
            <td>&nbsp;</td>
            <td>&nbsp;</td>
            <td>&nbsp;</td>
            <td colspan="2">
              <input type="submit" name="button" value="Submit">
            </td>
          </tr>
          <tr>
            <td height="23">&nbsp;</td>
            <td>&nbsp;</td>
            <td width="16%">&nbsp;</td>
            <td colspan="2">&nbsp;</td>
          </tr>
        </table>
      
      </form>
      </body>
      </html>

      second page ( save.php)
      Code:
      <?
      include "connection.php";
      
       mysql_connect("$host", "$username", "$password")or die("cannot connect");
       mysql_select_db("$db_name")or die("cannot select DB");
      
      // if (isset($_POST['submit'])) {
      // if (isset($_POST['selected'])){
           $UserName = $_POST['UserName'];
       //}
        
       //if (isset($_POST['comment'])){
          // $comment = $_POST['comment'];
       
       //}
       
       //$display="yes";
       
      // }
       
       $sql="insert into vote(chairman)values('$chairman')";
      
      mysql_query($sql) or die(mysql_error());
      
      ?>
      
      <style type="text/css">
      <!--
      body {
      	background-color: #D5EDB3;
      }
      -->
      </style><HEAD>
      <script type = "text/javascript">
      window.location="thank-you.html"
      </script>
      please help me...thank you so much....
      Last edited by Niheel; Dec 7 '11, 11:26 PM. Reason: codetags

      Comment

      • AutumnsDecay
        New Member
        • Mar 2008
        • 170

        #4
        I don't see where "$chairman" is being set on the "save.php" page.

        Judging by your code, you'd want to put something like this on save.php BEFORE your MySQL query:

        Code:
        $chairman = $_POST['select1'];

        Comment

        • enrekan2011 lan
          New Member
          • Dec 2011
          • 3

          #5
          AutumnsDecay... .. thanks for your reply....

          ok...i have put the something code in "save.php" page like bellow

          Code:
          $UserName = $_POST['$select1'];
          $UserName = $_POST['$name'];
          but...when i select the username and clik submit....the data suppose insert into database right.when i go to my database and check it...no data have been insert. i tired already to find the solution....

          here is my fully code..

          first page select.php
          Code:
          <?
          session_start ();
          include "connection.php";
          mysql_connect ("$host", "$username", "$password") or die ("cannot connect");
          mysql_select_db ("$db_name") or die ("cannot select DB");
          ?>
          
          ***************
          
          some html code 
          
          ****************
          
          <form name="form1" method="post" action="save.php">
            <table width="35%" border="1" align="center">
              <tr>
                <td colspan="2">Who you want to vote as a ......</td>
              </tr>
              <tr>
                <td colspan="2">&nbsp;</td>
              </tr>
              <tr>
                <td width="9%" height="26">Chairman</td>
                <td>
            <label>
            <select name="select1" id="select" >
          
          <?
          // select the data from my database
          $sql = "select UserName from advlogin order by UserName ASC";
          $rs = mysql_query($sql) or die (mysql_error());
          
          if ($rs)
          {
          	while ($rw = mysql_fetch_object($rs))
          	{
          		$UserName=$rw->UserName;
          ?>
          <? // only username i want to select from my database ?>
          	<option value="<? echo $UserName; ?>"<?php if(!empty($row['$UserName']) and ($row['option'] == "$UserName")) { echo(" selected=\"selected\""); }?>><? echo $UserName; ?> </option>
          <?
          }
          	
          }
          
          ?>
            </select> 
            </label>
                </td>
              </tr>
              <tr>
                <td>&nbsp;</td>
                <td><label>
                  <input type="submit" name="button" value="Submit">
                </label></td>
              </tr>
            </table>
          </form>


          this is my second page "save .php" page

          in this part of my coding.. i want the username have been selected can insert into database.....
          Code:
          <?
          include "con3.php";
          
           mysql_connect("$host", "$username", "$password")or die("cannot connect");
           mysql_select_db("$db_name")or die("cannot select DB");
          
          	//if (isset($_POST['submit'])) {
          	//if (isset($_POST['selected'])){
          	$UserName = $_POST['$select1'];
              $UserName = $_POST['$name'];
           //}
            
           //if (isset($_POST['comment'])){
              // $comment = $_POST['comment'];
           
           //}
           
           //$display="yes";
           
          // }
           
           $sql="insert into vote(name)values('$name')";
          
          mysql_query($sql) or die(mysql_error());
          ?>
          
          
          
          <?
          //}
          //}
          ?>
          
          
          
          
          <style type="text/css">
          <!--
          body {
          	background-color: #D5EDB3;
          }
          -->
          </style>
          <a href="select.php">back
          </a>
          <HEAD>


          please help me... im appreciated it....thanks...
          Last edited by Niheel; Dec 7 '11, 11:27 PM. Reason: please use code tags

          Comment

          • AutumnsDecay
            New Member
            • Mar 2008
            • 170

            #6
            First off, don't use the dollar sign ($) in the $_POST array.

            Code:
            $var = $_POST['variable'];  // GOOD
            $var = $_POST['$variable']; // BAD
            Another thing to note is that <option> tags go INSIDE a <select> element.

            Code:
            <select name="myDropDown">
                <option value="1">1</option>
                <option value="2">2</option>
            </select>
            You would then want to assign the $name variable to the value of the select box, example:

            Code:
            <?php
            
            $name = $_POST['myDropDown'];
            
            ?>
            Also, near the end of your save.php file you're overwriting a value by doing this:

            Code:
            $UserName = $_POST['$select1'];
            $UserName = $_POST['$name'];
            That sets $UserName to the Chairman name, but then immediately after it sets $UserName to the name of the user.

            Change it to something like:

            Code:
            $chairman = $_POST['$select1'];
            $UserName = $_POST['$name'];
            You have a fair amount of errors in your script.. You'll want to go through it patiently and comb through areas you're not sure of. A little research can go a long way.

            Comment

            Working...