Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/268.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何在$\u POST[]中使用变量_Php_Mysql_Post - Fatal编程技术网

Php 如何在$\u POST[]中使用变量

Php 如何在$\u POST[]中使用变量,php,mysql,post,Php,Mysql,Post,我需要遍历一组动态生成的字段,但这不起作用: $population_density = $_POST['$current_location_id']; 我在一页上列出了一个地点及其人口的列表;我需要这样做,这样你可以一次更新很多。因此,我让字段名动态地对应于location_id。提交帖子时,我需要像这样迭代它们,但似乎无法在帖子中放入变量 for ( $y_count = 1 ; $y_count <= $zone_height; $y_count++ ) { for ( $

我需要遍历一组动态生成的字段,但这不起作用:

$population_density = $_POST['$current_location_id'];
我在一页上列出了一个地点及其人口的列表;我需要这样做,这样你可以一次更新很多。因此,我让字段名动态地对应于location_id。提交帖子时,我需要像这样迭代它们,但似乎无法在帖子中放入变量

for ( $y_count = 1 ; $y_count <= $zone_height; $y_count++ ) {
    for ( $x_count = 1 ; $x_count <= $zone_width; $x_count++ ) {
        $result = mysql_query("SELECT * FROM locations WHERE location_zone='$zone_id' AND x_location='$x_count' AND y_location='$y_count' ");
        $current_location = mysql_fetch_array( $result );
        $current_location_id = $current_location['ID'];
        $population_density = $_POST['$current_location_id'];
        $result = mysql_query("UPDATE locations SET population_density='$population_density' WHERE ID='$current_location_id' ");
    }
}

对于($y\u count=1;$y\u count不使用单引号:

$_POST[$current_location_id]
现在,
$current\u location\u id
的值被用作键,而不是字符串
$current\u location\u id
。您也可以直接使用
$current\u location['id']

$_POST[$current_location['ID']]
即使在您的查询中:

for ( $y_count = 1 ; $y_count <= $zone_height; $y_count++ ) {
    for ( $x_count = 1 ; $x_count <= $zone_width; $x_count++ ) {
        $result = mysql_query("SELECT * FROM locations WHERE location_zone='$zone_id' AND x_location='$x_count' AND y_location='$y_count' ");
        $current_location = mysql_fetch_array( $result );
        $result = mysql_query("UPDATE locations SET population_density='".$_POST[$current_location['ID']]."' WHERE ID='".$current_location['ID']."' ");
    }
}

for($y\u count=1;$y\u count单引号禁止在字符串中插入变量;可以使用双引号,也可以将它们全部省略。

$POST[“cst$var”]-cst\u$var表示字符串

正确的答案是: $POST['cst.$var]

$_POST[$var] or $_POST["cst_$var"]