Post drop-down menu selection to different tables in mysql

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • HumbleAprentice
    New Member
    • Mar 2012
    • 1

    Post drop-down menu selection to different tables in mysql

    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:

    Code:
    	<form action='register.php' method='POST'>
    
    		FName: <br />
    
    	<input type="text" name="FName" size ="40"><br />
    
    		LName:<br />
    
    	<input type="text" name="LName" size ="25"><br />
    
    		Course:<br />
    
    	<select name ="Course">
    	<option value =""  </option><br />
    	<option value ="Math"  size ="40">Math</option><br />
    	<option value ="Chem"  size ="40">Chem</option>
    	</select><br />
    
    		Phone:<br/>
    
    	<input type="text" name="Phone" size ="20"><br />
    
    		Email:
    
    	<input type="text" name="Email" size ="60"><br />
    
    		submit:
    
    	<input type="submit"name ="submit"/>

    Here is the code for register.php page

    Code:
    <?php
    
    
    define('DB_NAME',"Colstudents");
    define('DB_USER',"root");
    define('DB_PASSWORD', "");
    define('DB_HOST',"localhost");
    
    $link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);//store connection into a variable
    
    if(!$link) {
    	die('could not connect: ' . mysql_error());
    }
    
    mysql_select_db(DB_NAME, $link);
    
    if(!$link) {
    	die("can\'t use" . DB_NAME . ':' . mysql_error());
    }
    
    echo 'connected sucessfully';
    
    $selectcourse ='Course';
    
    if (option value == 'math'); $_POST mathcourse['Course'];
    
    else (option value == 'chem'); $_POST chemcourse['Course'];
    
    
    //store data into our dabase
    
    
    $FName 	=$_POST['FName'];
    $LName 	=$_POST['LName'];
    $Course	=$_POST['Course'];
    $Phone 	=$_POST['Phone'];
    $Email 	=$_POST['Email'];
    $submit = $_POST['submit'];
    $date = date("Y-m-d");
    
    $sql2 ="INSERT INTO mthcourse SET  FName='$FName', LName= '$LName', Course = '$Course',  Phone = '$Phone', Email = '$Email', Date = '$date'";
    $result=mysql_query($sql2);
    
    //if(!//mysql_query) {
    //	di//e('Error: ' .mysql_error());
    //}
    
    mysql_close();
    
    ?>
    Last edited by Dormilich; Mar 22 '12, 04:19 PM. Reason: Please use [CODE] [/CODE] tags when posting code.
  • Dormilich
    Recognized Expert Expert
    • Aug 2008
    • 8694

    #2
    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)

    Comment

    • Bharat383
      New Member
      • Aug 2011
      • 93

      #3
      // first get the value of selection from dropdown menu something like this :

      Code:
      $temp = $_POST['drop_down_menu'];
      now make query with if condition like this ::

      Code:
      if($temp == "math")
      {
         $qry = "insert into mathcourse values('','','')";
      } 
      else
      {
         $qry = "insert into chemcourse values('','','')";
      }
      now execure the query like this :::

      Code:
      $query = mysql_query($qry) or die(mysql_error());
      try it.........

      Bharat Parmar(Bharat38 3)
      Last edited by Dormilich; Apr 16 '12, 08:17 AM. Reason: :code:

      Comment

      Working...