簡體   English   中英

你如何在$ _POST []中使用變量

[英]How do you use a variable in a $_POST[]

我需要遍歷一堆動態生成的字段,但這不起作用:

$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' ");
    }
}

是否可以將變量放在$ _POST []中? 如果沒有,我該如何更新動態生成的字段?

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

使用它而不使用單引號:

$_POST[$current_location_id]

現在$current_location_id的值用作鍵而不是字符串$current_location_id 您也可以直接使用$current_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']."' ");
    }
}

單引號禁止字符串中的變量插值; 使用雙引號或完全省略它們。

$ POST [“cst $ var”] - cst_ $ var表示一個字符串

核心答案是:$ POST ['cst '。$ var]

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM