473,394 Members | 1,567 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

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



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
5 15434
zorgi
431 Expert 256MB
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
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
170 100+
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
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
170 100+
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

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

Similar topics

4
by: Laura K | last post by:
I have a drop down menu which has a list of subcategories and the initial value is "please choose a Subcategory". When the user chooses a subcategory they are taken to a new page where the drop...
2
by: Boujii | last post by:
Greetings, I have been attempting to make a drop down menu of countries. From this menu I wish to create a variable in order to INPUT into mysql database. I have no trouble making the drop down menu,...
4
by: TycoonUK | last post by:
Hi, As I do not have IE7 on my computer, I was wondering if there is a fault in my CSS Menu when using IE7. Please can someone look at my site - http://www.worldofmonopoly.co.uk and tell me...
3
by: SQLjunky | last post by:
Hello, Contrary to what my screen name indicates, i'm actually quite new to SQL. Having said that, I'll get right into my problem. I'm trying to query an mssql database to return to the value...
2
by: giandeo | last post by:
Hello all, It's almost a couple of weeks since i am struggling to get this code work. Unfortunately, i am stuck. There seems to be no hope... Please Help....... I am working with an asp page...
0
by: Slickuser | last post by:
From my PHP page: Grab all data from the database. Go through a loop to generate the HTML. Client side: From the Color drop menu list, if a user change the value. It will grab that value &...
0
by: Slickuser | last post by:
From my PHP page: Grab all data from the database. Go through a loop to generate the HTML. Client side: From the Color drop menu list, if a user change the value. It will grab that value &...
0
by: rahullko05 | last post by:
i have designed a menu list program in which i'm facing a problem where the last li item (white crappie) shifts down when i hover mouse pointer to just above li item (ozrack bazz) of white crappie...
7
by: wizardry | last post by:
i can query the list fine using: select list.list, many_table.list_id_fk from list left join many_table on list.id = many_table.list_id_fk and many_table.userid = 'UserId'; that populates...
1
by: micropos | last post by:
hey,i m newbie to php.i wan ask that how to pass value from drop down menu into database .after click ''go'' button ,it will out.thanks...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.