valoukh Posted March 8, 2008 Share Posted March 8, 2008 hey all. I'm trying to insert data into two tables at once and I'm not really sure what I'm doing wrong. When I submit the data, it only gets inserted into the News table; the Gigs one is ignored. Thanks in advance for any help! <? $sql="INSERT INTO Gigs (gigdate, venue, venueaddress, info, date, gigtime) VALUES ('$_POST[gigdate]','$_POST[venue]','$_POST[venueaddress]','$_POST[info]', '$_POST[date]','$_POST[gigtime]')"; $sql="INSERT INTO News (title, content, date) VALUES ('New gig added!','Denby Grace will be at $_POST[venue] on $_POST[gigdate]. See the Gigs page for more info!','$_POST[date]')"; if (!mysql_query($sql,$dbh)) { die('Error: ' . mysql_error()); } echo "Gig added, thanks!"; mysql_close($dbh) ?> Link to comment https://forums.phpfreaks.com/topic/95081-insert-into-multiple-mysql-tables-only-1-works/ Share on other sites More sharing options...
BlueSkyIS Posted March 8, 2008 Share Posted March 8, 2008 you never run mysql_query() on the first $sql before you set it to another value. for 2 different SQL statements, you'll need to call mysql_query() twice, once for each SQL statement. Link to comment https://forums.phpfreaks.com/topic/95081-insert-into-multiple-mysql-tables-only-1-works/#findComment-487037 Share on other sites More sharing options...
valoukh Posted March 8, 2008 Author Share Posted March 8, 2008 Thanks for your speedy response! I tried it like this but I'm not sure I understand the difference (this didn't work either): mysql_query("INSERT INTO Gigs (gigdate, venue, venueaddress, info, date, gigtime) VALUES ('$_POST[gigdate]','$_POST[venue]','$_POST[venueaddress]','$_POST[info]', '$_POST[date]','$_POST[gigtime]'"); mysql_query("INSERT INTO News (title, content, date) VALUES ('New gig added!','Denby Grace will be at $_POST[venue] on $_POST[gigdate]. See the Gigs page for more info!','$_POST[date]'"); Link to comment https://forums.phpfreaks.com/topic/95081-insert-into-multiple-mysql-tables-only-1-works/#findComment-487041 Share on other sites More sharing options...
BlueSkyIS Posted March 8, 2008 Share Posted March 8, 2008 $sql="INSERT INTO Gigs (gigdate, venue, venueaddress, info, date, gigtime) VALUES ('$_POST[gigdate]','$_POST[venue]','$_POST[venueaddress]','$_POST[info]', '$_POST[date]','$_POST[gigtime]')"; if (!mysql_query($sql,$dbh)) { die('Error: ' . mysql_error()); } $sql="INSERT INTO News (title, content, date) VALUES ('New gig added!','Denby Grace will be at $_POST[venue] on $_POST[gigdate]. See the Gigs page for more info!','$_POST[date]')"; if (!mysql_query($sql,$dbh)) { die('Error: ' . mysql_error()); } echo "Gig added, thanks!"; mysql_close($dbh) Link to comment https://forums.phpfreaks.com/topic/95081-insert-into-multiple-mysql-tables-only-1-works/#findComment-487043 Share on other sites More sharing options...
valoukh Posted March 8, 2008 Author Share Posted March 8, 2008 now I understand! it works like a charm. Thanks so much for your help. Link to comment https://forums.phpfreaks.com/topic/95081-insert-into-multiple-mysql-tables-only-1-works/#findComment-487047 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.