简体   繁体   English

警报主键插入

[英]Alert primary key insert

How to create a primary key alerts when the data input together with CodeIgniter? 当数据与CodeIgniter一起输入时,如何创建主键警报? example if I insert 001 but in the database already exists 001 subsequently appeared alert 例如,如果我插入001但在数据库中已经存在001,则随后出现警报

In your Model you can run an SQL query to see if the primary key is already present. 在模型中,您可以运行SQL查询以查看主键是否已经存在。 If it is, then you can simply return an error code to the Controller, which should handle things from there (by sending an error message to the View). 如果是这样,那么您可以简单地向控制器返回错误代码,该控制器应该在那里处理事情(通过向视图发送错误消息)。

Model 模型

function checkPrimaryKey($primary_key){
    $sql = "SELECT * FROM table_name WHERE primary_key = '$primary_key'";
    /* Replace table_name And primary_key With Actual Table Name And Column Name */
    $query=$this->db->query($sql);
    if($query->num_rows() == 1){
        return -1; //Key already exists
    }
    else{
        return 0;  //Key does not exist
    }
}

Controller 控制者

function checkPrimaryKey($primary_key){
    $this->load->model('Model_name');
    if($this->Model_name->checkPrimaryKey($primary_key)==-1){
        //Appropriate Error Message
    }
    else {
        //Call Some Model Function to Insert The Data And/Or Display Appropriate Success Message
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM