Simplified PHP $_POST data handling
When posting data from a form to a page, the $_POST variable gets set to an array similar to this:
Array ([name] => "daniel", [state] => "illinois", [phone] => "630-618-9588")
Now, one way to process this is to know in advance (which, normally you will), what the values will be, and set them to more usable variables. For example:
$newclient["name"] = $_POST["name"]; $newclient["state"] = $_POST["state"]; $newclient["phone"] = $_POST["phone"];
Now, for a simple form that only has one or two values, that’s fine. The code is clean, easy to read, and it gets the job done. Now, if you have a more complex form, the array may look more like this:
Array ( [lastname] => costalis [firstname] => daniel [MI] => g [lastnameguardian] => kolar [firstnameguardian] => maureen [MIguardian] => p [Month] => 10 [Day] => 05 [Year] => 1983 [Age] => [address1] => 445 n ardmore ave #k [address2] => [city] => villa park [state] => IL [zip] => 60181 [phone1] => 6306189588 [phone2] => [phone3] => 6306189588 [email] => costalis.dan@gmail.com [feet] => 5 [inches] => 10 [pounds] => 165 [hair] => Brown [eyes] => Green [union] => Yes [experience] => None [skills] => Jumping [notes] => Great Guy [submit] => Add Client )
You can copy and paste 31 times, and edit all the values if you want… or, write something that will do it for you. (read: copy and paste my code and edit it to your needs) This particular set of code is designed to update a mysql database, but can be modified to your liking. the “safedata” function is to prevent hacking of your database.
function safedata( $string ) {
return "'" . mysql_real_escape_string( $string ) . "'"; //add needed slashes,
enclose data in quotes.
}
foreach ($_POST as $field => $value) //Count through each post item
{
$fields[] = $field; //add current field name to fields array
$values[] = $value;//add current field value to value array. Will have same index
as its field name
}
$query = "INSERT INTO `SomeTable` ("; //Start of MySql query
foreach ($fields as $fieldname) //for each field name
$query .= "`" . $fieldname . "`,"; //add field to list in query. safedata not
needed... you supplied the names
$query = rtrim($query,","); //trim off extra comma after final value
$query .= ") VALUES("; //close off field names, begin listing values
foreach ($values as $valuename) //for each value corresponding to its field
$query .= safedata($valuename) . ","; //add value to list
$query = rtrim($query,","); //again, we have an extra comma
$query .= ") ;"; //end query string
$result = mysql_query($query); //excecute query
// created by : http://www.whathuhstudios.com/press : attribution may be removed and
is not required
You could easily use this to assign the values to an array of your liking, or whatever you like. Either way, this is a nice easy way to deal with large form submissions without the use of a PHP framework. Do what you want with this code, and if you happen to include a link to my site on your site, or leave me a comment… then so be it. Hope this helps someone.
| 2.5 |
Post Info
This entry was posted on Wednesday, May 7th, 2008 and is filed under development, php. It has 646 viewsComments Feed. | Leave Comment | Leave Trackback.
Previous Post: Blackberry Flip Phone! »
Next Post: Flash is open source… nothing has changed. »













May 23rd, 2008 01:53
Great article!Thank you!
June 13th, 2008 16:33
do you know any information about this subject in other languages?