Through one html form how to insert data into different database table.
Html form
Collapse
X
-
first: set the form method to post like below:
Code:<form id="aform" method="post">
Code:if($_SERVER['REQUEST_METHOD'] == 'post'){
Code:$conn = mysql_connect("myDB","root",""); $result = mysql_query("INSERT INTO myTable (field1, field2) VALUES ('".$_POST['field1']."', '".$_POST['field2']."')");
-
Comment
-
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'])) { ... }
[...]
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']."')");
Comment
-
Comment
-
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
-
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>
Comment
-
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> </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> </td> </tr> <tr> <td><input name="submit" type="submit" id="submit" value="Submit" /></td> <td> </td> </tr> </table> </form>
Comment
-
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>
i guess your code is correct.it would be easier to read if you used code tags.Comment
Comment