Html form

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • luckysanj
    New Member
    • May 2009
    • 68

    Html form

    Through one html form how to insert data into different database table.
  • Ciary
    Recognized Expert New Member
    • Apr 2009
    • 247

    #2
    first: set the form method to post like below:
    Code:
    <form id="aform" method="post">
    then, check if you did a post
    Code:
    if($_SERVER['REQUEST_METHOD'] == 'post'){
    after that you simply use an insert query

    Code:
    $conn = mysql_connect("myDB","root","");
    $result = mysql_query("INSERT INTO myTable (field1, field2) VALUES ('".$_POST['field1']."', '".$_POST['field2']."')");
    if you have multiple tables you need more then one insert-query

    Comment

    • Dormilich
      Recognized Expert Expert
      • Aug 2008
      • 8694

      #3
      Originally posted by Ciary
      then, check if you did a post
      Code:
      if($_SERVER['REQUEST_METHOD'] == 'post'){
      another way to check if something was posted:
      Code:
      // HTML
      <input type="submit" name="submit" value="submit form">
      
      // PHP
      if (isset($_POST['submit'])) { … }

      Comment

      • Atli
        Recognized Expert Expert
        • Nov 2006
        • 5062

        #4
        Originally posted by Dormilich
        another way to check if something was posted:
        Code:
        // HTML
        <input type="submit" name="submit" value="submit form">
        
        // PHP
        if (isset($_POST['submit'])) { … }
        There is one cavity in that.
        If the form is submitted without the use of the submit button (by pressing Enter, for example), the submit button will not be included in the request.

        A way around this is to include a hidden element and use that to check.
        Code:
        // HTML
        <input type="hidden" name="submitted" value="1" />
        <input type="submit" name="submit" />
        
        // PHP
        if(isset($_POST['submitted'])) {
          ...
        }
        Originally posted by Ciary
        [...]
        after that you simply use an insert query

        Code:
        $conn = mysql_connect("myDB","root","");
        $result = mysql_query("INSERT INTO myTable (field1, field2) VALUES ('".$_POST['field1']."', '".$_POST['field2']."')");
        if you have multiple tables you need more then one insert-query
        Just make sure you don't actually put the $_POST fields directly into the query, or your database will be vulnerable to SQL Injection.

        Comment

        • Dormilich
          Recognized Expert Expert
          • Aug 2008
          • 8694

          #5
          Originally posted by Atli
          There is one cavity in that.
          If the form is submitted without the use of the submit button (by pressing Enter, for example), the submit button will not be included in the request.
          geez, you never stop learning new things here… I'll remember that.

          Comment

          • luckysanj
            New Member
            • May 2009
            • 68

            #6
            Thank you for your all cooperation & it is very helpful for me. But i have not got my actually answer. It may be i was not able to clear my problem with you.

            So once more i am trying to clear you my problem.
            Dear sir, I have three different database table like:

            tbl_a, tbl_b, tbl_c & it has different field also.

            & I want to insert data in these different table through one html form.

            so plz guide me is it possible to insert data in different database table through on html form.

            Comment

            • Markus
              Recognized Expert Expert
              • Jun 2007
              • 6092

              #7
              Multiple inserts, through one query, can be achieved with syntax like:

              Code:
              INSERT INTO tbl_name (a,b,c) VALUES(1,2,3),(4,5,6),(7,8,9);
              Not sure what the earliest version of MySQL this will work with, but it does for 5.1.

              Comment

              • luckysanj
                New Member
                • May 2009
                • 68

                #8
                It doesn't work my mysql cuz i have mysql 5.

                give me another idea or method to input data through one html form to different database table.

                Comment

                • Markus
                  Recognized Expert Expert
                  • Jun 2007
                  • 6092

                  #9
                  Originally posted by luckysanj
                  It doesn't work my mysql cuz i have mysql 5.

                  give me another idea or method to input data through one html form to different database table.
                  Run separate INSERT queries for each table.

                  Comment

                  • luckysanj
                    New Member
                    • May 2009
                    • 68

                    #10
                    Hello Ciary, Can u give me idea to insert data into multiple table like tbl_a, tbl_b, tbl_c through one HTML form.
                    there is::
                    3 text box in single form, linked to 3 diffrent tables,
                    do u have any idea to insert data to 3 different tables.

                    Comment

                    • Dormilich
                      Recognized Expert Expert
                      • Aug 2008
                      • 8694

                      #11
                      you just nned to know which input (aka which POST index) goes to which database and then construct the SQL accordingly.

                      Comment

                      • Ciary
                        Recognized Expert New Member
                        • Apr 2009
                        • 247

                        #12
                        an example:

                        Code:
                        <?php
                        if($_SERVER['REQUEST_METHOD'] == 'post'){
                        $conn = mysql_connect("myDB","root","");
                        
                        $result = mysql_query("INSERT INTO myfirsttable (field1,field2,field3) VALUES (".$_POST('myfield1').",".$_POST('myfield2').",".$_POST('myfield3').")");
                        
                        $result = mysql_query("INSERT INTO mysecondtable (field1,field2,field3) VALUES (".$_POST('myfield4').",".$_POST('myfield5').",".$_POST('myfield6').")");
                        
                        $result = mysql_query("INSERT INTO myfirsttable (field1,field2,field3) VALUES (".$_POST('myfield1').",".$_POST('myfield3').",".$_POST('myfield5').")");
                        
                        mysql_close($conn);
                        }
                        ?>
                        
                        <form method=post>
                        <input type='text' id='field1' name='field1' />
                        <input type='text' id='field2' name='field2' />
                        <input type='text' id='field3' name='field3' />
                        <input type='text' id='field4' name='field4' />
                        <input type='text' id='field5' name='field5' />
                        <input type='text' id='field6' name='field6' />
                        </form>
                        this is an example with multiple values inserted in multiple tables. normally (as atli discribed) you don't put post-variables directly in your query to avoid SQL injection.

                        Comment

                        • luckysanj
                          New Member
                          • May 2009
                          • 68

                          #13
                          IN UR GUIDENCE I have GOT NEW IDEA & THANK YOU VERY MUCH for ur cooperation.

                          IN

                          $result = mysql_query("IN SERT INTO myfirsttable (field1,field2, field3) VALUES (".$_POST('myfiel d1').",".$_POST('myfiel d2').",".$_POST('myfiel d3').")");

                          In these above underline line there is error. So i use little bit change on ur method.

                          Now it works very well. & can u tell me below this my method is right or not.


                          Code:
                          <?php
                          foreach($_POST as $key=>$value)
                          	{
                          	$$key=$value;
                          	}
                          $link=mysql_connect("localhost","root","") or die("Connection Failed");
                          mysql_select_db("test",$link) or die("Database Not Found");
                          
                          mysql_query("INSERT INTO tbl_personal (per_name,per_address) VALUES ('$per_name','$per_address')");
                          mysql_query("INSERT INTO tbl_employment (org_name,org_address) VALUES ('$org_name','$org_address')");
                          mysql_query("INSERT INTO tbl_technical (tec_course,tec_institution) VALUES ('$tec_course','$tec_institution')");
                          mysql_close($link);
                          
                          ?>
                          <body>
                          <form id="form1" name="form1" method="post" action="">
                            <table width="71%" border="1" align="center">
                              <tr>
                                <td width="48%"><strong>Personal Details </strong></td>
                                <td width="52%"><strong>Employement Details </strong></td>
                              </tr>
                              <tr>
                                <td><table width="100%" border="0">
                                  <tr>
                                    <td width="35%">Name:</td>
                                    <td width="65%"><input name="per_name" type="text" id="per_name" /></td>
                                  </tr>
                                  <tr>
                                    <td>Address:</td>
                                    <td><input name="per_address" type="text" id="per_address" /></td>
                                  </tr>
                                  
                                </table></td>
                                <td><table width="100%" border="0">
                                  <tr>
                                    <td width="47%">Organization Name:</td>
                                    <td width="53%"><input name="org_name" type="text" id="org_name" /></td>
                                  </tr>
                                  <tr>
                                    <td>Address</td>
                                    <td><input name="org_address" type="text" id="org_address" /></td>
                                  </tr>
                                  
                                </table></td>
                              </tr>
                              <tr>
                                <td><strong>Technical Qualification </strong></td>
                                <td>&nbsp;</td>
                              </tr>
                              <tr>
                                <td><table width="100%" border="0">
                                  <tr>
                                    <td width="34%">Course Covered: </td>
                                    <td width="66%"><input name="tec_course" type="text" id="tec_course" /></td>
                                  </tr>
                                  <tr>
                                    <td>Institution</td>
                                    <td><input name="tec_institution" type="text" id="tec_institution" /></td>
                                  </tr>
                                  
                                </table></td>
                                <td>&nbsp;</td>
                              </tr>
                              <tr>
                                <td><input name="submit" type="submit" id="submit" value="Submit" /></td>
                                <td>&nbsp;</td>
                              </tr>
                            </table>
                          </form>
                          Last edited by Markus; May 17 '09, 06:49 PM. Reason: Added [code] tags.

                          Comment

                          • Ciary
                            Recognized Expert New Member
                            • Apr 2009
                            • 247

                            #14
                            Code:
                            # <?php
                            # if($_SERVER['REQUEST_METHOD'] == 'post'){
                            # $conn = mysql_connect("myDB","root","");
                            #  
                            # $result = mysql_query("INSERT INTO myfirsttable (field1,field2,field3) VALUES (".$_POST('myfield1').",".$_POST('myfield2').",".$_POST('myfield3').")");
                            #  
                            # $result = mysql_query("INSERT INTO mysecondtable (field1,field2,field3) VALUES (".$_POST('myfield4').",".$_POST('myfield5').",".$_POST('myfield6').")");
                            #  
                            # $result = mysql_query("INSERT INTO mythirdtable (field1,field2,field3) VALUES (".$_POST('myfield1').",".$_POST('myfield3').",".$_POST('myfield5').")");
                            #  
                            # mysql_close($conn);
                            # }
                            # ?>
                            #  
                            # <form method=post>
                            # <input type='text' id='myfield1' name='myfield1' />
                            # <input type='text' id='myfield2' name='myfield2' />
                            # <input type='text' id='myfield3' name='myfield3' />
                            # <input type='text' id='myfield4' name='myfield4' />
                            # <input type='text' id='myield5' name='myfield5' />
                            # <input type='text' id='myfield6' name='myfield6' />
                            # </form>
                            srr there was a small typo in my code here is the right version (without errors)

                            i guess your code is correct.it would be easier to read if you used code tags.

                            Comment

                            • luckysanj
                              New Member
                              • May 2009
                              • 68

                              #15
                              Still ur same code doesn't work. But it's ok i have solve my problem with ur good guidance So thank you very much.

                              Comment

                              Working...