Sometimes we feel boring to write mysql insertion query to store data into database from a large form where are too many input fields (such as about 50 fields). We face problem to handle all the fields, sometimes we miss the field name. Here is a small trick to store data from such a large form.
In this trick, we have to do same naming of table fields and input fields. For example â€“
Then the field name of the database will be â€œfirst_nameâ€
Then the php code will be:
Suppose, name of the submit button is submit [html][/html]
safe_entry($_POST); // here obj is the object; safe_entry is a custom function which confirms the input validation of XSS and mysql injection; otherwise you can use $data = safe_entry($_POST); or simply use $data = $_POST;
$field = $value = array(); //two blank array in which field names and field values will be stored
foreach($data as $key=>$val)
if($key != â€˜submitâ€™)
$fields = implode(â€˜,â€™, $field);
$values = implode(â€œâ€˜, â€˜â€, $value);
$sql = â€œinsert into tablename($fields) values(â€˜$valuesâ€™)â€;
$query = $obj->insertQuery($sql); //use direct function mysql_query() in stead of custom function and object
Thatâ€™s it! This code will store data from a large form. Thanks.