473,396 Members | 1,605 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Html form

luckysanj
Through one html form how to insert data into different database table.
May 15 '09 #1
23 2529
Ciary
247 Expert 100+
first: set the form method to post like below:
Expand|Select|Wrap|Line Numbers
  1. <form id="aform" method="post">
then, check if you did a post
Expand|Select|Wrap|Line Numbers
  1. if($_SERVER['REQUEST_METHOD'] == 'post'){
after that you simply use an insert query

Expand|Select|Wrap|Line Numbers
  1. $conn = mysql_connect("myDB","root","");
  2. $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
May 15 '09 #2
Dormilich
8,658 Expert Mod 8TB
@Ciary
another way to check if something was posted:
Expand|Select|Wrap|Line Numbers
  1. // HTML
  2. <input type="submit" name="submit" value="submit form">
  3.  
  4. // PHP
  5. if (isset($_POST['submit'])) { … }
May 15 '09 #3
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.
Expand|Select|Wrap|Line Numbers
  1. // HTML
  2. <input type="hidden" name="submitted" value="1" />
  3. <input type="submit" name="submit" />
  4.  
  5. // PHP
  6. if(isset($_POST['submitted'])) {
  7.   ...
  8. }
@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.
May 15 '09 #4
Dormilich
8,658 Expert Mod 8TB
@Atli
geez, you never stop learning new things here… I'll remember that.
May 15 '09 #5
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.
May 16 '09 #6
Markus
6,050 Expert 4TB
Multiple inserts, through one query, can be achieved with syntax like:

Expand|Select|Wrap|Line Numbers
  1. INSERT INTO tbl_name (a,b,c) VALUES(1,2,3),(4,5,6),(7,8,9);
  2.  
Not sure what the earliest version of MySQL this will work with, but it does for 5.1.
May 16 '09 #7
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.
May 16 '09 #8
Markus
6,050 Expert 4TB
@luckysanj
Run separate INSERT queries for each table.
May 16 '09 #9
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.
May 17 '09 #10
Dormilich
8,658 Expert Mod 8TB
you just nned to know which input (aka which POST index) goes to which database and then construct the SQL accordingly.
May 17 '09 #11
Ciary
247 Expert 100+
an example:

Expand|Select|Wrap|Line Numbers
  1. <?php
  2. if($_SERVER['REQUEST_METHOD'] == 'post'){
  3. $conn = mysql_connect("myDB","root","");
  4.  
  5. $result = mysql_query("INSERT INTO myfirsttable (field1,field2,field3) VALUES (".$_POST('myfield1').",".$_POST('myfield2').",".$_POST('myfield3').")");
  6.  
  7. $result = mysql_query("INSERT INTO mysecondtable (field1,field2,field3) VALUES (".$_POST('myfield4').",".$_POST('myfield5').",".$_POST('myfield6').")");
  8.  
  9. $result = mysql_query("INSERT INTO myfirsttable (field1,field2,field3) VALUES (".$_POST('myfield1').",".$_POST('myfield3').",".$_POST('myfield5').")");
  10.  
  11. mysql_close($conn);
  12. }
  13. ?>
  14.  
  15. <form method=post>
  16. <input type='text' id='field1' name='field1' />
  17. <input type='text' id='field2' name='field2' />
  18. <input type='text' id='field3' name='field3' />
  19. <input type='text' id='field4' name='field4' />
  20. <input type='text' id='field5' name='field5' />
  21. <input type='text' id='field6' name='field6' />
  22. </form>
  23.  
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.
May 17 '09 #12
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.


Expand|Select|Wrap|Line Numbers
  1. <?php
  2. foreach($_POST as $key=>$value)
  3.     {
  4.     $$key=$value;
  5.     }
  6. $link=mysql_connect("localhost","root","") or die("Connection Failed");
  7. mysql_select_db("test",$link) or die("Database Not Found");
  8.  
  9. mysql_query("INSERT INTO tbl_personal (per_name,per_address) VALUES ('$per_name','$per_address')");
  10. mysql_query("INSERT INTO tbl_employment (org_name,org_address) VALUES ('$org_name','$org_address')");
  11. mysql_query("INSERT INTO tbl_technical (tec_course,tec_institution) VALUES ('$tec_course','$tec_institution')");
  12. mysql_close($link);
  13.  
  14. ?>
  15. <body>
  16. <form id="form1" name="form1" method="post" action="">
  17.   <table width="71%" border="1" align="center">
  18.     <tr>
  19.       <td width="48%"><strong>Personal Details </strong></td>
  20.       <td width="52%"><strong>Employement Details </strong></td>
  21.     </tr>
  22.     <tr>
  23.       <td><table width="100%" border="0">
  24.         <tr>
  25.           <td width="35%">Name:</td>
  26.           <td width="65%"><input name="per_name" type="text" id="per_name" /></td>
  27.         </tr>
  28.         <tr>
  29.           <td>Address:</td>
  30.           <td><input name="per_address" type="text" id="per_address" /></td>
  31.         </tr>
  32.  
  33.       </table></td>
  34.       <td><table width="100%" border="0">
  35.         <tr>
  36.           <td width="47%">Organization Name:</td>
  37.           <td width="53%"><input name="org_name" type="text" id="org_name" /></td>
  38.         </tr>
  39.         <tr>
  40.           <td>Address</td>
  41.           <td><input name="org_address" type="text" id="org_address" /></td>
  42.         </tr>
  43.  
  44.       </table></td>
  45.     </tr>
  46.     <tr>
  47.       <td><strong>Technical Qualification </strong></td>
  48.       <td>&nbsp;</td>
  49.     </tr>
  50.     <tr>
  51.       <td><table width="100%" border="0">
  52.         <tr>
  53.           <td width="34%">Course Covered: </td>
  54.           <td width="66%"><input name="tec_course" type="text" id="tec_course" /></td>
  55.         </tr>
  56.         <tr>
  57.           <td>Institution</td>
  58.           <td><input name="tec_institution" type="text" id="tec_institution" /></td>
  59.         </tr>
  60.  
  61.       </table></td>
  62.       <td>&nbsp;</td>
  63.     </tr>
  64.     <tr>
  65.       <td><input name="submit" type="submit" id="submit" value="Submit" /></td>
  66.       <td>&nbsp;</td>
  67.     </tr>
  68.   </table>
  69. </form>
May 17 '09 #13
Ciary
247 Expert 100+
Expand|Select|Wrap|Line Numbers
  1. # <?php
  2. # if($_SERVER['REQUEST_METHOD'] == 'post'){
  3. # $conn = mysql_connect("myDB","root","");
  4. #  
  5. # $result = mysql_query("INSERT INTO myfirsttable (field1,field2,field3) VALUES (".$_POST('myfield1').",".$_POST('myfield2').",".$_POST('myfield3').")");
  6. #  
  7. # $result = mysql_query("INSERT INTO mysecondtable (field1,field2,field3) VALUES (".$_POST('myfield4').",".$_POST('myfield5').",".$_POST('myfield6').")");
  8. #  
  9. # $result = mysql_query("INSERT INTO mythirdtable (field1,field2,field3) VALUES (".$_POST('myfield1').",".$_POST('myfield3').",".$_POST('myfield5').")");
  10. #  
  11. # mysql_close($conn);
  12. # }
  13. # ?>
  14. #  
  15. # <form method=post>
  16. # <input type='text' id='myfield1' name='myfield1' />
  17. # <input type='text' id='myfield2' name='myfield2' />
  18. # <input type='text' id='myfield3' name='myfield3' />
  19. # <input type='text' id='myfield4' name='myfield4' />
  20. # <input type='text' id='myield5' name='myfield5' />
  21. # <input type='text' id='myfield6' name='myfield6' />
  22. # </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.
May 17 '09 #14
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.
May 17 '09 #15
Dormilich
8,658 Expert Mod 8TB
just for info: $_POST['key'] (with square brackets, but that's probably a typo)
May 17 '09 #16
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.
May 17 '09 #17
Ciary
247 Expert 100+
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:
Expand|Select|Wrap|Line Numbers
  1. $result = mysql_query("SELECT * FROM tbl_a");
  2. $anArray = mysql_fetch_assoc($result);
  3.  
  4. $lastRecord = $anArray[(count($anArray)-1)];
  5.  
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
May 17 '09 #18
It doesn't work ciary sir.
May 18 '09 #19
Dormilich
8,658 Expert Mod 8TB
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.
May 18 '09 #20
Markus
6,050 Expert 4TB
Use ORDER BY on std_id and then LIMIT 1.
Expand|Select|Wrap|Line Numbers
  1. SELECT * FROM `tbl_name` ORDER BY `std_id` DESC
  2.  
May 18 '09 #21
Thank you .
Jul 26 '09 #22
Canabeez
126 100+
@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:

Expand|Select|Wrap|Line Numbers
  1. $$key = addslashes($value);
  2. /* or */
  3. $$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.
Jul 26 '09 #23
Thank you very much .
Jul 27 '09 #24

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

Similar topics

7
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:...
5
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...
16
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...
17
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...
4
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...
7
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"...
19
Atli
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...
1
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...
10
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...
9
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 --...
0
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...
0
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
1
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...
0
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...
0
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...
0
Oralloy
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,...
0
jinu1996
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...
0
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...
0
tracyyun
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...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.