繁体   English   中英

如何检查id是否已经存在 - codeigniter

[英]How to check if id already exists - codeigniter

我试图检查数据库中的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);
        }
    }
}

?>
$ql = $this->db->select('id')->from('testing')->where('id',$id)->get();

if( $ql->num_rows() > 0 ) {} else {
    $a = array('id' => $id, 'message' => $message);
    $this->db->insert('testing', $a);
}

这应该做到这一点。

您需要修复的代码存在逻辑问题。

在您的代码中,您将查询的结果保存为$q = $this->db->get('testing') ,并且无论返回的行数如何, $q将始终评估为true。

您需要使用$query->num_rows() > 0检查行数,然后其余的代码将按预期运行。

有关更多详细信息,请参阅: http//ellislab.com/codeigniter/user-guide/database/results.html

您需要在MYSQL表中选择要检查的ID的ID,然后计算行数。 如果行计数为0,则id不存在。

     $query = mysql_query("SELECT * FROM     your_table WHERE id='$id'");
     $count = mysql_num_rows($query);
     If($count!=0){
     // id exists
     } else {
     // id doesn't exist
     }

通常'id'字段使用auto_increment设置并设置primary ,这是唯一且不可重复的。 所以担心现有问题没有问题。

但是,在您的情况下,我认为您并未将其用作“独特字段”。

让我给你举个例子。

我在这里有一个名为'fruits'的表

++++++++++++++++++++++++++++++++++++
ငfruit_id  | int (primary)
name      | text 
id        |  int
++++++++++++++++++++++++++++++++++++++

在你的模型中

function checkId($id)
{
   $query=$this->db->get_where('fruits',array('id'=>$id)); //check if 'id' field is existed or not

   if($query!=null)  // id found stop
   {
     return FALSE; 
   }
   else // id not found continue..
   {
       $data = array(
             'fruit_id' => $fruit_id ,
              'name' => $name ,
             'id' => $id
        );    
      $this->db->insert('fruits', $data);          
   }    
}

对于检查Id或任何不存在于数据库CI中的列值都有一个验证规则。

在此处查看: 验证规则

规则: is_unique

如果表单元素对参数中的表和字段名称不唯一,则返回FALSE。 注意:此规则要求启用查询生成器才能工作。

示例: is_unique[table.field]

$this->form_validation->set_rules(
        'username', 'Username',
        'required|min_length[5]|max_length[12]|is_unique[users.username]',
        array(
                'required'      => 'You have not provided %s.',
                'is_unique'     => 'This %s already exists.'
        )
);

要获得更多高级验证,您可以使用数组添加所有验证设置规则。

        $this->form_validation->set_rules(
            'username', 'Username',
            'required|min_length[5]|max_length[12]|is_unique[users.username]',
            array(
                    'required'      => 'You have not provided %s.',
                    'is_unique'     => 'This %s already exists.'
            )
    );


    $config = array(
        'your_rule_name' => array(
                array(
                        'username', 'Username',
                        'required|min_length[5]|max_length[12]|is_unique[users.username]',
                        array(
                                'required'      => 'You have not provided %s.',
                                'is_unique'     => 'This %s already exists.'
                        )
                )
        ),        
        array(
                'field' => 'email',
                'label' => 'Email',
                'rules' => 'required'
        )
);

$this->form_validation->set_rules($config);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM