By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,972 Members | 1,092 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,972 IT Pros & Developers. It's quick & easy.

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

P: 3
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...)



Expand|Select|Wrap|Line Numbers
  1. <form name="form1" method="post" action="">
  2.   <table width="80%" border="1" align="center">
  3.     <tr>
  4.       <td colspan="5">my election.</td>
  5.     </tr>
  6.     <tr>
  7.       <td width="2%">&nbsp;</td>
  8.       <td width="3%">&nbsp;</td>
  9.       <td colspan="3">&nbsp;</td>
  10.     </tr>
  11.     <tr>
  12.       <td>&nbsp;</td>
  13.       <td colspan="4">Who you want to vote as a ......</td>
  14.     </tr>
  15.     <tr>
  16.       <td>&nbsp;</td>
  17.       <td>&nbsp;</td>
  18.       <td colspan="3">&nbsp;</td>
  19.     </tr>
  20.     <tr>
  21.       <td height="26">&nbsp;</td>
  22.       <td>&nbsp;</td>
  23.       <td>Chairman</td>
  24.       <td width="24%">
  25. <label>
  26. <select name="select1" id="select" >
  27. <?
  28. $sql = "select UserName from advlogin order by UserName ASC";
  29. $rs = mysql_query($sql) or die (mysql_error());
  30.  
  31. if ($rs)
  32. {
  33.     while ($rw = mysql_fetch_object($rs))
  34.     {
  35.         $UserName=$rw->UserName;
  36. ?>
  37. <option><? echo $UserName; ?> </option>
  38.  
  39. <?
  40.  
  41.     }
  42.  
  43. }
  44.  
  45. ?>
  46. </select> 
  47. </label>
  48.       </td>
  49.       <td width="39%">&nbsp;</td>
  50.     </tr>
  51.     <tr>
  52.       <td height="26">&nbsp;</td>
  53.       <td>&nbsp;</td>
  54.       <td>Deputy Chairman</td>
  55.       <td>
  56.  
  57. <label>
  58. <select name="select2" id="select" >
  59. <?
  60. $sql = "select UserName from advlogin order by UserName ASC";
  61. $rs = mysql_query($sql) or die (mysql_error());
  62.  
  63. if ($rs)
  64. {
  65.     while ($rw = mysql_fetch_object($rs))
  66.     {
  67.         $UserName=$rw->UserName;
  68. ?>
  69. <option><? echo $UserName; ?> </option>
  70.  
  71. <?
  72.  
  73.     }
  74.  
  75. }
  76.  
  77. ?>
  78. </select> 
  79. </label>
  80.  
  81.  
  82.  
  83.  
  84.  
  85.  
  86.       </td>
  87.       <td>&nbsp;</td>
  88.     </tr>
  89.     <tr>
  90.       <td height="26">&nbsp;</td>
  91.       <td>&nbsp;</td>
  92.       <td>Secretary</td>
  93.       <td>
  94. <label>
  95. <select name="select3" id="select" >
  96. <?
  97. $sql = "select UserName from advlogin order by UserName ASC";
  98. $rs = mysql_query($sql) or die (mysql_error());
  99.  
  100. if ($rs)
  101. {
  102.     while ($rw = mysql_fetch_object($rs))
  103.     {
  104.         $UserName=$rw->UserName;
  105. ?>
  106. <option><? echo $UserName; ?> </option>
  107.  
  108. <?
  109.  
  110.     }
  111.  
  112. }
  113.  
  114. ?>
  115. </select> 
  116. </label>
  117.       </td>
  118.       <td>&nbsp;</td>
  119.     </tr>
  120.     <tr>
  121.       <td>&nbsp;</td>
  122.       <td>&nbsp;</td>
  123.       <td>Treasurer</td>
  124.       <td>
  125. <label>
  126. <select name="select4" id="select" >
  127. <?
  128. $sql = "select UserName from advlogin order by UserName ASC";
  129. $rs = mysql_query($sql) or die (mysql_error());
  130.  
  131. if ($rs)
  132. {
  133.     while ($rw = mysql_fetch_object($rs))
  134.     {
  135.         $UserName=$rw->UserName;
  136. ?>
  137. <option><? echo $UserName; ?> </option>
  138.  
  139. <?
  140.  
  141.     }
  142.  
  143. }
  144.  
  145. ?>
  146. </select> 
  147. </label>      
  148.  
  149.  
  150.       </td>
  151.       <td>&nbsp;</td>
  152.     </tr>
  153.     <tr>
  154.       <td>&nbsp;</td>
  155.       <td>&nbsp;</td>
  156.       <td>Commitee Members</td>
  157.       <td>
  158. <label>
  159. <select name="select5" id="select" >
  160. <?
  161. $sql = "select UserName from advlogin order by UserName ASC";
  162. $rs = mysql_query($sql) or die (mysql_error());
  163.  
  164. if ($rs)
  165. {
  166.     while ($rw = mysql_fetch_object($rs))
  167.     {
  168.         $UserName=$rw->UserName;
  169. ?>
  170. <option selected><? echo $UserName; ?> </option>
  171.  
  172. <?
  173.  
  174.     }
  175.  
  176. }
  177.  
  178. ?>
  179. </select> 
  180. </label>      
  181.       </td>
  182.       <td>&nbsp;</td>
  183.     </tr>
  184.     <tr>
  185.       <td>&nbsp;</td>
  186.       <td>&nbsp;</td>
  187.       <td>&nbsp;</td>
  188.       <td colspan="2"><label>
  189.         <input type="submit" name="button" id="button" value="Submit">
  190.       </label></td>
  191.     </tr>
  192.     <tr>
  193.       <td height="23">&nbsp;</td>
  194.       <td>&nbsp;</td>
  195.       <td width="16%">&nbsp;</td>
  196.       <td colspan="2">&nbsp;</td>
  197.     </tr>
  198.   </table>
  199.  
  200. </form>
