【问题标题】:Deleting a database record using codeigniter使用 codeigniter 删除数据库记录
【发布时间】:2014-06-11 17:06:43
【问题描述】:

好的,在这里有点挣扎,我对 codeigniter 很陌生,对 PHP 也很陌生。我想从数据库中删除一条记录。我收到控制器错误。这是我的代码

控制器:

    public function removeitem(){
    $this->load->helper(array('form', 'url'));
    $this->load->view('vRemove');
}

public function doremove(){
    $this->load->model("mphoto");

    $this->mphoto->removeItem($id);
    $this->load->view('removed_success');
}

型号:

    public function removeItem($id){
    $this->db->where('ProductID', $id);
    $this->db->delete('Streamline');
}        

和视图:

    <?php echo form_open_multipart('site/doremove');?>

    <p>Are you sure you want to remove this item?</p>

    <a href="<?php echo base_url();?>index.php/site/admin">
      <input type="button" value="Cancel" />
    </a>
    <div><input type="submit" value="Submit" /></div>
    </form>
    <?php  ?>

我得到的错误是:

遇到了 PHP 错误

严重性:通知

消息:未定义变量:id

文件名:controllers/site.php

行号:114

这是哪一行:

    $this->mphoto->removeItem($id);

编辑:更改控制器中的以下代码后错误不再存在,但现在的问题是数据库中的项目没有被删除。

    public function doremove(){
    $this->load->model("mphoto");
    $id = $this->input->get('ProductID');
    $this->mphoto->removeItem($id);
    $this->load->view('removed_success');
}

非常感谢任何帮助。

【问题讨论】:

  • 粘贴PHP返回的错误..
  • 你为什么不调试一下你的代码。尝试输出您的 $id 以查看您是否获得了正确的 ID。 $id = $this->input->get('ProductID'); 之后写回声 $id;死();如果你得到一个空的屏幕,这意味着你没有得到任何 id,如果你在屏幕上得到一些东西,将它与 db 中的 id 匹配

标签: php mysql database html codeigniter


【解决方案1】:

控制器的doremove() 函数中的$id 未定义。您需要将 ID 的值从视图传递给控制器​​;有几种常见的方法可以实现这一点。

URI 段参数(GET)

通过 URI 段传递 ID。例如:http://yousite.com/site/doremove/2 会将2 作为参数传递给site 控制器中的doremove 函数。

查看

<p>Are you sure you want to remove this item?</p>
// Change 2 to the id of the product you want to remove - you can do this dynamically
<a href="echo site_url('site/doremove/2');">Yes</a>
// Add cancel button etc...

控制器

public function doremove($id = null) {
    if ($id === null)
    {
        // The ID isn't set - show an appropriate message, redirect, whatever you want...
    }
    else
    {
        $this->load->model("mphoto");

        // Is the delete operation sucessful?
        if ($this->mphoto->removeItem($id))
            $this->load->view('removed_success');
        else
            $this->load->view('removed_failure'); // Or whatever you want
    }
}

通过表单 (POST)

查看

<?php echo form_open('site/doremove');?>
  <p>Are you sure you want to remove this item?</p>
  // Change 2 to the id of the product you want to remove
  <input type="hidden" name="product_id" value="2" />
  <input type="submit" value="Submit" />
</form>

控制器

public function doremove() {
    $id = $this->input->post('product_id');
    if($id)
    {
        $this->load->model("mphoto");    

        // Is the delete operation sucessful?
        if ($this->mphoto->removeItem($id))
            $this->load->view('removed_success');
        else
            $this->load->view('removed_failure'); // Or whatever you want
    }
    else
    {
         // The ID isn't set - show an appropriate message, redirect, whatever you want...
    }

}

检查对数据库执行的操作是否成功也是一个好主意。

型号

public function removeItem($id) {
    // Attempt to delete the row
    $this->db->where('ProductID', $id);
    $this->db->delete('Streamline');
    // Was the row deleted?
    if ($this->db->affected_rows() == 1)
        return TRUE;
    else
        return FALSE;
}

【讨论】:

    【解决方案2】:

    函数中doremove()$id是未定义的,需要取资源的$id

    如果HTTP方法是GET,则需要使用$id = $this-&gt;input-&gt;get('the_input_name');

    或者如果是POST$id = $this-&gt;input-&gt;post('the_input_name');

    或者您可以使用用户友好的方式,在函数范围内传递 $id,doremove($id),只需确保您的 URL 设置正确 ('site/doremove/$id')

    阅读User Guide,它将对您的代码产生重大影响。 在此链接中有一个使用 codeigniter 的简单表单的很好示例:http://ellislab.com/codeigniter/user-guide/libraries/form_validation.html

    【讨论】:

    • 谢谢。我已经尝试使用 post 和 get 方法,其中任何一个错误不再存在并且成功页面出现时没有错误但该项目没有被删除?我假设我的代码中还有另一个错误:/
    猜你喜欢
    • 1970-01-01
    • 2023-04-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-04
    • 2012-05-16
    • 2011-07-12
    • 2016-12-10
    相关资源
    最近更新 更多