2013-04-20 111 views
6

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

} 
+0

您是否检查过运行此命令时是否存在'$ q'? – matthewpavkov 2013-04-20 21:51:59

+0

$ q检查数据库中是否存在任何id,因为它不会向数据库中插入任何新信息 – Hashey100 2013-04-20 21:55:25

+0

正确,但我会验证$ q实际上是“false”。试试'if($ q-> num_rows()> 0){...}'然后在你的'if'中做一个'echo',这样你就可以看到发生了什么。 – matthewpavkov 2013-04-20 22:03:27

回答

9

型号

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

?> 
+2

为此,您始终可以使用表单验证规则is_unique [table.filed_name]来检查唯一值 – MSN 2015-07-05 12:59:07

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 
    } 
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);   
    }  
} 
+0

$ fruit_id,$ name,$ id是从您的表单中发布的值 – 2013-04-20 22:59:55

+0

我试过了您的代码,但它将所有结果返回为假即使他们是一个新的ID – Hashey100 2013-04-20 23:04:40

+0

好吧得到它的工作,你必须使用| NUM_ROWS();检查我的下一个答案以获得完整的解决方案 – 2013-04-21 00:41:04

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

这应该这样做。

3

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

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

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

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

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); 
相关问题