Jump to content

MySQL_Insert_Id


Xtremer360

Recommended Posts

How can you debug to find out why it's not getting the mysql_insert_id number? I have it echoed all the queries and all with all the correct values from my form but the only problem is that its not getting the insert id number of the id.

 

$query1 = "INSERT INTO `efed_bio` (charactername,username,posername,style_id,gender,status_id,division_id,alignment_id,sortorder) VALUES ('".$charactername."','".$username."','".$posername."','".$style."','".$gender."','".$status."','".$division."','".$alignment."','".$sort."')";
            mysql_query($query1);
            $query1_id = mysql_insert_id(); 
            echo $query1;
            echo $query1_id;
            $query2 = "INSERT INTO `efed_bio_allies` (bio_id) VALUES (".$query1_id.")";
            mysql_query($query2); 
            echo $query2;
            $query3 = "INSERT INTO `efed_bio_rivals` (bio_id) VALUES (".$query1_id.")";
            mysql_query($query3);  
            echo $query3;
            $query5 = "INSERT INTO `efed_bio_singles` (bio_id) VALUES (".$query1_id.")";
            mysql_query($query5);  
            echo $query5;

Link to comment
https://forums.phpfreaks.com/topic/214878-mysql_insert_id/
Share on other sites

perhaps...

 

 

$query1 = "INSERT INTO efed_bio (charactername, username, posername, style_id, gender, status_id, division_id, alignment_id, sortorder) VALUES ('$charactername', '$username', '$posername', '$style', '$gender', '$status', '$division', '$alignment', '$sort')";$result1 = mysql_query($query1);$query1_id = mysql_insert_id(); echo $query1;echo $query1_id;$query2 = "INSERT INTO efed_bio_allies (bio_id) VALUES ('$query1_id')";$result2 = mysql_query($query2); echo $query2;$query3 = "INSERT INTO efed_bio_rivals (bio_id) VALUES ('$query1_id')";$result3 = mysql_query($query3); echo $query3;$query5 = "INSERT INTO efed_bio_singles (bio_id) VALUES ('$query1_id')";$result5 = mysql_query($query5); echo $query5;

 

Link to comment
https://forums.phpfreaks.com/topic/214878-mysql_insert_id/#findComment-1117918
Share on other sites

How can you debug code that doesn't bother to check if what its doing is working? Echo'ing SQL isn't going to help. As chintansshah suggested, read the manual entry and check for unexpected return values.

 

 

Return Values

 

The ID generated for an AUTO_INCREMENT column by the previous query on success, 0 if the previous query does not generate an AUTO_INCREMENT value, or FALSE  if no MySQL connection was established.

 

 

$sql = "INSERT INTO..."; // snipif(!$query = mysql_query($sql)) {    // mysql_query returned FALSE    echo "Error: a query failed on line ".__LINE__.". ".mysql_error();    exit;}if(!$insert_id = mysql_insert_id()) {    // mysql_insert_id returned FALSE    echo "Error: no MySQL connection was established.";    exit;} elseif($insert_id == 0) {    // mysql_insert_id returned 0    echo "Error: the previous query did not generate an AUTO_INCREMENT value.";    exit;}

 

Link to comment
https://forums.phpfreaks.com/topic/214878-mysql_insert_id/#findComment-1117922
Share on other sites

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.