Use Codeigniter to override insert (recommended), codeigniterinsert
A solution exists for the unique index key value when inserting data using the Codeiginter framework
When data is stored, some unique index fields already have values, and data insertion fails. The common solution is to first check whether the value exists, if it exists, it is followed by the new update. If it does not exist, it is insert. The following code overrides the insert method in the Codeigniter model, which greatly simplifies the process!
/***** Insert a single record ** @ param array $ params Insert field and value array * @ param string $ table Insert table Name, insert the primary table * @ param array $ columns insert the array of field names in the table, if this parameter is specified, filter the column array * @ param array $ updateColumns to be updated when the unique key conflicts * @ return mixed FALSE: No inserted field int: insert Record id ** @ throws \ C_DB_Exception */public function insert ($ params, $ table = NULL, $ columns = array (), $ updateColumns = array ()) {// if no table name is specified, the operation if (empty ($ table) is performed on the master table at the Model layer )) {$ Table = $ this-> table; $ columns = $ this-> columns;} // filter out if (! Empty ($ columns) {foreach ($ params as $ column => $ val) {if (in_array ($ column, $ columns )) {$ row [$ column] = $ val ;}}// else {$ row = $ params;} // if (! Isset ($ row) {return FALSE;} // ensure that the if (! Isset ($ row ['create _ at']) & in_array ('create _ at', $ columns )) {$ this-> load-> helper ('common _ function'); $ datetime = get_datetime (); $ row ['create _ at'] = $ datetime; $ row ['Update _ at'] = $ datetime;} // encapsulate the SQL file and filter out unsafe factors. $ SQL = $ this-> db-> insert_string ($ table, $ row); // if the column array needs to be updated when the unique key conflict occurs, the SQL if (! Empty ($ updateColumns) {$ updateValues = ''; foreach ($ updateColumns as $ updateColumn) {if (isset ($ updateValues [0]) {$ updateValues. = ',';} $ updateValues. = $ updateColumn. '= VALUES ('. $ updateColumn. ')';} $ SQL = $ SQL. 'On duplicate key Update '. $ updateValues;} // SQL Execution $ query = $ this-> db-> query ($ SQL); // SQL Execution fails, throw an exception if (FALSE ===$ query) {$ code = $ this-> db-> _ error_number (); $ msg = $ this-> db-> _ error_message (); C_log: Error ($ code. ':'. $ msg, $ this-> user_id, $ SQL, self: STATUS_DB_LOG); throw new C_DB_Exception ('', C_DB_Exception: DB_ERR_CODE );} // write the DB log in SQL. if (LOG_UPDATE_ SQL) {C_log: Info ('', $ this-> user_id, $ SQL, self: STATUS_DB_LOG );} // get the insert ID and return $ id = $ this-> db-> insert_id (); return $ id ;}
If you write SQL directly, there is also a statement
Syntax:Insert into table VALUES () on duplicate key update row = '$ row'
For example:
INSERT INTO it_teacher (uid,name,qq,create_at) VALUES ('$userid','$name','$qq',NOW()) ON DUPLICATE KEY UPDATE name='$name',qq='$qq',phone='$phone'";
The method for rewriting insert using Codeigniter (recommended) above is all the content shared by the editor. I hope you can give us a reference and support for our guests.