我正在制作插入函数,它接受$ table参数和$ cols(作为数组)参数.它在给定的表中插入给定的值:
$db->query("insert into $table({$cols[0]},{$cols[1]}) values('{$_POST[{$cols[0]}]}','{$_POST[{$cols[1]}]})");
Run Code Online (Sandbox Code Playgroud)
这一切都很好,除了我没有多长时间.这该怎么做??
您还没有做的一件事是使用正确的转义机制来转义SQL.
$postCols = $_POST['cols'];
foreach($postCols as &$col) {
$col = '"' . mysql_real_escape_string($col) . '"';
}
$db->query("insert into $table(" . implode(',', $cols) . ") values(" . implode(',', $postCols . ");
Run Code Online (Sandbox Code Playgroud)