Hi,
I just want to check if the below is possible and if so, is there any obvious reason it is not working.
Basically I am creating text inputs in a form with a loop:
$numroutes = count($route_array);
for($i=0; $i<$numroutes; $i++){?>
<p><input type="text" name="<?php echo 'location'.$i?>" id="<?php echo 'location'.$i?>" value="<?php echo $route_array[$i]?>" /></p>
<?php }?>
You can see that I am setting the input name and id dynamically because I don’t know how many input’s there will be.
This is all working. I then want to update a database table with the new text input text when the form is submitted:
$locationresult = mysql_query("SELECT id FROM wp_travel_route_locations WHERE route_id='$id' ORDER BY id ASC");
$location_array = array();
while($locationrow = mysql_fetch_assoc($locationresult)){
$location_array[] = $locationrow['id'];
}
$numroutes = count($location_array);
for($i=0; $i<$numRoutes; $i++){
$locationname = 'location'.$i;
$locationid = $location_array[$i];
$route_location = $_POST[$locationname];
$query = "UPDATE wp_travel_route_locations SET route_location='$route_location' WHERE id='$locationid'";
}
$locationname should now be the same as the input form field name.
The only reason I can see that it wouldn’t be working is if I am not allowed to do this $route_location = $_POST[$locationname];
Can anyone give me some insight?
Thanks
Pst variables should be;
$route_location = $_POST[$locationname];
to be used like this;
$route_location = $_POST[‘locationname’];
Even though $locationname is a string variable?
Are you saying that I can’t pass a variable into the POST? it has to be a string?
you want to pass this $locationname value in the sql query?
I am aren’t I?
Here:
SET route_location='$route_location'
When passing values from $_POST or $_GET to a variable you should do like:
if (isset($_POST[$locationname])) {
$route_location = $_POST[$locationname];
} else {
$route_location = ''; // set a default value
}
if you don’t depending on what level of error reporting you’ve got set you’ll get Notice level errors. Are you sanitizing the values from $_POST before letting them anywhere near the database?
What does “not working” mean? What is it doing (or not doing)?
If that is really all the code in the for loop, it won’t do anything because you don’t do anything with $query after you set it.
The end of the for loop and function:
$query = “UPDATE wp_travel_route_locations SET route_location=‘$route_location’ WHERE id=‘$locationid’”;
$result = mysql_query($query);
}
if (!$result) {
echo “Could not update location. “. mysql_error();
} else {
header(“Location:”.$locationstring.”?status=successfully edited in the database&route=”.$route_location);
}
}
“Could not update location.”
This is what I receive when I run the UPDATE code
I am sorry to have wasted everyones time but it was very much an obvious mistake. I must have been looking at my code too long and could no longer see it.
$numroutes = count($location_array);
for($i=0; $i<$numRoutes; $i++){
Notice:
$numroutes
and
$numRoutes
Sorry.
Thanks