0

I have multiple table rows output with values populated from a database.

<tr>
    <td>
        <select name='edit_ativity[]'>
           <option value="11" selected="selected">Aerobic Dancing - Low Impact</option>
           <option value="12">Aerobic Dancing - Strenuously</option>
           <option value="13">Aerobics - High Impact</option>
           <option value="19">Skipping - Slow</option>
        </select>
     </td>
     <td>
          <input type='text' name='edit_duration[]' value='100' />
     </td>
     <td>
          <input type='text' class='datepicker' name='edit_datepicker[]' value='2019-03-01' id=''>
     </td>
</tr>
An other Row 
An other Row 

The user can change the values on each of the row fields then submit the form which updates the database with the new values.

if(isset($_POST['edit'])) {
    foreach ($_POST['edit_ativity'] as $key=>$value) {
        $dist_activity_id = $_POST['edit_ativity'][$key];
        $dist_activity_duration = $_POST['edit_duration'][$key];
        $dist_id = $_POST['distance_id'][$key];
        $dist_date = $_POST['edit_datepicker'][$key];                   
        $stmt = $conn->prepare("UPDATE distance SET dist_activity_id=$dist_activity_id, dist_activity_duration=$dist_activity_duration, dist_date='$dist_date' WHERE id=$dist_id");
        $stmt->execute();
    }

    $stmt->close();
    mysqli_close($conn);
}

What I need help with is only executing the update command none of fields are empty.

5
  • 1
    Can you clarify - do you want to update all rows where none of the fields are empty, or prevent form submission altogether if any fields are empty in the entire table? Commented Mar 25, 2019 at 14:07
  • I don't want to update any fields. I'd like to output a message telling the visitor there are errors. Commented Mar 25, 2019 at 14:18
  • Are you using mysqli or PDO Commented Mar 25, 2019 at 14:18
  • I'm using mysqli Commented Mar 25, 2019 at 14:23
  • Just asking stupidly: what keeps you from checking for that condition? Using isset or empty, this should not be too hard Commented Mar 26, 2019 at 11:07

2 Answers 2

2

Simply do another isset() for all the fields you want to be not empty inside the loop

if(isset($_POST['edit'])) {
    foreach ($_POST['edit_ativity'] as $key=>$value) {
        if (isset($_POST['edit_ativity'][$key],
                  $_POST['edit_duration'][$key],
                  $_POST['distance_id'][$key], 
                  $_POST['edit_datepicker'][$key])
        ){
            $dist_activity_id = $_POST['edit_ativity'][$key];
            $dist_activity_duration = $_POST['edit_duration'][$key];
            $dist_id = $_POST['distance_id'][$key];
            $dist_date = $_POST['edit_datepicker'][$key];                   
            $stmt = $conn->prepare("UPDATE distance SET dist_activity_id=$dist_activity_id, dist_activity_duration=$dist_activity_duration, dist_date='$dist_date' WHERE id=$dist_id");
            $stmt->execute();
        }
    }

    $stmt->close();
    mysqli_close($conn);
}

I should also point out that Your script is wide open to SQL Injection Attack Even if you are escaping inputs, its not safe! Use prepared parameterized statements in either the MYSQLI_ or PDO API's

Assuming you are using the mysqli_ API

This code would protect you against SQL Injection and be easier to read as well.

if(isset($_POST['edit'])) {
    foreach ($_POST['edit_ativity'] as $key=>$value) {
        if (isset($_POST['edit_ativity'][$key], 
                  $_POST['edit_duration'][$key],
                  $_POST['distance_id'][$key], 
                  $_POST['edit_datepicker'][$key])
        ){
              
            $stmt = $conn->prepare("UPDATE distance SET 
                                        dist_activity_id=?,
                                        dist_activity_duration=?, 
                                        dist_date=? 
                                    WHERE id=?");

            $stmt->bind_param('iisi',$_POST['edit_ativity'][$key],
                                     $_POST['edit_duration'][$key],
                                     $_POST['edit_datepicker'][$key],
                                     $_POST['distance_id'][$key]
                            );
            $stmt->execute();
        }
    }

    $stmt->close();
    mysqli_close($conn);
}
Sign up to request clarification or add additional context in comments.

4 Comments

Get the error when updating Fatal error: Uncaught Error: Call to a member function bind_param() on boolean in
Woops I missed a Comma after dist_activity_id=?, try again
There was a missing , after SET dist_activity_id=?
Posted before I refreshed the page.
0

if your trying to validate all the input before updating the values you can do this by:

if(isset($_POST['edit'])) {
    foreach ($_POST['edit_ativity'] as $key=>$value) {
        $dist_activity_id = $_POST['edit_ativity'][$key];
        $dist_activity_duration = $_POST['edit_duration'][$key];
        $dist_id = $_POST['distance_id'][$key];
        $dist_date = $_POST['edit_datepicker'][$key];   
        // Validation//
            // if one or more inputs are empty inside the if wont run//
        if (!empty($dist_activity_id) && !empty($dist_activity_duration) && !empty($dist_id) && !empty($dist_date)) {
            $stmt = $conn->prepare("UPDATE distance SET dist_activity_id=$dist_activity_id, dist_activity_duration=$dist_activity_duration, dist_date='$dist_date' WHERE id=$dist_id");
            $stmt->execute();
        }              

    }

    $stmt->close();
    mysqli_close($conn);
}

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.