2013-04-20 113 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); 
相關問題