Re: validation & inserts not working

From: Date: Wed, 11 Mar 2009 22:49:33 +0000
Subject: Re: validation & inserts not working
References: 1 2 3 4 5  Groups: php.general 
Request: Send a blank email to php-general+get-289903@lists.php.net to get a copy of this message
PJ wrote:
Lex Braun wrote:
PJ,
    <snip>
    
$sql1 = "INSERT INTO book ( title, sub_title, descr, comment, bk_cover, copyright, ISBN, language, sellers ) VALUES ('$titleIN', '$sub_titleIN', '$descrIN', '$commentIN', '$bk_coverIN', '$copyrightIN', '$ISBNIN', '$languageIN', '$sellersIN')"; $result1 = mysql_query($sql1, $db); $autoid = mysql_insert_id($result1);
You're actually sending mysql_insert_id() the wrong parameter. It should be: $autoid = mysql_insert_id($db); // you send the resource of your MySQL connection (http://ca3.php.net/manual/en/function.mysql-insert-id.php) This should be corrected in everywhere you call mysql_insert_id() -Lex
I tried this (from the link above) $result1 = mysql_query($sql1, $db);
    $autoid = mysql_insert_id();
    echo $autoid;
works... but now, I have another problem... I am trying to debug this thing by going 1 query at a time ( I comment out the rest): every time I do an INSERT INTO book... the insert works fine but the ID is increased not from the highest id value but from the last id inserted. So my highest at the moment is 11; I insert test data and it goes in as id = 15; I delete this field and redo an insert and it goes in as id = 16. How can I change this so the auto_increment continues from the 11 ?
I'm not sure you can, and for good reason. MySQL keeps track of last id in an autoincrement even if you deleted the record. You don't want ID clashes even with records that have been deleted. What if they were accidentally deleted and need to be restored from backup? Now you will have an ID conflict because an old record and new record will have same ID. That's why mysql will not use a record id in autoincrement that has been used before, even if it appears to be available.

« previous php.general (#289903) next »