One SQL statement updates multiple records
Obtain two arrays from the form. one is to update the field data and the other is the conditional array. here I use id Source: http://www.liuhai.org/sql-set-key-case-when-then
- Function save_category_district ($ parame, $ id ){
- If (! Is_array ($ parame) |! Is_array ($ id) return false;
- $ SQL = "UPDATE". table. 'Category _ district '. 'set ';
- Foreach ($ parame as $ key => $ val ){
- $ Csql. = $ key. '= CASE id ';
- Foreach ($ parame [$ key] as $ key => $ val ){
- $ Csql. = "WHEN {$ id [$ key]} THEN '{$ val}'"; // note that the key of the id must be the same as the key of the data to be updated, is the number key in the form.
- }
- $ Csql. = 'end ,';
- }
- $ SQL. = rtrim ($ csql, ','); // This step is used to remove the last END comma.
- $ Id = implode (',', $ id); // The id is changed to a string here.
- $ SQL. = "WHERE id IN ({$ id })";
- If (! $ This-> mydb-> query ($ SQL) return false;
- Return true;
- }
|