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

Post drop-down menu selection to different tables in mysql

P: 1
Hi All,

I am new to PHP and mysql and would greatly appreciate your help.

I am trying to learn PHP and mysql so I decided to create a project to help me do so. I created a registration form that post data to different tables in mysql database.


My form contains a dropdown menu. I was able to post all the items from my form to the mathcourse table in mysql database but not from my dropdown menu.

What I would like to achieve is that if a selection is
made from my form then all the information should go to a particular table.

I have two tables: mathcourse and chemcourse. If math is selected, then the user information should go to the
'mathcourse' table. If chem is selected, then the user information should go to the 'chemcourse' table. Please help

Below is my html form:

Expand|Select|Wrap|Line Numbers
  1.     <form action='register.php' method='POST'>
  2.  
  3.         FName: <br />
  4.  
  5.     <input type="text" name="FName" size ="40"><br />
  6.  
  7.         LName:<br />
  8.  
  9.     <input type="text" name="LName" size ="25"><br />
  10.  
  11.         Course:<br />
  12.  
  13.     <select name ="Course">
  14.     <option value =""  </option><br />
  15.     <option value ="Math"  size ="40">Math</option><br />
  16.     <option value ="Chem"  size ="40">Chem</option>
  17.     </select><br />
  18.  
  19.         Phone:<br/>
  20.  
  21.     <input type="text" name="Phone" size ="20"><br />
  22.  
  23.         Email:
  24.  
  25.     <input type="text" name="Email" size ="60"><br />
  26.  
  27.         submit:
  28.  
  29.     <input type="submit"name ="submit"/>

Here is the code for register.php page

Expand|Select|Wrap|Line Numbers
  1. <?php
  2.  
  3.  
  4. define('DB_NAME',"Colstudents");
  5. define('DB_USER',"root");
  6. define('DB_PASSWORD', "");
  7. define('DB_HOST',"localhost");
  8.  
  9. $link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);//store connection into a variable
  10.  
  11. if(!$link) {
  12.     die('could not connect: ' . mysql_error());
  13. }
  14.  
  15. mysql_select_db(DB_NAME, $link);
  16.  
  17. if(!$link) {
  18.     die("can\'t use" . DB_NAME . ':' . mysql_error());
  19. }
  20.  
  21. echo 'connected sucessfully';
  22.  
  23. $selectcourse ='Course';
  24.  
  25. if (option value == 'math'); $_POST mathcourse['Course'];
  26.  
  27. else (option value == 'chem'); $_POST chemcourse['Course'];
  28.  
  29.  
  30. //store data into our dabase
  31.  
  32.  
  33. $FName     =$_POST['FName'];
  34. $LName     =$_POST['LName'];
  35. $Course    =$_POST['Course'];
  36. $Phone     =$_POST['Phone'];
  37. $Email     =$_POST['Email'];
  38. $submit = $_POST['submit'];
  39. $date = date("Y-m-d");
  40.  
  41. $sql2 ="INSERT INTO mthcourse SET  FName='$FName', LName= '$LName', Course = '$Course',  Phone = '$Phone', Email = '$Email', Date = '$date'";
  42. $result=mysql_query($sql2);
  43.  
  44. //if(!//mysql_query) {
  45. //    di//e('Error: ' .mysql_error());
  46. //}
  47.  
  48. mysql_close();
  49.  
  50. ?>
Mar 22 '12 #1
Share this Question
Share on Google+
2 Replies


Dormilich
Expert Mod 5K+
P: 8,639
line #25 & #27 (PHP code) are totally off. that's not how you do a condition or fetching form data (that part should get you a syntax error)
Mar 22 '12 #2

P: 93
// first get the value of selection from dropdown menu something like this :

Expand|Select|Wrap|Line Numbers
  1. $temp = $_POST['drop_down_menu'];
now make query with if condition like this ::

Expand|Select|Wrap|Line Numbers
  1. if($temp == "math")
  2. {
  3.    $qry = "insert into mathcourse values('','','')";
  4. else
  5. {
  6.    $qry = "insert into chemcourse values('','','')";
  7. }
now execure the query like this :::

Expand|Select|Wrap|Line Numbers
  1. $query = mysql_query($qry) or die(mysql_error());
try it.........

Bharat Parmar(Bharat383)
Apr 16 '12 #3

Post your reply

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