Click to See Complete Forum and Search --> : How do you insert Data into two tables Using one form?


balamberas
06-07-2009, 02:02 PM
Hi, Does anybody know how to insert Data into two tables Using one html form?

ryanbutler
06-07-2009, 02:13 PM
Usually you can do it by just separating the insert statements with a semi-colon:

INSERT INTO Table() VALUES(); INSERT INTO Table2() VALUES()

balamberas
06-22-2009, 02:19 PM
Hi Ryan.

$sql= "INSERT INTO flats (date_posted, type, location, rent, title, image, description, contactEmail, number)
VALUES
(NOW(),'$_POST[type]', '$_POST[location]','$_POST[rent]', '$_POST[title]', '$_POST[image]',
'$_POST[description]','$_POST[contactEmail]','$_POST[number]')";

"INSERT INTO users (email, password, conpass,) VALUES
('$_POST[email]','$_POST[password]','$_POST[confirmPassword]')";

I manage to insert into the flats table but not the users table.
can you see anything wrong with this?

JavaServlet
06-22-2009, 06:21 PM
Get rid of last comma after conpass: "INSERT INTO users (email, password, conpass,) VALUES
('$_POST[email]','$_POST[password]','$_POST[confirmPassword]')";

If this is an all or nothing proposition, I would recommend using Transactions assuming the database your working with supports it.

balamberas
06-23-2009, 04:03 PM
Hi, I removed the comma but still no luck. I only manage to insert into the flats table.

$sql="INSERT INTO flats (date_posted, type, location, rent, title, image, description, contactEmail, number)
VALUES
(NOW(),'$_POST[type]', '$_POST[location]','$_POST[rent]', '$_POST[title]', '$_POST[image]', '$_POST[description]','$_POST[contactEmail]','$_POST[number]')";

"INSERT INTO users (email, password, conpass) VALUES
('$_POST[email]','$_POST[password]','$_POST[confirmPassword]')";

any ide why?

JavaServlet
06-23-2009, 07:59 PM
Hi, I removed the comma but still no luck. I only manage to insert into the flats table.

$sql="INSERT INTO flats (date_posted, type, location, rent, title, image, description, contactEmail, number)
VALUES
(NOW(),'$_POST[type]', '$_POST[location]','$_POST[rent]', '$_POST[title]', '$_POST[image]', '$_POST[description]','$_POST[contactEmail]','$_POST[number]')";

"INSERT INTO users (email, password, conpass) VALUES
('$_POST[email]','$_POST[password]','$_POST[confirmPassword]')";

any ide why?

I assume you have a reason for first statement in a variable and the second one is not. Do you have any error messages after you execute the PHP script? Next step is to test the SQL in the database with test values and see if it works and go from there. For example if using Oracle you can test the statements in SQL Plus.

balamberas
06-24-2009, 07:54 AM
Hi, no error massages. if i would put the user table first then i manage to insert into that table but not the flats table. It will only insert into one table

"I assume you have a reason for first statement in a variable and the second one is not".

I dont know what you meen by this. and i inserted manuanly into the database and its inserting data fine.

any other way to insert data into two tables using one html form??

kurby
06-24-2009, 04:14 PM
If your using php to execute the command then you have to separate the statements. I believe the mysql_query command in PHP can only execute one statement at a time. Just call mysql_query twice.

balamberas
06-24-2009, 05:56 PM
yeah. i solved it today and was going to post the solution. i did this. $sql= INSERT INTO... and then $sql2=INSERT INTO...