1

I have 3 text fields and a hidden field. The text field accepts a date. If the date entered is later than today for any text field, it should display an error message. This only works properly if I enter a later date in the first text field. If I do it in the second or third, it continues to the redirect which obviously shouldn't happen.

$count = count($_POST['complete_date']);
        for($i = 0; $i < $count; ++$i) {

        if($_POST['complete_date'][$i] > date('Y-m-d')) {
            echo error_message("Date can't be in the future");
            break;

            } else {


$stmt = $link->prepare("UPDATE `units` SET `complete_date` = ? WHERE `units` = ?");
        $stmt->bind_param("si", $_POST['complete_date'][$i], $_POST['units'][$i]);
        $stmt->execute();
        $stmt->close();
        header("location: dashboard.php");
        exit();

    }
} 

1 Answer 1

1

That's normal, if you have an error in the second or third position, you still execute your query for the first item of the array. You should do something like that :

$count = count($_POST['complete_date']);
$error = false;
    for($i = 0; $i < $count && ! $error; ++$i) {

    if($_POST['complete_date'][$i] > date('Y-m-d')) {
        echo error_message("Date can't be in the future");
        $error = true;

        } 
   }

  //Only process if there are no errors in all the dates
  if(! $error){
  $stmt = $link->prepare("UPDATE `units` SET `complete_date` = ? WHERE `units` = ?");
    $stmt->bind_param("si", $_POST['complete_date'][$i],$_POST['units'][$i]);
    $stmt->execute();
    $stmt->close();
    header("location: dashboard.php");
    exit();

}
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.