V Posted May 21, 2010 Share Posted May 21, 2010 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? :-\ Quote Link to comment https://forums.phpfreaks.com/topic/202547-insert-into-two-tables/ Share on other sites More sharing options...
premiso Posted May 21, 2010 Share Posted May 21, 2010 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) Quote Link to comment https://forums.phpfreaks.com/topic/202547-insert-into-two-tables/#findComment-1061819 Share on other sites More sharing options...
V Posted May 21, 2010 Author Share Posted May 21, 2010 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... Quote Link to comment https://forums.phpfreaks.com/topic/202547-insert-into-two-tables/#findComment-1061821 Share on other sites More sharing options...
premiso Posted May 22, 2010 Share Posted May 22, 2010 desc is a reserved mysql word. Either encase it in back ticks ( ` ) or rename that column to a non-mysql reserved word. Quote Link to comment https://forums.phpfreaks.com/topic/202547-insert-into-two-tables/#findComment-1061857 Share on other sites More sharing options...
V Posted May 22, 2010 Author Share Posted May 22, 2010 wow out of all the possible names I chose a mysql word.. Thanks for sharing! It works now Quote Link to comment https://forums.phpfreaks.com/topic/202547-insert-into-two-tables/#findComment-1061860 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.