Forum Moderators: coopster

Message Too Old, No Replies

Inserting data into 2 tables using 1 form

         

whitewidow

10:41 pm on Sep 20, 2009 (gmt 0)

10+ Year Member



Hey everyone, I am hoping someone can help me with my problem.

I have a simple form that users can fill out with 4 different input fields and I would like to know how to insert this data into 2 seperate tables.

I think there maybe a few questions here but as I am a newcomer to this I may not be able to explain exactly what the problem is in great detail or ask for the right questions.

Anyway, here goes...

I have 2 tables: 'user' and 'blog' - each has 2 data columns (user: firstName and lastName)(blog: subject and content) and each has a primary key with auto-inc enabled.

The input fields on the form are named accordingly and it is set to $POST the data and "action" a process.php page.

My process page looks like this:

<<<php start>>>
$username = "root";
$password = "";
$hostname = "localhost";

### --- connect to database and select it --- ###
mysql_connect($hostname,$username,$password);
mysql_select_db("test");

### --- My insert code--- ###
$sql="INSERT INTO user (firstName, lastName) VALUES ('$_POST[firstName]','$_POST[lastName]');
INSERT INTO blog (subject, content) VALUES ('$_POST[subject]','$_POST[content]')";

if (!mysql_query($sql))
{
die('Error: ' . mysql_error());
}
echo "1 record added";

mysql_close()
<<<php end>>>

I have seen an example on this forum (http://www.webmasterworld.com/forum88/322-1-10.htm) using a similar INSERT query but it starts - $query="INSERT etc

Which should I use ($query or $sql) or do they both do the same thing?

My code must be wrong as it errors saying "check syntax".
I have tried the following instead to see what happened but it only enters data into 1 of the tables (blog):

$sql="INSERT INTO user (firstName, lastName) VALUES ('$_POST[firstName]','$_POST[lastName]')";
$sql="INSERT INTO blog (subject, content) VALUES ('$_POST[subject]','$_POST[content]')";

If anyone can shed any light on this I would greatly appreciate it.

Thanks in advance

Pico_Train

3:19 pm on Sep 21, 2009 (gmt 0)

10+ Year Member



You can do this:

$sql="INSERT INTO user (firstName, lastName) VALUES ('$_POST[firstName]','$_POST[lastName]')";
$sql1="INSERT INTO blog (subject, content) VALUES ('$_POST[subject]','$_POST[content]')";

if (!mysql_query($sql))
{
die('Error: ' . mysql_error());
}
echo "1 record added";

if (!mysql_query($sql1))
{
die('Error: ' . mysql_error());
}
echo "2 records added";

notice the $sql and $sql1 differentiation.

whitewidow

5:28 pm on Sep 21, 2009 (gmt 0)

10+ Year Member



Hi Pico_Train and thanks for your reply. I have got this sorted now. Looking at your reply I now see where I was going wrong and understand that the IF query is what is actualy doing the work and the $sql was just defining the string. Or at least I think that's it.
Many thanks!

Pico_Train

11:28 am on Sep 22, 2009 (gmt 0)

10+ Year Member



Sure thing...