检查数据库是否有相同的条

时间:2014-05-26 09:59:20

标签: php sql validation database-abstraction

我在数据库中添加了一个条目,我需要检查它是否已经有一个条目。我正在使用MVC

查看

<form action="<?php echo base_url(); ?>some_controller/insertServ_idx" method="post">
<center>Service Name: <input type="text"  name="ci_name"/>
    <input type="submit" class="classname" value="Save"/></center><br>

控制器

public function index_addServ(){
    /* A pop up dialog will appear containing the details on how to add a new service
     */

        $data['current_user']=$this->session->userdata('email');
        $appname = $this->input->post('appname');
        $data['people'] = $this->some_model->getPeople();
        $data['mapList'] = $this->some_model->getMapped();
        $data['serv'] = $this->some_model->getServiceApp($appname);
        $data['appServList'] = $this->some_model->getApp_serv();
        $this->load->view('templates/header.php',$data);
        $this->load->view('some_page/index_addServ.php',$data);


}

模型

public function addCI($ci_name){
    /* Adds a new service
     */
    $ci_name = $this->db->escape_str($ci_name);

    $queryStr = "INSERT INTO appwarehouse.service(service) VALUES ('$ci_name');"; 
    $query = $this->db->query($queryStr);
    return $query;
}

我知道我需要添加另一个select语句来检查是否已有行。但是如何?

3 个答案:

答案 0 :(得分:0)

您应首先将唯一标识符作为索引,例如$ci_name。确保它是独一无二的。 然后添加手册中的ON DUPLICATE KEY UPDATE语法。这样,您就不必运行2个查询并将其全部完成。

修改

通常在数据库表中至少有一个last_updated或类似的时间戳字段。这样你的完整查询看起来就像这样:

"INSERT INTO appwarehouse.service(service) VALUES ('$ci_name') 
ON DUPLICATE KEY UPDATE last_updated=" + time();

有关详细信息,请参阅here

答案 1 :(得分:0)

对于快速信息,我想到了两个想法。

1)使字段唯一

2)通过比较检查它是否已经存在(糟糕的方法,但能够)

public function addCI($ci_name){
    /* Adds a new service
     */
    $ci_name = $this->db->escape_str($ci_name);

    $queryStr = "INSERT INTO appwarehouse.service(service) VALUES ('$ci_name')";//if feild is unique then this query will throw some exception or error, w/e just check it if there is error or exception then due to uniqueness then simply do w/e you want 
if (mysql_errno()) {
    die('do what ever you wanna do');// coming here means query sent an error and there is a duplicate entry if assuming your query have no syntax error.

}else{
    $query = $this->db->query($queryStr);}
    return $query;
}

其他方式就是

2)

     $ci_name = $this->db->escape_str($ci_name);

        $queryStr = "Select value from table where value= ('$ci_name')";
        $query = $this->db->query($queryStr);
        if(mysql_num_rows($query)>0){
           echo "result already exists";
         }else{
$queryStr = "INSERT INTO appwarehouse.service(service) VALUES ('$ci_name');"; 
        $query = $this->db->query($queryStr);
}

答案 2 :(得分:0)

public function addCI($ci_name){
/* Adds a new service
 */
$ci_name = $this->db->escape_str($ci_name);

$queryStr = "INSERT INTO appwarehouse.service(service) VALUES            ('$ci_name')";//if feild is unique then this query will throw some exception or   error, w/e just check it if there is error or exception then due to uniqueness then simply do w/e you want 
  if (mysql_errno()) {
die('do what ever you wanna do');// coming here means query sent an error   and there is a duplicate entry if assuming your query have no syntax error.

 }else{
$query = $this->db->query($queryStr);}
return $query;
   }