如何检查id是否已经存在 - codeigniter [英] How to check if id already exists - codeigniter

查看:132
本文介绍了如何检查id是否已经存在 - codeigniter的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图检查数据库中的id是否已经存在,如果它不插入那个id而不是其他的存在

i am trying to check if an id in the database already exists and if it does not then only insert that id and not the other ones that exist

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

I have tried to do a where statement that checks if their is a id exists in the database but even if their are new information it does not insert it into the database

Im相当丢失这里
任何指导都会感激。

Im quite lost here any guidance would be appreciated

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

ps i dont want to update a row i want to insert a new updated one that does not exist

            $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);
        }
    }
}

?>

这篇关于如何检查id是否已经存在 - codeigniter的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