-2

我正在向数据库中添加一个条目,我需要检查它是否已经有一个条目。我正在使用 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 语句来检查是否已经有行。但是怎么做?

4

3 回答 3

0

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

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);
}
于 2014-05-26T10:25:31.710 回答
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();

请参阅此处了解更多信息

于 2014-05-26T10:03:08.800 回答
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;
   }
于 2015-05-17T21:54:09.480 回答