<?php
$_POST['aantal'] = 4;
$_POST['begin'] = 10;
$iets = $_POST['aantal'] + $_POST['begin'];
$ietsanders = $iets - 1;
$values = "'$_POST[a" . $_POST['begin'] . "]', " ;
for ($i = $_POST['begin'] + 1 ; $i < $ietsanders ; $i++){
$values = $values . "'$_POST[a" .$i. "]', ";
}
$values = $values."'$_POST[a" . $ietsanders . "]', ";
echo "using ".$values;
?>
This is my code; what’s wrong? It gives me an error at:
$values = $values . "'$_POST[a" .$i. "]', ";
and:
$values = $values."'$_POST[a" . $ietsanders . "]', ";
It gives me no error when I leave away the ' at '$_POST. I want my for loop to give me '$_POST[a$i]' every run:
'$_POST[a10]', '$_POST[a11]', '$_POST[a12]', '$_POST[a13]',
Concatenate the string
'a'with$_POST['begin']as in index to the outer$_POST, as in:Your loop looks then like:
The way you were attempting to do it by building up the
$_POST['stuff']as strings, would require a call toeval(), which is not to be done under most any circumstances when accepting user input like$_POST.If you are intending to pass these values from
$_POSTto a SQL query, you will need to be performing some protection against SQL injection on them. If possible, it is recommended to switch to an API supporting prepared statements instead.Finally, Not sure why you are writing to the
$_POSTsuperglobal. Hopefully you have a good reason to do this:Update after comments:
Well, it turns out the idea is to build the literal string
'$_POST[a10]', '$_POST[a11]', '$_POST[a12]', '$_POST[a13]',rather than to interpolate the values from it. To do that, the string should be single-quoted to prevent the$from delimiting variables.Here’s a working example