开发者

Automate processing form data

开发者 https://www.devze.com 2023-03-06 00:52 出处:网络
Is there a way to automate (or at least automate some of the process) of extracting data from forms that is stored in the $_POST or $_GET superglobals?

Is there a way to automate (or at least automate some of the process) of extracting data from forms that is stored in the $_POST or $_GET superglobals?

I know you can iterate through the array with a foreach loop and other kinds of loops to do something with each key/value pair, but there are many times where I have other code that needs that data but just won't work when inside a foreach loop at all.

EDIT: Here's an example I can think of. Say I have a whole bunch of posted data and want to insert it into a database. As far as I can see, doing something like this isn't possible since I'd need to insert a value into a different column on each loop through:

foreach($array as $key => $value) {
mysql_query(INSERT INTO table (somecolumn) VALUES($value);
}

Also, another thing I'd really like to do is take all of the values from the posted data and assign each of them to a variable somehow; kind of like what extract() does but without the issue of overwriting other variables. The only thing I can think of to handle this is to somehow put the data into an array, but I'm not sure if this much much more efficient:

$data = array();
foreach($array as $key => $value) {
$data[$key] = $value;
}

One last case I can think of is when I have to use this data outside of the foreach loop. If I modify my code to have it so that everything is executed inside of the foreach loop, then I am restricted to keeping everything that has to do with the form data inside of the loop. 开发者_如何学CIf variables and/or constants are defined inside of a loop, they can't be accessed outside of it, correct? I have been working with Java a lot lately and am used to the more strict scope and strict way of handling variables :)

I've been reading about the extract() method a bit, but it looks like a bit of a dangerous thing to use (overwriting variables, security holes, etc).

For anyone else that processes large amounts of data (lets say 25+ form fields), how do you do it? I'm tired of assigning variables like $data = $_POST['somedata'] over and over to use them elsewhere...

Thanks!


I know you can iterate through the array with a foreach loop and other kinds of loops to do something with each key/value pair, but there are many times where I have other code that needs that data but just won't work when inside a foreach loop at all.

Do you have an example of such cases?

I've been reading about the extract() method a bit, but it looks like a bit of a dangerous thing to use (overwriting variables, security holes, etc).

Yes, do not use that. This was actually used quite a lot in previous versions of PHP, and it caused lots of security issues.

For anyone else that processes large amounts of data (lets say 25+ form fields), how do you do it? I'm tired of assigning variables like $data = $_POST['somedata'] over and over to use them elsewhere...

I generally split lots of form fields into similar functional groups (names, numbers, dates, etc.) and deal with them that way. To be honest I have more of an issue with non-checked data going in my database then with checking lots of form fields.


Get things teed up. In your html form, and your database if you have an element 'email' then make sure that matches up with your table, ie don't have a db table called 'user_email'.

Isolate those items you simply HAVE to validate, check them, add them back to the $_POST array - or fail or abort accordingly.

$_POST['web'] = {do your cleansing here, add any missing http:// kinda thing}

Remove any submit button cruft which may have made its way into your $_POST array.

unset($_POST['submitMe']);

Then do the equivalent of:

$db->saveNew($_POST);

Where $db is a slight layer lying over PDO which automatically and without fail uses Prepared Statements to escape and protect your database.

You could have your $db class pick up an ini file which determines which kind of variable each of your tables expects if you wanted.

0

精彩评论

暂无评论...
验证码 换一张
取 消