xhelle Posted June 18, 2009 Share Posted June 18, 2009 Hi to all, I have this code that will generate id, but the problem is I need to follow the first number in the existing id number and everytime I run my code, I generate a large id like this 1245302265407. This the code: // get artist id $sql = "SELECT id, name FROM artist WHERE name like '%$name%' "; $query = mysql_query($sql); $rows = mysql_num_rows($query); if($rows<1){ echo 'No Existing Artist ID for Artist '.$name; }else{ $record = mysql_fetch_array($query); //insert to db $id = time().rand(100,999); $sql2 = "INSERT INTO inbox (id, artist_id, date_in, content, preview, picture) VALUES( '".$id."' ,'".$record["id"]."' ,'".date("Y-m-d")."' ,'".$message."' ,'".$message."' ,'".$fname."' ) "; if(mysql_query($sql2)){ echo 'Data Inserted'; } } How I can minimize the value of my id and the generated id must be followed the existing number on the table. Thanks in advance... Quote Link to comment https://forums.phpfreaks.com/topic/162715-autoincrement-problem/ Share on other sites More sharing options...
joel24 Posted June 18, 2009 Share Posted June 18, 2009 you should let MySQL generate the id's... set the ID column to be primary key & auto_increment id INT NOT NULL AUTO_INCREMENT, Quote Link to comment https://forums.phpfreaks.com/topic/162715-autoincrement-problem/#findComment-858692 Share on other sites More sharing options...
xhelle Posted June 18, 2009 Author Share Posted June 18, 2009 Hi joel, The id in my table was already set into id INT NOT NULL AUTO_INCREMENT. The problem is during I run my code it generate a large number and it not follow in the existing problem, is there a way or code that generate the number and follow the existing number on the table. Thanks again. Quote Link to comment https://forums.phpfreaks.com/topic/162715-autoincrement-problem/#findComment-858698 Share on other sites More sharing options...
joel24 Posted June 18, 2009 Share Posted June 18, 2009 are you doing that just so the id's don't start at 1? i.e. they start at say 10000? if so you can use id INT NOT NULL AUTO_INCREMENT = 10000, on your table which will make the auto increment start at 10000? otherwise you'd have to get a php script which looks at the last inserted value... and adds one to that? Quote Link to comment https://forums.phpfreaks.com/topic/162715-autoincrement-problem/#findComment-858712 Share on other sites More sharing options...
xhelle Posted June 18, 2009 Author Share Posted June 18, 2009 Hi joel, Thanks for the reply, my auto increment has a large number. Now my problem is fix. Thanks again. Quote Link to comment https://forums.phpfreaks.com/topic/162715-autoincrement-problem/#findComment-858749 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.