1

Basically I am trying to update my database with multiple values. Input fields are in a loop, so I need to update multiple checkboxes and textboxes at the same time.

$cityID = $_POST["cityID"];
$cityStatus = $_POST["cityStatus"];
$cityOrder = $_POST["cityOrder"];  

$updateCities = $db->execute("UPDATE cities SET city_status=?, city_order=? WHERE city_ID=$cityID", array($city_status, $cityOrder));

foreach($cities as $row) :
?>

<input type="checkbox" name="cityStatus[]" value="1">
<input type="text" name="cityOrder[]" value="<?php echo $row->city_order ?>">
<input type="hidden" name="cityID" value="<?php echo $row->city_ID; ?>">
6
  • Whats the question?Any errors? Commented Apr 18, 2015 at 18:17
  • why are you injecting $cityID directly in your query, when you are using placeholders for the others? Commented Apr 18, 2015 at 18:20
  • Input fields are in a loop, so I am trying to update multiple checkboxes at the same time. Commented Apr 18, 2015 at 18:23
  • if this is in a loop, then you have multiple name="cityID". Should it be name="cityID[]" Commented Apr 18, 2015 at 18:27
  • 2
    You need to give all your inputs the same key, as only checked checkboxes are posted on form submit, so in order to know which one is checked to access the other fields, they all need to be the same, ie. name="cityStatus[$row->city_ID]"/name="cityOrder[]"/name="cityID[$row->city_ID]". Then you can loop over each checkbox using the key - foreach($_POST['cityStatus'] as $key => $value) { $_POST['cityStatus'][$key]; $_POST['cityOrder'][$key]; $_POST['cityID'][$key]; Commented Apr 18, 2015 at 18:33

1 Answer 1

1

What i understood is 1. you have array of 3 fields (cityid, cityorder, city status) 2. you are submitting all these values from the form 3. you want update cityorder and citystatus depends on cityid

The solutions is pretty simple

$i=0;
foreach($_REQUEST['cityOrder']) as $cityorder){

$cityID = $_POST["cityID"][$i];
$cityStatus = isset($_POST["cityStatus"][$i])?$_POST["cityStatus"][$i]:0;
$cityOrder = $_POST["cityOrder"][$i];  

$updateCities = $db->execute("UPDATE cities SET city_status=?, city_order=? WHERE city_ID=$cityID", array($city_status, $cityOrder));

$i++;
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.