'a'将带有$_POST['begin']as in 索引的字符串连接到外部$_POST,如下所示:
$_POST['a' . $_POST['begin']]
你的循环看起来像:
// Concatenate a single quote with the $_POST dynamic key, then another single quote and comma.
$values = "'" . $_POST['a' . $_POST['begin']] ."', " ;
// In the loop, same thing concatenating single quotes around the $_POST key
// dynamically built with $i
for ($i = $_POST['begin'] + 1 ; $i < $ietsanders ; $i++){
$values = $values . "'" . $_POST['a' .$i] . "', ";
}
您尝试通过构建$_POST['stuff']as 字符串来执行此操作的方式需要调用eval(),在接受用户输入(如$_POST.
如果您打算将这些值传递$_POST给 SQL 查询,则需要对它们执行一些防止 SQL 注入的保护。如果可能,建议改用支持预处理语句的 API。
最后,不知道你为什么要写信给$_POST超全球。希望您有充分的理由这样做:
$_POST['aantal'] = 4;
$_POST['begin'] = 10;
评论后更新:
好吧,事实证明这个想法是构建文字字符串'$_POST[a10]', '$_POST[a11]', '$_POST[a12]', '$_POST[a13]',而不是从中插入值。为此,字符串应该用单引号引起来,以防止$分隔变量。
// Single quote the string, and escape single quotes inside it
$values = '\'$_POST[a' . $_POST['begin'] . ']\', ';
// In the loop, same thing concatenating single quotes around the $_POST key
// dynamically built with $i
for ($i = $_POST['begin'] + 1 ; $i < $ietsanders ; $i++){
$values = $values . '\'$_POST[a' . $i .']\', ';
}
// After the loop, you have an extra comma and space at the end. trim() it off
$values = trim($values, ', ');
这是一个工作示例