Through one html form how to insert data into different database table.
23 2529
first: set the form method to post like below: - <form id="aform" method="post">
then, check if you did a post - if($_SERVER['REQUEST_METHOD'] == 'post'){
after that you simply use an insert query - $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
@Ciary
another way to check if something was posted: - // HTML
-
<input type="submit" name="submit" value="submit form">
-
-
// PHP
-
if (isset($_POST['submit'])) { … }
Atli 5,058
Expert 4TB @Dormilich
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. -
// HTML
-
<input type="hidden" name="submitted" value="1" />
-
<input type="submit" name="submit" />
-
-
// PHP
-
if(isset($_POST['submitted'])) {
-
...
-
}
@Ciary
Just make sure you don't actually put the $_POST fields directly into the query, or your database will be vulnerable to SQL Injection.
@Atli
geez, you never stop learning new things here… I'll remember that.
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.
Multiple inserts, through one query, can be achieved with syntax like: -
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.
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.
@luckysanj
Run separate INSERT queries for each table.
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.
you just nned to know which input (aka which POST index) goes to which database and then construct the SQL accordingly.
an example: - <?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.
IN UR GUIDENCE I have GOT NEW IDEA & THANK YOU VERY MUCH for ur cooperation.
IN
$result = mysql_query("INSERT INTO myfirsttable (field1,field2,field3) VALUES (" .$_POST('myfield1')."," .$_POST('myfield2')."," .$_POST('myfield3').")");
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. - <?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>
- # <?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.
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.
just for info: $_POST['key'] (with square brackets, but that's probably a typo)
thank you, Dormilich for replying. It is now working.
Is there any function on php which reads the last value of data on the database table.
For eg: On database table
tbl_a
std_id name
1 Ram
2 Shyam
3 Hari
4 Kishor
In these above table. I want to read that last value of std_id(4) through php function or any idea if u have .
& one thing more is How to use primary key & foreign key on sql database.
seems like there were a lot of typo's in my code. i also forgot mysql_select_db but you already knew i did.
the request is very simple i think: -
$result = mysql_query("SELECT * FROM tbl_a");
-
$anArray = mysql_fetch_assoc($result);
-
-
$lastRecord = $anArray[(count($anArray)-1)];
-
for the question about primary key and foreign key, i would refer to the mysql forum since it isnt php anymore and ... i can't really answer that :-O
It doesn't work ciary sir.
that's because only the first row was fetched…
depending on your DB system, there are functions that will fetch all results in one go (like PDOStatement->fetchAll()) where this would work.
Use ORDER BY on std_id and then LIMIT 1. -
SELECT * FROM `tbl_name` ORDER BY `std_id` DESC
-
@luckysanj
Hi, using this code won't protect you from SQL Injection, you're still inserting direct $_POST vars into your SQL query, I suggest you at least do something like: -
$$key = addslashes($value);
-
/* or */
-
$$key=mysql_real_escape_string($value);
And to be more accurate, the query for multiple inserts, as Markus mentioned
Multiple inserts, through one query, can be achieved with syntax like:
Expand|Select|Wrap|Line Numbers
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.
that is available since mySQL 4, so it should work perfectly (in case you are using mySQL 4 or higher), otherwise you must probably have sql syntax error or your number of inserts is limited by configuration.
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Hansan |
last post by:
Hi all, I hope you have time to help me out a little. My problem is that I
want to combine some python code I have made with some html templates, I
found a tutorial at dev shed:...
|
by: Richard Cornford |
last post by:
I am interested in hearing opinions on the semantic meaning of FORM
(elements) in HTML.
I have to start of apologising because this question arose in a context
that is not applicable to the...
|
by: Philippe C. Martin |
last post by:
Hi,
I am trying to change the data in a form field from python. The following
code does not crash but has no effect as if "form" is just a copy of the
original html form.
Must I recreate the...
|
by: Lloyd Sheen |
last post by:
This IDE is driving me nuts. I needed another button so I copied an
existing one, changed the Text and the id and position by drag and drop.
Well then I run and get the following:
Control...
|
by: Sathyaish |
last post by:
I am no JavaScript progammer, and unfortunately am having to babysit an
old code base that has a lot of JavaScript in it.
I have two questions:
(1) Can two HTML controls have the same name? It...
|
by: thersitz |
last post by:
I can't seem to get my html form to submit properly from within a web form.
Here's my form tag syntax and some delivery hidden fields.
<form id="myForm"...
|
by: Atli |
last post by:
Introduction
At some point, all web developers will need to collect data from their users. In a dynamic web page, everything revolves around the users input, so knowing how to ask for and collect...
|
by: Arpit Nagar |
last post by:
Hi...
I am creating a dummy project for collage.
Here I had choosen my project as Jewelleryshoping.
Now the scenerio is like that thier are jewellry item which I had display in table format with...
|
by: happyse27 |
last post by:
Hi All,
I got this apache errors(see section A1 and A2 below) when I used a html(see section b below) to activate acctman.pl(see section c below). Section D below is part of the configuration...
|
by: dhtml |
last post by:
I have written an article "Unsafe Names for HTML Form Controls".
<URL: http://jibbering.com/faq/names/ >
I would appreciate any reviews, technical or otherwise.
Garrett
--...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
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
|
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...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
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,...
|
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...
|
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...
|
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...
| |