Forum Moderators: coopster
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
$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.