0

Help please, am trying to validate a form date input not empty and should not be greater that today's date. this is what I did so far. am getting 000-00-00 inserted in MySQL db. what am I doing wrong? here is what in the form input

<div class="form-group">
    <label>Join Date</label>
    <input type="date" name="joindate" class="form-control <?php echo (!empty($joindate_err)) ? 'is-invalid' : ''; ?> " value="<?php echo $joindate ?>">
    <span class="invalid-feedback"><?php echo $joindate_err; ?></span>
</div>

the php tag above has this validations

//date validation
$input_joindate = trim($_POST["joindate"]);

if (empty($input_joindate)) {
    $joindate_err = "Select join date";
}
if (!empty($input_joindate)) {
    $input_joindate = date('Y-m-d', strtotime($input_joindate));
    $today = strtotime("now");
    if (($input_joindate) > $today)
        $joindate_err = "Date should not be in the future";
} else {
    $joindate = $input_joindate;
}
nice_dev
  • 17,053
  • 2
  • 21
  • 35
Y Alhanai
  • 3
  • 1
  • Maybe you should first try to just store a date in the DB using a value statically defined and be sure you can master that part because here I see only the validation stage and it doesn't tell the whole story. For comparing dates you can see a similar SO question [here](https://stackoverflow.com/questions/32642417/php-date-time-greater-than-today) – Diego D Aug 23 '22 at 09:00
  • that part is working if (empty($fullname_err) && empty($joindate_err) && empty($status_err)) { $sql = "INSERT INTO member (fullname, joindate, status) VALUES (?, ?, ?)"; if ($stmt = mysqli_prepare($mydbconn, $sql)) { mysqli_stmt_bind_param($stmt, "sss", $param_fullname, $param_joindt, $param_status); $param_fullname = $fullname; $param_joindt = $joindt; $param_status = $status; if (mysqli_stmt_execute($stmt)) { header("location: list.php"); exit(); – Y Alhanai Aug 23 '22 at 09:10
  • I don't know if it works... you said "_getting 000-00-00 inserted in MySQL db_". Plus I see in your INSERT statement you are pushing the string as is. My suggestion meant to use a static value for the date instead of using the data coming from POST params. Did you try populating the date with a static string in php and just run that statement to see if the row gets correctly inserted? And did you see the other SO question I suggested before describing better how to compare dates instead of using `strtotime`? – Diego D Aug 23 '22 at 09:16
  • the point is that you are focusing on validation but maybe your INSERT would never work even when the validation passed. – Diego D Aug 23 '22 at 09:19
  • thanks for point that from me, I did make a table only on date field and try the script below and worked. – Y Alhanai Aug 23 '22 at 12:40

1 Answers1

0
<?php

$today = date("Y-m-d");
$joindate = $_POST["joindate"];

if (empty($joindate) || !isset($joindate)) {
    $joindate_err = "Select join date";
} elseif (strtotime($today) < strtotime($joindate)) {
    $joindate_err = "Date should not be in the future";
}
francisco
  • 1,387
  • 2
  • 12
  • 23
  • The line `$joindate = $_POST["joindate"]` will throw a notice if 'joindate' is not set in $_POST. And, FYI, `empty()` already checks `isset()`. – Syscall Aug 23 '22 at 15:58
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Aug 27 '22 at 12:56