F. Schulte F. Schulte - 1 year ago 61
SQL Question

Insert auto incrementing variable names into database

I have the following code that replaces {input:text} with an actual input field:

while (strpos($setCode, '{input:text}') !== false)
$setCode = preg_replace('/{input:text}/', '<input class="bookTextInput" type="text" name="ANT'. $i++ . '" />', $setCode, 1);

What I want is to insert the values of all generated input fields into SQL:

$AntCount = substr_count($string, '{input:text}');

for( $i= 1 ; $i <= $AntCount ; $i++ ) {

$query = "INSERT INTO TableName (Answer) VALUES ('" . $ANT.$i . "')";
mysqli_query($link, $query);

With this code only numbers are being inserted into the database ($i). The $ANT is being cancelled. I would like to insert the values of ANT1, ANT2 etc.

It seems that $ANT.$i doesnt work, is there a way to insert the automatically generated input field values in a way like this?

Answer Source

ANT is form field so is in POST, right?

So not: $ANT.$i

But: $_POST['ANT'.$i]