Jump to content

Insert into two tables


V

Recommended Posts

I searched and couldn't find a solution for this..

 

I have a table which stores author information like name, email, website..

 

and another table that stores a post's data like title, content, date posted

 

I'm inserting values in those tables via a form but I don't know how to insert  into both tables using that same form. What I have now is

 

 

mysql_select_db("moviecollection", $con);

$insert="INSERT INTO authors (name, email, website)
VALUES
('".$name."', '".$email."', '".$website."')";

////////////////////////////////////////////////////////////insert into second table


$insert="INSERT INTO post (title, img, desc, cat_id)
VALUES
('".$title."', '".$img."', '".$desc."', '".$cat_id."')";

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

mysql_close($con)

 

the error I get is

 

Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'desc, cat_id) VALUES ('title', 'img', 'desc', '3')' at line 1

 

I can add data in phpmyadmin without any errors but not via the form.  Am I doing this totally wrong?  :-\

 

Link to comment
https://forums.phpfreaks.com/topic/202547-insert-into-two-tables/
Share on other sites

mysql_select_db("moviecollection", $con);

$insert="INSERT INTO authors (name, email, website)
VALUES
('".$name."', '".$email."', '".$website."')";

if (!mysql_query($insert,$con))
  {
  die('Error: ' . mysql_error());
  }
echo "1 record added to table 1<br />";

////////////////////////////////////////////////////////////insert into second table
$insert="INSERT INTO post (title, img, desc, cat_id)
VALUES
('".$title."', '".$img."', '".$desc."', '".$cat_id."')";

if (!mysql_query($insert,$con))
  {
  die('Error: ' . mysql_error());
  }
echo "1 record added to table 2";

mysql_close($con)

Thank you for replying! I tried your code and I'm able to insert data into the first table but I get the error for the second table

 

1 record added in authorError: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'desc, cat_id) VALUES...

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.