New users ask PHP how to obtain POST values in batches and store them in the database!

Source: Internet
Author: User
New users ask PHP how to obtain POST values in batches and store them in the database! $ Data = array ();
$ Data ['A'] = in ($ _ POST ['A']);
$ Data ['B'] = in ($ _ POST ['B']);
$ Data ['c'] = in ($ _ POST ['c]);
$ Data ['D'] = in ($ _ POST ['D']);
$ Data ['A'] = in ($ _ POST ['A']);
$ Data ['A1'] = in ($ _ POST ['A1']);
...................... This is omitted
Model ('initong ')-> update ("id = 1", $ data );

The above is a PHP code for normal database updates.

However, the current problem is that more than 50 fields in this table are required. And image
$ Data ['A'] = in ($ _ POST ['A']); it is unscientific to format a row of input.
Can I obtain post values in batches by field names and update them to the database together?


Reply to discussion (solution)

Foreach ($ _ POST as $ key => $ val) {// it is best to add a key that is not a field in logical filtering. $ data [$ key] = in ($ val );}

Foreach ($ _ POST as $ key => $ val) {// it is best to add a key that is not a field in logical filtering. $ data [$ key] = in ($ val );}

Newbie, can you give me more details? thank you!

$ Fields = array ('A', 'B', 'C', 'D'); // your field foreach ($ fields as $ val) {if (! Empty ($ _ POST [$ val]) {$ data [$ val] = in ($ _ POST [$ val]);}

$ Fields = array ('A', 'B', 'C', 'D'); // your field foreach ($ fields as $ val) {if (! Empty ($ _ POST [$ val]) {$ data [$ val] = in ($ _ POST [$ val]);}

This is clear,
However, on the second floor, you can use another method I have tried. But I don't know what the difference is. which one is better.

First, an error will be reported if psot uploads a key that does not exist in the database.
All fields I want to filter
This method can be used if not all fields are inserted at a time.

In the second case, check whether each field has a value.
This issue can be used if all fields are inserted at a time.

Each has its own advantages. it depends on how users use it.

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.