// Not sure how you handled your sql connexion, so I wrote a short example
$linkDB=new mysqli('localhost', 'user', 'pwd', 'db_name');
if($connectDB->connect_error) {
die('Erreur de connexion ('.$connectDB->connect_errno.'): '.$linkDB->connect_error);
exit();
}
$linkDB->set_charset('utf-8');
foreach($_POST as $key=>$value) {
/* Just to be safe, I suggest to manually escape the key,
* to avoid injections. Someone correct me if it's wrong.
*/
$key=mysqli_real_escape_string($linkDB, $key);
$stmt=$linkDB->prepare(/** @lang MySQL */
"INSERT INTO `tablename` (`$key`)
VALUES (?)");
$stmt->bind_param('s', $value);
if($stmt !== false) {
$stmt->execute();
}
$stmt->close();
}
$linkDB->close();
In case you want to use all key/values in the post in one query, then do like this instead.
$queryCols="";
$queryVals="";
foreach($_POST as $key=>$value) {
$queryCols.="`".mysqli_real_escape_string($linkDB, $key)."`,";
$queryVals.="'".mysqli_real_escape_string($linkDB, $value)."',";
}
$queryCols=preg_replace("/(,)$/", "", $queryCols);
$queryVals=preg_replace("/(,)$/", "", $queryVals);
$stmt=$linkDB->prepare(/** @lang MySQL */
"INSERT INTO `tablename` ($queryCols)
VALUES ($queryVals)");
if($stmt !== false) {
$stmt->execute;
}
$stmt->close();
$linkDB->close();
Something like this should do the trick... ;)
INSERT BATCHfor this.$keyequivalent to your SQL column name?