Jump to content

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?

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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