加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

php – 如何检查id是否已存在 – codeigniter

发布时间:2020-12-13 18:14:02 所属栏目:PHP教程 来源:网络整理
导读:我试图检查数据库中的id是否已经存在,如果不存在则只插入该id而不是其他存在的id 我试图做一个where语句,检查它们是否存在于数据库中,但即使它们是新信息,也不会将其插入到数据库中 我在这里很丢失 任何指导将不胜感激 ps我不想更新一行我想插入一个不存在的
我试图检查数据库中的id是否已经存在,如果不存在则只插入该id而不是其他存在的id

我试图做一个where语句,检查它们是否存在于数据库中,但即使它们是新信息,也不会将其插入到数据库中

我在这里很丢失
任何指导将不胜感激

ps我不想更新一行我想插入一个不存在的新更新的行

$this->db->where('id',$id);
$q = $this->db->get('testing');

if($q)
{
    //Do nothing
}
else
{

    $this->db->set('id',$id);
    $this->db->set('message',$message);
    $query= $this->db->insert('testing');

}
模型
<?php
class Fruits_model extends CI_Model
{
    function __construct()
    {
        parent::__construct();
        $this->load->database();
    }

    function check()
    {
        $query = null; //emptying in case 

        $id   = $_POST['id']; //getting from post value
        $name = $_POST['name'];

        $query = $this->db->get_where('fruits',array(//making selection
            'id' => $id
        ));

        $count = $query->num_rows(); //counting result from query

        if ($count === 0) {
            $data = array(
                'name' => $name,'id' => $id
            );
            $this->db->insert('fruits',$data);
        }
    }
}

?>

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读