我想让下面的内容正常工作。我让其他人正常工作,但我在if部分遇到问题。它正在做的是删除行并在hottest_cat
行中插入0。它应该插入$dd
的值。关于我做错了什么的想法?
public function change_category() {
$dd = $this->input->post('dd');
$sql = $this->db->get('default_hottest_cat');
if ($sql->num_rows() > 0) {
$row = $sql->row();
$old = $row->hottest_cat;
// Stuff is found in this table. Needs to be deleted first, then inserted.
$this->db->delete('default_hottest_cat', array('hottest_cat' => $old));
$this->db->insert('default_hottest_cat', array('hottest_cat' => $dd));
} else {
// Nothing is found in this table. Needs only to be inserted.
$this->db->insert('default_hottest_cat', array('hottest_cat' => $dd));
}
}
答案 0 :(得分:0)
你和我回答的问题非常相似。 请查看此信息以获取更多信息 How to check if id already exists - codeigniter
逻辑是
1.Select "the database table" with the value you had posted.(search)
2.Check the count of result_row(if found >0 , if not found =0)
3.If not found 'insert', if found 'update',( in your case delete)
模型
<?php
class Cars_model extends CI_Model
{
function __construct()
{
parent:: __construct();
$this->load->database();
}
function check()
{
$query=null; //emptying in case
$dd= $_POST['dd']; //getting from post value
$query = $this->db->get_where('default_hottest_cat', array('dd' => $dd)); ၊၊၊။။//making selection
$count= $query->num_rows(); //counting result from query
if ($count === 0) // data is not present
{
$data = array(
'dd' => $dd
);
$this->db->insert('default_hottest_cat', $data);
}
else //data is present
{
$this->db->delete('default_hottest_cat', array('dd'=>$dd));
}
}
}
?>