skip to Main Content

I have a reservation system in PHP & MySQL with custom running numbers.

The format is like this: XXX01, XXX02, XXX03 etc’

Every day the format reset to zero and the XXX is also change.
For this reason I can’t use auto increment to create the format.

The problem begins that sometimes reservation are created at exactly same second, becuse of that there is a duplication in the reservation number (Like two reservation with the ID XXX03).
Does anyone have any idea how to solve this?
Thanks.

Edit – This is the PHP code that create the ID (codeignitor 3):

$this->CI->db->select('reservation_inside_id');
$this->CI->db->where('DATE(reservation_insert_time)',date("Y-m-d"));
$this->CI->db->from($this->table);
$this->CI->db->order_by('reservation_inside_id DESC');
$this->CI->db->limit(1);
       
$query = $this->CI->db->get();
$row = $query->result();
if (isset($row[0]->reservation_inside_id)) {
    $some_string = THE_FORMAT; //irrelevant
    $inside_id = $some_string.$row[0]->reservation_inside_id;
    $inside_id++;
    return $inside_id;
} else {
    return NEW_ID //here is OK
}

2

Answers


  1. Maybe try calling the getLastInsertReservation() method before saving to the database. This method would ask MySQL for the last inserted row and only on this basis you could make it for yourself.

    Login or Signup to reply.
  2. To get the last inserted ID in a database using CodeIgniter’s database library, you can use the insert_id() method of the database object. Here’s an example:

    // Insert a new record into the database
    $data = array(
        'name' => 'John',
        'email' => '[email protected]'
    );
    $this->db->insert('users', $data);
    
    // Get the last inserted ID
    $last_id = $this->db->insert_id();
    

    In this example, we insert a new record into the users table, then use the insert_id() method to retrieve the ID of the newly inserted record. The $last_id variable will contain the ID value.

    Note that the insert_id() method returns the ID of the last inserted record for the current database connection, so if you have multiple connections, you should make sure to use the correct one. Also, this method may not work for all database drivers, so you should check the CodeIgniter documentation for your specific driver to make sure it is supported.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search