Php URI段可以';不能保存在数据库中

Php URI段可以';不能保存在数据库中,php,codeigniter,Php,Codeigniter,我试图在数据库表中插入一条新记录。执行插入的函数如下所示: public function comment($id_b,$comment){ $id_u = $this->session->userdata('userid'); $result=$this->db->insert('comments',['id_book'=>$id_b, 'id_user'=>$id_u, 'comment'=>$comment] ); return $result;

我试图在数据库表中插入一条新记录。执行插入的函数如下所示:

public function comment($id_b,$comment){
$id_u = $this->session->userdata('userid');
$result=$this->db->insert('comments',['id_book'=>$id_b, 'id_user'=>$id_u, 'comment'=>$comment] );
return $result;
}
调用
comment
的控制器函数:

public function comment()
{
$this->load->model('model');
$id_b= $this->uri->segment('3');
$comment=$this->input->post('comment');
$this->model->comment($id_b,$comment);
}
我试图获取的uri段是一个
id
。问题是它不能保存在数据库中,它的值总是
NULL

我尝试了
echo$this->uri->segment('3')
并且它实际上返回id,但我不明白为什么它在数据库中保存为
NULL

当我尝试这样做时:
(int)$this->uri->segment('3'),我得到了这个错误:

错误号码:1452

无法添加或更新子行:外键约束失败(
db
comments
,约束
comments\u ibfk\u 1
外键(
id\u book
)引用
book
id\u book

注释
id\u book
id\u user
comments
)中插入值(0,'5','test')


因此,它尝试插入一本带有
id\u book=0
的书,而实际上book表中没有这样的记录,我不知道它从哪里得到
id\u book=0
,因为
$this->uri->segment(3)
返回1。

有一个解决方案,这是从路由到的控制器方法的参数中获取
$id\b
值。根据上面的评论,您的URI字符串似乎是search\u results/result/1。这意味着您要路由到搜索结果控制器中的结果方法<代码>$id_b
为1

在Search_results.php控制器中:

/**
 * Result
 * @param  int $id_b
 */
public function result( $id_b )
{
    // Pass $id_b to the comment method
    // or whatever method calls the comment method
    $this->comment( $id_b );
}

/**
 * Comment
 * @param  int $id_b
 */
public function comment( $id_b )
{
    $comment = $this->input->post('comment');
    $this->load->model('model');

    // Continue passing $id_b to the model
    $this->model->comment( $id_b, $comment );
}
模型中的代码基本上可以保持不变

/**
 * Comment
 * @param  int $id_b
 * @param  string $comment
 */
public function comment( $id_b, $comment )
{
    $id_u = $this->session->userdata('userid');

    return $this->db->insert('comments',[
        'id_book' => $id_b, 
        'id_user' => $id_u, 
        'comment' => $comment
    ]);
}
我无法告诉您为什么在使用
$this->URI->segment(3)
获取URI的第三段时遇到问题,但希望此解决方法允许您继续


如果是我,为了修复URI段问题,我将在/application/config/config.php中更改URI_协议的配置值。在那里时,请确保设置了有效的基本url。在过去的版本中,您不必设置基本url,但CodeIgniter说这现在是强制性的。我从2009年的1.7.2版就开始使用CodeIgniter,从未见过URI段出现问题。如果我的建议没有给您指明正确的方向,我建议您共享一些有关CodeIgniter config.php文件和服务器的信息。

有一个解决方案,那就是从路由到的控制器方法的参数中获取
$id\b
值。根据上面的评论,您的URI字符串似乎是search\u results/result/1。这意味着您要路由到搜索结果控制器中的结果方法<代码>$id_b
为1

在Search_results.php控制器中:

/**
 * Result
 * @param  int $id_b
 */
public function result( $id_b )
{
    // Pass $id_b to the comment method
    // or whatever method calls the comment method
    $this->comment( $id_b );
}

/**
 * Comment
 * @param  int $id_b
 */
public function comment( $id_b )
{
    $comment = $this->input->post('comment');
    $this->load->model('model');

    // Continue passing $id_b to the model
    $this->model->comment( $id_b, $comment );
}
模型中的代码基本上可以保持不变

/**
 * Comment
 * @param  int $id_b
 * @param  string $comment
 */
public function comment( $id_b, $comment )
{
    $id_u = $this->session->userdata('userid');

    return $this->db->insert('comments',[
        'id_book' => $id_b, 
        'id_user' => $id_u, 
        'comment' => $comment
    ]);
}
我无法告诉您为什么在使用
$this->URI->segment(3)
获取URI的第三段时遇到问题,但希望此解决方法允许您继续


如果是我,为了修复URI段问题,我将在/application/config/config.php中更改URI_协议的配置值。在那里时,请确保设置了有效的基本url。在过去的版本中,您不必设置基本url,但CodeIgniter说这现在是强制性的。我从2009年的1.7.2版就开始使用CodeIgniter,从未见过URI段出现问题。如果我的建议没有给您指明正确的方向,我建议您共享一些有关CodeIgniter config.php文件和服务器的信息
应该是整数,检查它是否改变了什么?@cssBlaster21895你是说我应该将
$this->uri->segment(3)
转换成整数?php版本是什么?只有在PHP5.4之后,才能使用短数组语法,它将数组()替换为[]。@Vickel php version->5.6。14@eri您应该将3输入为整数,而不是表示字符串的“3”,并且可以解释为1甚至true:)
$this->uri->segment(3)
应该是整数,检查它是否改变了什么?@cssBlaster21895你是说我应该将
$this->uri->segment(3)
转换成整数?php版本是什么?只有在PHP5.4之后,才能使用短数组语法,它将数组()替换为[]。@Vickel php version->5.6。14@eri您应该将3输入为整数,而不是“3”,这意味着字符串,可以解释为1,甚至可以解释为true:)