php - how to auto increment variable

720

I need some quick help, I need to know if I can somehow increment the $user_id by one so that I do not get a duplicate entry error. Do I just add a +1 after ['user_id']?

 public function insert_video($video_id,$user_id,$caption) {
                $userObject = array();
                $userObject['user_id'] = $user_id;
                $userObject['video_id'] = $video_id;
                $userObject['caption'] = str_replace(array("'",'"'), '', $caption);
                $userObject['description'] = '';
                $userObject['votes'] = 0;
                $userObject['upload_time'] = date('Y-m-d H:i:s');
                $userObject['version'] = $this->contests->contest->version; //4
                //if($this->contests->contest->auto_approve)
                $userObject['approved'] = 1;
                $this->db->insert('photos', $userObject);
                return $this->db->insert_id();

ok guys I edited the function and removed any mention of user_id but for some reason I still get the error when I try to submit another video. Should I try dropping that column on the table? here is my updated function

public function insert_video($video_id,$caption) {
    $userObject = array();
    //$userObject['user_id'] = $user_id+1;
    $userObject['video_id'] = $video_id;
    $userObject['caption'] = str_replace(array("'",'"'), '', $caption);
    $userObject['description'] = '';
    $userObject['votes'] = 0;
    $userObject['upload_time'] = date('Y-m-d H:i:s');
    $userObject['version'] = $this->contests->contest->version; //4
    //if($this->contests->contest->auto_approve)
    $userObject['approved'] = 1;
    $this->db->insert('photos', $userObject);
    return $this->db->insert_id();
}
55

Answer

Solution:

You need to set up youruser_id To auto increment. If you find it easier as you clearly have a table already set up, use phpMyAdmin to set the AI tick box to true.

Then when you INSERT, you'll get a unique value. You do not need to specify theuser_id as that'll be taken care of.

INSERT INTO users (FirstName,LastName) VALUES ('Matt','HB')
264

Answer

Solution:

I suppose you need to have 2 tables, one that holds a user info with auto incresment column e.g. "ID". In table photos don't use auto incresment for user_id, as you need to give users ability to insert multiple photos and update current. just change in your table:

  `user_id` to `ID` -> this will auto incresment 
    create new column `user_id` int 200.. this will be a reference of `column - "ID"` from  **userinfo** table;
      //then run your query:
    INSERT INTO photos (user_id, video_id, caption, description, votes, upload_time, version, approved) VALUES (7339, 'fDTm1IzQf-U', 'new test', '', 0, '2014-11-12 16:17:36', '19', 1)

People are also looking for solutions to the problem: php - Laravel redirect after anything

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.