insert 100+ variables into query string

2019-03-06 23:28发布

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?

标签: php mysqli
1条回答
等我变得足够好
2楼-- · 2019-03-07 00:10

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());
查看更多
登录 后发表回答