Dec 5 '11 #1
Share this Question
Share on Google+
5 Replies


zorgi
Expert 100+
P: 431
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.
Dec 5 '11 #2

P: 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)the code bellow...


Expand|Select|Wrap|Line Numbers
  1. <?
  2. session_start ();
  3. include "connection.php";
  4. mysql_connect ("$host", "$username", "$password") or die ("cannot connect");
  5. mysql_select_db ("$db_name") or die ("cannot select DB");
  6. ?>
  7. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  8. <html>
  9. <head>
  10. <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  11. <title>Untitled Document</title>
  12. </head>
  13.  
  14. <body>
  15. <form name="form1" method="post" action="save.php">
  16.   <table width="80%" border="1" align="center">
  17.     <tr>
  18.       <td colspan="5">my electon</td>
  19.     </tr>
  20.     <tr>
  21.       <td width="2%">&nbsp;</td>
  22.       <td width="3%">&nbsp;</td>
  23.       <td colspan="3">&nbsp;</td>
  24.     </tr>
  25.     <tr>
  26.       <td>&nbsp;</td>
  27.       <td colspan="4">Who you want to vote as a ......</td>
  28.     </tr>
  29.     <tr>
  30.       <td>&nbsp;</td>
  31.       <td>&nbsp;</td>
  32.       <td colspan="3">&nbsp;</td>
  33.     </tr>
  34.     <tr>
  35.       <td height="26">&nbsp;</td>
  36.       <td>&nbsp;</td>
  37.       <td>Chairman</td>
  38.       <td width="24%">
  39. <label>
  40. <select name="select1" id="select" >
  41. <?
  42. $sql = "select UserName from advlogin order by UserName ASC";
  43. $rs = mysql_query($sql) or die (mysql_error());
  44.  
  45. if ($rs)
  46. {
  47.     while ($rw = mysql_fetch_object($rs))
  48.     {
  49.         $UserName=$rw->UserName;
  50. ?>
  51. <option value="<? echo $UserName; ?>"<?php if(!empty($row['option']) and ($row['option'] == "$UserName")) { echo(" selected=\"selected\""); }?>><? echo $UserName; ?> </option>
  52. <?
  53. }
  54.  
  55. }
  56. ?>
  57. </select> 
  58. </label>
  59.       </td>
  60.       <td width="39%">&nbsp;</td>
  61.     </tr>
  62.     <tr>
  63.       <td>&nbsp;</td>
  64.       <td>&nbsp;</td>
  65.       <td>&nbsp;</td>
  66.       <td colspan="2">
  67.         <input type="submit" name="button" value="Submit">
  68.       </td>
  69.     </tr>
  70.     <tr>
  71.       <td height="23">&nbsp;</td>
  72.       <td>&nbsp;</td>
  73.       <td width="16%">&nbsp;</td>
  74.       <td colspan="2">&nbsp;</td>
  75.     </tr>
  76.   </table>
  77.  
  78. </form>
  79. </body>
  80. </html>
  81.  

second page ( save.php)
Expand|Select|Wrap|Line Numbers
  1. <?
  2. include "connection.php";
  3.  
  4.  mysql_connect("$host", "$username", "$password")or die("cannot connect");
  5.  mysql_select_db("$db_name")or die("cannot select DB");
  6.  
  7. // if (isset($_POST['submit'])) {
  8. // if (isset($_POST['selected'])){
  9.      $UserName = $_POST['UserName'];
  10.  //}
  11.  
  12.  //if (isset($_POST['comment'])){
  13.     // $comment = $_POST['comment'];
  14.  
  15.  //}
  16.  
  17.  //$display="yes";
  18.  
  19. // }
  20.  
  21.  $sql="insert into vote(chairman)values('$chairman')";
  22.  
  23. mysql_query($sql) or die(mysql_error());
  24.  
  25. ?>
  26.  
  27. <style type="text/css">
  28. <!--
  29. body {
  30.     background-color: #D5EDB3;
  31. }
  32. -->
  33. </style><HEAD>
  34. <script type = "text/javascript">
  35. window.location="thank-you.html"
  36. </script>
  37.  
