dongshan4549 2014-04-17 22:55
浏览 35
已采纳

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

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.

  • 写回答

3条回答 默认 最新

  • duancaiyi7567 2014-04-17 23:01
    关注

    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'].'">
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥40 图书信息管理系统程序编写
  • ¥15 7-1 jmu-java-m02-使用二维数组存储多元线性方程组
  • ¥20 Qcustomplot缩小曲线形状问题
  • ¥15 企业资源规划ERP沙盘模拟
  • ¥15 树莓派控制机械臂传输命令报错,显示摄像头不存在
  • ¥15 前端echarts坐标轴问题
  • ¥15 ad5933的I2C
  • ¥15 请问RTX4060的笔记本电脑可以训练yolov5模型吗?
  • ¥15 数学建模求思路及代码
  • ¥50 silvaco GaN HEMT有栅极场板的击穿电压仿真问题