检索名称中包含另一个变量的变量

检索名称中包含另一个变量的变量

问题描述:

Dunno if the title makes sense, but I have a variable which would to put it in basic terms would be called like this:

$_POST['something'+$variable2]

I have a form which is for editing selected records, this form contains entries for all previously selected records:

<form name="input" action="editcar.php" method="POST">
                    <input type="submit" value="Yes">

    while($row = mysqli_fetch_assoc($result)) 
                    {
                    echo'
</div>     
                    <table style="color:white">
                        <tr>
                            <td style="text-align:right">Manufacture:</td><td><input type="text" name="manufacture'.$row['carIndex'].'" value="'.$row['make'].'"></td> 
                            <td style="text-align:right">Model:      </td><td><input type="text" name="model'.$row['carIndex'].'" value="'.$row['model'].'"></td>
                        </tr>
                        <tr>
                            <td style="text-align:right">Colour:     </td><td><input type="text" name="colour'.$row['carIndex'].'" value="'.$row['colour'].'"></td>
                            <td style="text-align:right">Reg:        </td><td><input type="text" name="reg'.$row['carIndex'].'" value="'.$row['Reg'].'"></td>
                        </tr>
                        <tr>
                            <td style="text-align:right">Price:      </td><td><input type="text" name="price'.$row['carIndex'].'" value="'.$row['price'].'"></td>
                            <td style="text-align:right">Mileage:    </td><td><input type="text" name="mileage'.$row['carIndex'].'" value="'.$row['miles'].'"></td>
                        </tr>
                        <tr>
                            <td style="text-align:right">Max MPH:    </td><td><input type="text" name="mph'.$row['carIndex'].'" value="'.$row['mph'].'"></td>
                            <td style="text-align:right">MPG:        </td><td><input type="text" name="mpg'.$row['carIndex'].'" value="'.$row['mpg'].'"></td>
                        </tr>   
                    </table>

                </form>

            </div> ';

                }
                ?>
                </form> 

The form is looped for each record previously chosen, to enable mass editing. The isue arouses when I realised I'd have multiple inputs with the same name, so I did:

<input type="text" name="model'.$row['carIndex'].'" value="'.$row['model'].'">

Placing the primary key of the record it was currently tired to on the end of it's name. Which seemed like a logical way to go about things.

However now I need to call these variables to place in the mysql query and I dunno how to do that, or even if I can.

I have the selected records saved in an array so I have:

foreach ($postid as $carID) 
   {  
      $query = "stuff";
      mysqli_query($db, $query);
   } 

Each loop has $carID containing the variables that was put on the end of the form input names.

So something like:

$_POST['something'+$variable2]

is all I can think of but doesn't work.

Any method that works for my overall code is welcome not just a solution to the issue I've made.

Actually your way should work. Just replace the + with . in $_POST['something'+$variable2]. My tip is: use an array as name in your html instead:

<input type="text" name="model[]" value="'.$row['model'].'">

On php-Side you can loop through all $_POST['model'] since its an array now.

You can add the index for every entry in your html, too:

<input type="text" name="model['.$row['carIndex'].']" value="'.$row['model'].'">

PHP uses a dot for concatenation, not + like Java and Javascript:

 $_POST['something' . $variable2]

Try something like this:

<form ...>

<?php
 while($row = mysqli_fetch_assoc(...):
   $index =  $row['carIndex'];

?>

<input type="text" name="carmodel[<?php echo $index?>][model]" value="<?php echo $row['model'] ?>">

<?php endforeach; ?>

</form>

This way you will have the data stored in $_POST['carmodel'] as an array indexed by carIndex value as the structure of data in $_POST is defined by names of inputs, here you will have names likee carmodel[1][model] for example so then in post it will be in $_POST['carmodel'][1][model]

you can read here as well How would I create this array structure in an HTML form?