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.