Hello i want to automatically add an id to the inserted row on my table in Mysql db.

The table has no uniquie key and no AUTO_INCREASMENT. I just want a function to add the id automatically when the row is inserted.

This is the statement i have

$db = getDB();
    if(isset($cId))
    {
        $cId = $request->post('cId');
    }
    else
    {
        $cId = '3';**//Here i want to put a new id **
    }

    $insert = array(
        'c_id' => $cId,
        'user_one' => $uid,
        'user_two' => $recieverId,
        'ip' => $_SERVER['REMOTE_ADDR'],
        'time' => date('Y-m-d H:i:s'),
    );

    $sql = "INSERT INTO conversation (c_id,user_one, user_two, ip, time) VALUES (?,?,?,?,?)";
    $stmt = $db->prepare($sql);
    $stmt->execute(array_values($insert));

$request->post('cId'); is returned as wanted

The starting value for AUTO_INCREMENT is 1, which is the default. It will get increment by 1 for each new record. To get the next auto increment id in MySQL, we can use the function last_insert_id() from MySQL or auto_increment with SELECT. Creating a table, with “id” as auto-increment.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.