One SQL statement updates multiple records

Source: Internet
Author: User
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

  1. Function save_category_district ($ parame, $ id ){
  2. If (! Is_array ($ parame) |! Is_array ($ id) return false;
  3. $ SQL = "UPDATE". table. 'Category _ district '. 'set ';
  4. Foreach ($ parame as $ key => $ val ){
  5. $ Csql. = $ key. '= CASE id ';
  6. Foreach ($ parame [$ key] as $ key => $ val ){
  7. $ 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.
  8. }
  9. $ Csql. = 'end ,';
  10. }
  11. $ SQL. = rtrim ($ csql, ','); // This step is used to remove the last END comma.
  12. $ Id = implode (',', $ id); // The id is changed to a string here.
  13. $ SQL. = "WHERE id IN ({$ id })";
  14. If (! $ This-> mydb-> query ($ SQL) return false;
  15. Return true;
  16. }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.