7

我正在尝试检查数据库中的 id 是否已经存在,如果不存在则只插入该 id 而不是其他存在的

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

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

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');

}
4

7 回答 7

11

模型

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

?>
于 2013-04-21T00:44:43.637 回答
3

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

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

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

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

于 2013-04-20T23:04:29.367 回答
3
$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);
}

这应该这样做。

于 2013-04-20T22:38:34.037 回答
1

您需要在 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
     }
于 2013-04-20T22:24:20.503 回答
1

通常“id”字段设置为auto_increment并设置唯一且不可重复的主要字段。所以不存在担心存在的问题。

但是,就您而言,我认为您没有将其用作“独特领域”。

让我给你举个例子。

在这里,我有一个表名“水果”

++++++++++++++++++++++++++++++++++++
င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);          
   }    
}
于 2013-04-20T22:35:41.810 回答
1

你应该这样尝试:

public function record_exists(){
   $exists = $this->db->get_where('table_name', array('id' => $id));
   if($exists->num_rows() > 0 ){
       echo "Some message";
       return false;
   }else{
      // Insert your data into the database...
   }
}
于 2019-10-11T15:50:01.337 回答
0

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

在这里看到它:Validation Rule

规则: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);
于 2017-07-11T05:00:33.547 回答