insert 100+ variables into query string

2019-03-07 00:10发布

问题:

I have an online survey format with approx 200 input / select fields which I now want to write into my database. Now, since I dont want to go through the tedious process of writing out every single variable one-by-one in the query string, I was hoping that there would be an easier way, eg. using an array?

回答1:

Here is a solution that I use. It requires that the database field names are the same as the <input> names:

$querystring1 = ""; $querystring2 = "";
foreach($_POST as $key => $var) {
    $querystring2 .= "'".$var."',"; $querystring1 .= $key.",";
}   
$insertquerystring = "(".$querystring1.") VALUES (".$querystring2.")";
mysql_query("INSERT INTO `mytablename` $insertquerystring") or die(mysql_error());


标签: php mysqli