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
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)

Link to comment
Share on other sites

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...

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.