All the data is stored in the $_POST special array. You would need to iterate over it and build an insert query.
Long ago, I wrote functions to handle database tasks. Here's the one to insert data from an array into a (PostgreSQL) database. You need to make a few edits to make it work with MySQL.
Code:
function db_insert($conn,$table,$input_array) {
# insert a record into $table using connection $conn
# all values in $input_array with keys that match
# fieldnames will be inserted
$get_types=pg_Exec($conn,"select * from $table limit 1;");
$nf=pg_NumFields($get_types);
$vars="";
$vals="";
# traverse the fields looking for fieldnames
# that match an index in $input_array
for ($i=0; $i<$nf; $i++) {
$type=pg_FieldType($get_types,$i);
$name=pg_FieldName($get_types,$i);
# ignore fields without matching input key
if (!isset($input_array[$name])) {
continue;
} else {
$value=$input_array[$name];
}
# handler for null entries ($input_array[$field]='')
# postgres allows set var=null statements
if (empty($value) && !is_numeric($value)) {
$value="null";
}
# coerce values if appropriate
if ($type=="bool" && ($value || $value="true" || $value="True" ||
$value=="t" || $value=="T" || $value=="Y")) {
$value='t';
} else if ($type=="bool") {
$value='f';
}
# escape any quotes in text fields with double quotes
if (preg_match("/char/i",$type) || preg_match("/text/i",$type)) {
if ($value=="null") {
$value="''";
} else if (strstr($value,"'")) {
$value="'".str_replace("'","''",trim($value))."'";
} else {
$value="'".trim($value)."'";
}
}
# build the insert query lists
$vars .= $name.",";
$vals .= $value.",";
}
# lop off trailing commas and compose the insert query
$query="insert into $table (".substr($vars,0,-1).") values(".substr($vals,0,-1).");";
$insert=@pg_Exec($conn,$query);
return $insert;
}
So if I had this function defined in my code (I maintain a PHP class that I require() in every script), I could run the command:
Code:
$myinsert=db_insert($conn,$sometablename,$_POST);
and the contents of the $_POST array would be searched for matching keys and the associated values stored in $sometablename in the database.
PHP comes with its own libraries of functions to handle tasks like this called
PEAR. (These came out after I wrote my own functions.) Here is the function to insert data:
https://pear.php.net/manual/en/packa...ect.insert.php
Writing PHP scripts from scratch that use a database like MySQL often requires you to have at least a basic knowledge of SQL.
Bookmarks