cs.punk Posted March 17, 2009 Share Posted March 17, 2009 $con = mysqli_connect ("$dbhost","$dbuser","$dbpass","$dbname") or die ("Could not connect to server"); $query = "INSERT INTO polls (title, question, author, date, a0, a1, a2, a3, a4, a5, a6, a7, a8, a9) VALUES ('$title', '$question', '$author', '$date', '$a[0]', '$a[1]', '$a[2]', '$a[3]', '$a[4]','$a[5]', '$a[6]', '$a[7]', '$a[8]', '$a[9]')"; $result = mysqli_query($con, $query) or die ( mysqli_error($result) ); $query2 = "SELECT id FROM polls WHERE title = '$title'"; $result2 = mysqli_query($con,$query2) or die ("Couldn’t execute query." . mysql_error()); $row = mysqli_fetch_row($result2); extract($row); $query3 = "INSERT INTO 'poll_ratings' (a0, a1, a2, a3, a4, a5, a6, a7, a8, a9, id) VALUES ('$a[0]', '$a[1]', '$a[2]', '$a[3]', '$a[4]', '$a[5]', '$a[6]', '$a[7]', '$a[8]', '$a[9]', '$id')"; $result3 = mysqli_query($con,$query3) or die ("Couldn’t execute query3." . mysql_error()); Thats some messy coding I know... But what I am trying to do is get the ID from the previous insert command so I can put it into another table called 'poll_ratings'. Link to comment https://forums.phpfreaks.com/topic/149789-select-id-from-previous-insert-and-insert-into-new-table/ Share on other sites More sharing options...
Floydian Posted March 17, 2009 Share Posted March 17, 2009 $insert_id = mysql_insert_id(); Put that after the insert query in question. Hope that helps. Link to comment https://forums.phpfreaks.com/topic/149789-select-id-from-previous-insert-and-insert-into-new-table/#findComment-786559 Share on other sites More sharing options...
cs.punk Posted March 21, 2009 Author Share Posted March 21, 2009 LoL thanks! Although with mysqli it should be $insert_id = mysqli_insert_id($con); //$con varible is the link identifier, when you connect into MYSQL But I am stuck... My second query won't excute for some reason.. $con = mysqli_connect ("$dbhost","$dbuser","$dbpass","$dbname") or die ("Could not connect to server"); $query = "INSERT INTO polls (title, question, author, date, a0, a1, a2, a3, a4, a5, a6, a7, a8, a9) VALUES ('$title', '$question', '$author', '$date', '$a[0]', '$a[1]', '$a[2]', '$a[3]', '$a[4]','$a[5]', '$a[6]', '$a[7]', '$a[8]', '$a[9]')"; $result = mysqli_query($con, $query) or die ( mysqli_error($result) ); $insert_id = mysqli_insert_id($con); $query2 = "INSERT INTO 'poll_ratings' (a0, a1, a2, a3, a4, a5, a6, a7, a8, a9, id) VALUES ('$a[0]', '$a[1]', '$a[2]', '$a[3]', '$a[4]', '$a[5]', '$a[6]', '$a[7]', '$a[8]', '$a[9]', '$insert_id')"; $result2 = mysqli_query($con,$query2) or die ("Couldn’t execute query2." . mysql_error()); echo "You poll has been added"; I get the "Couldn’t execute query2." error... I remember reading about mysqli_multiple_query but thats for running two queries in one varible if I remember correctly! Link to comment https://forums.phpfreaks.com/topic/149789-select-id-from-previous-insert-and-insert-into-new-table/#findComment-790296 Share on other sites More sharing options...
PFMaBiSmAd Posted March 21, 2009 Share Posted March 21, 2009 Your die() statement is using mysql_error() instead of mysqli_error(), which would prevent you from getting the error back from mysql. Procedural style: string mysqli_error ( mysqli $link ) Once you fix that, you will get an error about your table name because it is enclosed in single-quotes, making it a string, not an identifier. Link to comment https://forums.phpfreaks.com/topic/149789-select-id-from-previous-insert-and-insert-into-new-table/#findComment-790304 Share on other sites More sharing options...
cs.punk Posted March 22, 2009 Author Share Posted March 22, 2009 Thanks allot dude! It has been sorted! Your a genius lol Link to comment https://forums.phpfreaks.com/topic/149789-select-id-from-previous-insert-and-insert-into-new-table/#findComment-790839 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.