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
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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.