Php 使用codeigniter删除数据库记录

Php 使用codeigniter删除数据库记录,php,mysql,database,html,codeigniter,Php,Mysql,Database,Html,Codeigniter,好的,这里有点困难,我对codeigniter和PHP都是新手。我想从数据库中删除一条记录。我的控制器出现错误。这是我的密码 控制器: public function removeitem(){ $this->load->helper(array('form', 'url')); $this->load->view('vRemove'); } public function doremove(){ $this->load->m

好的,这里有点困难,我对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  ?>
编辑:在控制器中更改以下代码后,错误不再存在,但现在的问题是数据库中的项目未被删除

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


非常感谢您的帮助。

如果函数
doremove()
$id
未定义,您需要获取资源的
$id

如果HTTP方法是GET,则需要使用
$id=$this->input->GET('u input\u name')

或者如果是POST
$id=$this->input->POST('u input_name')

或者,您可以使用用户友好的方式,在功能范围内传递$id,
doremove($id)
,只需让舒尔正确设置您的URL即可
('site/doremove/$id')

阅读这篇文章,你的代码会有很大的不同。
在此链接中有一个使用codeigniter的简单表单的好例子:

在函数
doremove()
$id
未定义时,需要获取资源的
$id

如果HTTP方法是GET,则需要使用
$id=$this->input->GET('u input\u name')

或者如果是POST
$id=$this->input->POST('u input_name')

或者,您可以使用用户友好的方式,在功能范围内传递$id,
doremove($id)
,只需让舒尔正确设置您的URL即可
('site/doremove/$id')

阅读这篇文章,你的代码会有很大的不同。
在这个链接中有一个使用codeigniter的简单表单的好例子:

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

URI段参数(GET) 通过URI段传递ID。例如:将
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...
<?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>
通过表格(POST) 查看

<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...
<?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 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;
}

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

URI段参数(GET) 通过URI段传递ID。例如:将
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...
<?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>
通过表格(POST) 查看

<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...
<?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 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;
}

粘贴PHP返回的错误..为什么不调试一下代码呢。尝试输出您的$id以查看您是否获得了正确的id。在$id=$this->input->get('ProductID')之后;写入echo$id;模具();如果你得到一个空屏幕,这意味着你没有得到任何id,如果你在屏幕上得到了一些东西,把它与dbpaste的id匹配,PHP返回错误..你为什么不调试一下你的代码呢。尝试输出您的$id以查看您是否获得了正确的id。在$id=$this->input->get('ProductID')之后;写入echo$id;模具();如果你得到一个空屏幕,这意味着你没有得到任何id,如果你在屏幕上得到了一些东西,请将它与DBThank的id匹配。我尝试过使用post和get方法,其中任何一种方法错误不再存在,成功页面出现时没有错误,但项目没有被删除?我假设我的代码中还有一个错误:/谢谢。我尝试过使用post和get方法,其中任何一种方法错误不再存在,成功页面出现时没有错误,但项目没有被删除?我假设我的代码中还有另一个错误:/