Jump to content

autoincrement problem...


xhelle

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/162715-autoincrement-problem/
Share on other sites

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.

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?

Archived

This topic is now archived and is closed to further replies.

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