please help me...thank you so much....
Dec 6 '11 #3

AutumnsDecay
100+
P: 170
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:

Expand|Select|Wrap|Line Numbers
  1. $chairman = $_POST['select1'];
  2.  
Dec 6 '11 #4

P: 3
AutumnsDecay..... thanks for your reply....

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

Expand|Select|Wrap|Line Numbers
  1. $UserName = $_POST['$select1'];
  2. $UserName = $_POST['$name'];
  3.  
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
Expand|Select|Wrap|Line Numbers
  1. <?
  2. session_start ();
  3. include "connection.php";
  4. mysql_connect ("$host", "$username", "$password") or die ("cannot connect");
  5. mysql_select_db ("$db_name") or die ("cannot select DB");
  6. ?>
  7.  
  8. ***************
  9.  
  10. some html code 
  11.  
  12. ****************
  13.  
  14. <form name="form1" method="post" action="save.php">
  15.   <table width="35%" border="1" align="center">
  16.     <tr>
  17.       <td colspan="2">Who you want to vote as a ......</td>
  18.     </tr>
  19.     <tr>
  20.       <td colspan="2">&nbsp;</td>
  21.     </tr>
  22.     <tr>
  23.       <td width="9%" height="26">Chairman</td>
  24.       <td>
  25.   <label>
  26.   <select name="select1" id="select" >
  27.  
  28. <?
  29. // select the data from my database
  30. $sql = "select UserName from advlogin order by UserName ASC";
  31. $rs = mysql_query($sql) or die (mysql_error());
  32.  
  33. if ($rs)
  34. {
  35.     while ($rw = mysql_fetch_object($rs))
  36.     {
  37.         $UserName=$rw->UserName;
  38. ?>
  39. <? // only username i want to select from my database ?>
  40.     <option value="<? echo $UserName; ?>"<?php if(!empty($row['$UserName']) and ($row['option'] == "$UserName")) { echo(" selected=\"selected\""); }?>><? echo $UserName; ?> </option>
  41. <?
  42. }
  43.  
  44. }
  45.  
  46. ?>
  47.   </select> 
  48.   </label>
  49.       </td>
  50.     </tr>
  51.     <tr>
  52.       <td>&nbsp;</td>
  53.       <td><label>
  54.         <input type="submit" name="button" value="Submit">
  55.       </label></td>
  56.     </tr>
  57.   </table>
  58. </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.....
Expand|Select|Wrap|Line Numbers
  1. <?
  2. include "con3.php";
  3.  
  4.  mysql_connect("$host", "$username", "$password")or die("cannot connect");
  5.  mysql_select_db("$db_name")or die("cannot select DB");
  6.  
  7.     //if (isset($_POST['submit'])) {
  8.     //if (isset($_POST['selected'])){
  9.     $UserName = $_POST['$select1'];
  10.     $UserName = $_POST['$name'];
  11.  //}
  12.  
  13.  //if (isset($_POST['comment'])){
  14.     // $comment = $_POST['comment'];
  15.  
  16.  //}
  17.  
  18.  //$display="yes";
  19.  
  20. // }
  21.  
  22.  $sql="insert into vote(name)values('$name')";
  23.  
  24. mysql_query($sql) or die(mysql_error());
  25. ?>
  26.  
  27.  
  28.  
  29. <?
  30. //}
  31. //}
  32. ?>
  33.  
  34.  
  35.  
  36.  
  37. <style type="text/css">
  38. <!--
  39. body {
  40.     background-color: #D5EDB3;
  41. }
  42. -->
  43. </style>
  44. <a href="select.php">back
  45. </a>
  46. <HEAD>


please help me... im appreciated it....thanks...
Dec 7 '11 #5

AutumnsDecay
100+
P: 170
First off, don't use the dollar sign ($) in the $_POST array.

Expand|Select|Wrap|Line Numbers
  1. $var = $_POST['variable'];  // GOOD
  2. $var = $_POST['$variable']; // BAD
  3.  
Another thing to note is that <option> tags go INSIDE a <select> element.

Expand|Select|Wrap|Line Numbers
  1. <select name="myDropDown">
  2.     <option value="1">1</option>
  3.     <option value="2">2</option>
  4. </select>
  5.  
You would then want to assign the $name variable to the value of the select box, example:

Expand|Select|Wrap|Line Numbers
  1. <?php
  2.  
  3. $name = $_POST['myDropDown'];
  4.  
  5. ?>
  6.  
Also, near the end of your save.php file you're overwriting a value by doing this:

Expand|Select|Wrap|Line Numbers
  1. $UserName = $_POST['$select1'];
  2. $UserName = $_POST['$name'];
  3.  
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:

Expand|Select|Wrap|Line Numbers
  1. $chairman = $_POST['$select1'];
  2. $UserName = $_POST['$name'];
  3.  
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.
Dec 7 '11 #6

Post your reply

Sign in to post your reply or Sign up for a free account.