How do you use the variable in $ _POST []

I need to iterate through a bunch of dynamically generated fields, but this does not work:

$population_density = $_POST['$current_location_id']; 

I have a list of places with their population on one page; I need to make sure that you can update them right away. Therefore, I called the field names dynamically matching location_id. When the message is sent, I need to go through them like this, but it looks like you cannot put a variable in the message.

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

Is it possible to put a variable inside $ _POST []? If not, how do I need to update dynamically generated fields?

+4
source share
4 answers

Use it without single quotes:

 $_POST[$current_location_id] 

Now the value of $current_location_id used as a key instead of the string $current_location_id . You can also use $current_location['ID'] directly:

 $_POST[$current_location['ID']] 

Even in your request:

 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']."' "); } } 
+1
source
 $_POST[$var] or $_POST["cst_$var"] 
+2
source

Single quotes forbid variable interpolation in a string; either use double quotes or omit them altogether.

0
source

$ POST ["cst $ var"] - cst_ $ var represents a string

Response to the answer: $ POST ['cSt. $ Var]

0
source

Source: https://habr.com/ru/post/1299277/


All Articles