dpi96151 2014-08-11 13:57
浏览 8

从另一个表中分配ID

In my database, I have 3 tables.

Jokes:

CREATE TABLE IF NOT EXISTS `jokes` (
  `joke_id` int(11) NOT NULL AUTO_INCREMENT,
  `joke` varchar(1024) NOT NULL,
  `category_id` int(11) NOT NULL,
  `vote` int(255) NOT NULL,
  `date_added` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (`joke_id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;

Category:

CREATE TABLE IF NOT EXISTS `category` (
  `category_id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(51) NOT NULL,
  PRIMARY KEY (`category_id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;

And finally, Comments:

CREATE TABLE IF NOT EXISTS `comments` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(40) NOT NULL,
  `comment` text NOT NULL,
  `joke_id` int(11) NOT NULL,
  `post_id` int(11) NOT NULL,
  `date` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;

insertComment function assignes the values and sends to the model:

public function insertComment(){
    if($this->input->post('act') == 'add-com'){
    $name = $this->input->post('user-com');
    $comment = $this->input->post('user-com');


    $this->comments_m->insertComment($name, $comment);


    }
}

The model function below grabs the data from the html, and places it an array.

//inserts the comments
function insertComment (){

    extract($_POST);
    if($_POST['act'] == 'add-com'){
        $data = array(
            'user' => htmlentities($user),
            'comment' => htmlentities($comment),
            'id_post' => $id_post = "1",
            //need to assign the joke_id to this string to fecth comments from specific joke
            'joke_id' => "1"

        );

        if(strlen($data['user']) <= '1'){
            $data['user'] = 'Guest';
        }

        $this->db->insert('comments', $data);
    }
}

My Question is, what can I do to alter the joke_id's assignment value to the actual joke_id so the comment for that joke will be uniquely stored for that idenity. At the moment it is "1" for test purposes, and because i can't get past the assignment for it.

  • 写回答

2条回答 默认 最新

  • dsyo9700 2014-08-11 14:03
    关注

    Most SQL databases increment the ID automatically when inserting a new row, so there should not be a need to do it yourself. But if you need to, just retrieve the last record in the table and set the ID to +1.

    评论

报告相同问题?

悬赏问题

  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分