【问题标题】:How to check if id already exists - codeigniter如何检查 id 是否已经存在 - codeigniter
【发布时间】:2013-04-20 21:46:11
【问题描述】:

我正在尝试检查数据库中的 id 是否已经存在,如果不存在则只插入该 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');

}

【问题讨论】:

  • 您在运行此程序时是否检查过$q 是什么?
  • $q 检查数据库中是否存在任何 ID,因为它不会向数据库中插入任何新信息
  • 对,但我会验证 $q 实际上是 false。尝试if($q->num_rows() > 0) { ... },然后在if 中输入echo,这样您就可以确定发生了什么。
  • 我在 if 和 else 语句中做了一个回显,它打印在我不知道为什么的两个决定上

标签: php codeigniter


【解决方案1】:

型号

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

?>

【讨论】:

  • 为此,您始终可以使用表单验证规则 is_unique[table.filed_name] 来检查唯一值
【解决方案2】:

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

在您的代码中,您将查询的结果保存为$q = $this-&gt;db-&gt;get('testing'), 无论您返回多少行,$q 的计算结果始终为 true。

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

更多详情请见:http://ellislab.com/codeigniter/user-guide/database/results.html

【讨论】:

    【解决方案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);
    }
    

    应该这样做。

    【讨论】:

      【解决方案4】:

      你应该这样尝试:

      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...
         }
      }
      

      【讨论】:

        【解决方案5】:

        您需要在 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
             }
        

        【讨论】:

          【解决方案6】:

          通常“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);          
             }    
          }
          

          【讨论】:

          • $fruit_id,$name,$id 是您表单中发布的值
          • 我尝试了您的代码,但即使它们是新的 id,它也会将所有结果返回为 false
          • 好的,你必须使用 | num_rows();检查我的下一个答案以获得完整的解决方案
          【解决方案7】:

          用于检查 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);
          

          【讨论】:

            猜你喜欢
            • 2016-12-04
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2019-09-24
            • 1970-01-01
            • 2019-08-21
            • 2019-03-19
            相关资源
            最近更新 更多