kevin66 Posted May 7, 2011 Share Posted May 7, 2011 Hi. When I add data to a table from a php form, the new entry is say 7 but the previous one was 2. It skips numbers when adding a new row. Any help please. Can I define the next ID when I submit the data? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/235760-insert-data-needs-correct-id-as-it-skips-ids/ Share on other sites More sharing options...
amolv Posted May 7, 2011 Share Posted May 7, 2011 plz check auto increment attr.. Quote Link to comment https://forums.phpfreaks.com/topic/235760-insert-data-needs-correct-id-as-it-skips-ids/#findComment-1211870 Share on other sites More sharing options...
kevin66 Posted May 9, 2011 Author Share Posted May 9, 2011 Thanks I have looked at this. I can only see examples where it is used when creating tables but not when inserting a new row. Any ideas on how? I am very new to php/mysql. Here is my code so far - $name = $_POST['name']; $email = $_POST['email']; $sql = "INSERT INTO properties SET Band_4_Room='$name', Band_4_Price='$email'"; if (@mysql_query($sql)) { echo('<p>New employee added</p>'); } else { echo('<p>Error adding new employee: ' . mysql_error() . '</p>'); } // This finds the id of the row once it has been added... $id = MYSQL_INSERT_ID(); // Display it... ECHO $id; Quote Link to comment https://forums.phpfreaks.com/topic/235760-insert-data-needs-correct-id-as-it-skips-ids/#findComment-1212827 Share on other sites More sharing options...
fenway Posted May 13, 2011 Share Posted May 13, 2011 You shouldn't be wanting to define a unique number -- that's the database's job. Quote Link to comment https://forums.phpfreaks.com/topic/235760-insert-data-needs-correct-id-as-it-skips-ids/#findComment-1214949 Share on other sites More sharing options...
amolv Posted May 13, 2011 Share Posted May 13, 2011 after addition of one row use maxid in select query will gives you id of an currently added record. Quote Link to comment https://forums.phpfreaks.com/topic/235760-insert-data-needs-correct-id-as-it-skips-ids/#findComment-1214957 Share on other sites More sharing options...
fenway Posted May 13, 2011 Share Posted May 13, 2011 after addition of one row use maxid in select query will gives you id of an currently added record. No, it won't. That's not thread-safe. Quote Link to comment https://forums.phpfreaks.com/topic/235760-insert-data-needs-correct-id-as-it-skips-ids/#findComment-1214959 Share on other sites More sharing options...
amolv Posted May 13, 2011 Share Posted May 13, 2011 http://php.net/manual/en/function.mysql-insert-id.php mysql_insert_id ([ resource $link_identifier ] ) Retrieves the ID generated for an AUTO_INCREMENT column by the previous query (usually INSERT). ie unique id required and it will solve problem. Quote Link to comment https://forums.phpfreaks.com/topic/235760-insert-data-needs-correct-id-as-it-skips-ids/#findComment-1214968 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.