mysql_insert_id(); not returning value after successful row insert

后端 未结 1 601
伪装坚强ぢ
伪装坚强ぢ 2020-12-19 12:23

I swear I have poured and poured over every other similar question on this site and others... but I think I\'m just missing something. Hopefully someone can point out a sill

相关标签:
1条回答
  • 2020-12-19 13:05

    Per the documentation:

    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.

    The documentation states that it can only return 0 if the query last executed does not generate an AUTO_INCREMENT value, which should mean that your PRIMARY KEY column in notes is not properly setup with auto_increment. I would recommend double-checking that the PRIMARY KEY column in notes is in-fact setup with auto_increment (never hurts to check again!).

    Viewing your sample code, you do call mysql_insert_id() immediately after insertion, so there shouldn't be any conflicting queries in between to skew the result.

    The only thing I see that may cause an issue is that you're passing the MySQL resource to mysql_query(), but not to mysql_insert_id():

    if (!mysql_query($note_sql,$con))
    ...
    $note_id = mysql_insert_id();
    

    There may be a conflict in the resources due to this. Try updating your call to:

    $note_id = mysql_insert_id($con);
    
    0 讨论(0)
提交回复
热议问题