Jump to content

SELECT id from previous INSERT and INSERT into new table


cs.punk

Recommended Posts

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

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!

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.

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.