如何自动增加变量

时间:2014-11-12 21:08:02

标签: php mysql

我需要一些快速帮助,我需要知道我是否能以某种方式将$ user_id增加一个,这样我就不会出现重复的输入错误。我只是在[' user_id']之后添加+1?

 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();

好的家伙我编辑了这个函数并删除了对user_id的任何提及,但由于某种原因,当我尝试提交另一个视频时,我仍然收到错误。我应该尝试将该列放在桌子上吗?这是我更新的功能

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();
}

2 个答案:

答案 0 :(得分:0)

您需要设置user_id自动增量。如果你发现已经设置了一个表,你会发现它更容易,使用phpMyAdmin将AI勾选框设置为true。

然后当你INSERT时,你将获得一个唯一的值。您无需指定user_id,因为它将被处理。

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

答案 1 :(得分:0)

我想你需要有2个表,其中一个表包含一个包含auto incresment列的用户信息,例如" ID&#34 ;. 在表格照片中,不要为user_id使用自动创建,因为您需要为用户提供插入多张照片和更新当前内容的功能。只需改变你的表格:

  `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)