I am trying to insert a datetime into my MySQL DB that adds a certain amount of time depending on what is selected from the form. For example, clicking the button on 2021-04-01 22:00:00
and selecting “1 day” would result in an entry in my DB of 2021-05-01 22:00:00
, “3 Days” 2021-07-01 22:00:00
etc…
Here is what I have so far but I can’t get it to work. Can anyone point me in the best direction? I’ve trimmed down my code to make it more readable, i think it makes sense though.
<?php // Include config file require_once "config.php"; $expire = ""; $expire_err = ""; if($_SERVER["REQUEST_METHOD"] == "POST"){ // Create a function to add + days to current datetime based on value selected one form and insert into DB column expire // $input_expire = trim($_POST["expire"]); if ($input_expire = "1 Day" { // add + 1 day $expire_date = date('Y-m-d H:i:s'); $expire_date = date('Y-m-d H:i:s', strtotime($expire_date . ' +1 day')); $expire = $expire_date; echo $expire_date; } elseif ($input_expire = "3 Day" { // add + 3 day $expire_date = date('Y-m-d H:i:s'); $expire_date = date('Y-m-d H:i:s', strtotime($expire_date . ' +3 day')); $expire = $expire_date; echo $expire_date; } else ($input_expire = "5 Day" { // add + 5 day $expire_date = date('Y-m-d H:i:s'); $expire_date = date('Y-m-d H:i:s', strtotime($expire_date . ' +5 day')); $expire = $expire_date; echo $expire_date; } ////////////////////////////////////////////////////////////////////////////////////////////////////////////////// if(empty($expire_err)){ $sql = ("INSERT INTO table (expire) VALUES ('$expire')"); if($stmt = mysqli_prepare($link, $sql)){ mysqli_stmt_bind_param($stmt, "sss", $param_expire); // Set parameters $param_tags = $expire; if(mysqli_stmt_execute($stmt)){ header("location: index.html"); exit(); } else{ echo "Something went wrong"; } } mysqli_stmt_close($stmt); } mysqli_close($link); } ?> <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Create Message</title> </head> <body> <div class="wrapper"> <div class="container-fluid"> <div class="row"> <div class="col-md-12"> <div class="page-header"> <h2>Create Message</h2> </div> <form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>" method="post"> <div <p style="width: 45%"; class="form-group <?php echo (!empty($expire_err)) ? 'has-error' : ''; ?>"> <label>Message Expiry</label> <select class="form-control" name="expire"><?php echo $expire; ?> <option value=""></option> <option value="1 Day">1 Day</option> <option value="3 Days">3 Days</option> <option value="5 Days">5 Days</option> </select> <span class="help-block"><?php echo $expire_err;?></span> </div> <br> <input type="submit" class="btn btn-primary" value="Submit"> <a href="index.html" class="btn btn-default">Cancel</a> <br> </form> </div> </div> </div> </div> </div> </body> </html>
Advertisement
Answer
You have much problem in this code:
- You didn’t close
)
in allif
- All
if
use=
instead of==
(operators link) - In the form you write value like “5 Days” or “3 Days” but you try to catch “5 Day”
- The last
if
you useelse
instead ofelseif
Final Code:
<?php if($_SERVER["REQUEST_METHOD"] == "POST"){ // Create function to add + days to current datetime based on value select on form and insert into DB column expire // $input_expire = trim($_POST["expire"]); if ($input_expire == "1") { // add + 1 day $expire_date = date('Y-m-d H:i:s'); $expire_date = date('Y-m-d H:i:s', strtotime($expire_date . ' +1 day')); $expire = $expire_date; } elseif ($input_expire == "3") { // add + 3 day $expire_date = date('Y-m-d H:i:s'); $expire_date = date('Y-m-d H:i:s', strtotime($expire_date . ' +3 day')); $expire = $expire_date; } elseif ($input_expire == "5") { // add + 5 day $expire_date = date('Y-m-d H:i:s'); $expire_date = date('Y-m-d H:i:s', strtotime($expire_date . ' +5 day')); $expire = $expire_date; } echo $expire; } ?> <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Create Message</title> </head> <body> <div class="wrapper"> <div class="container-fluid"> <div class="row"> <div class="col-md-12"> <div class="page-header"> <h2>Create Message</h2> </div> <form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>" method="post"> <div <p style="width: 45%"; class="form-group <?php echo (!empty($expire_err)) ? 'has-error' : ''; ?>"> <label>Message Expiry</label> <select class="form-control" name="expire"><?php echo $expire; ?> <option value=""></option> <option value="1">1 Day</option> <option value="3">3 Days</option> <option value="5">5 Days</option> </select> <span class="help-block"><?php echo $expire_err;?></span> </div> <br> <input type="submit" class="btn btn-primary" value="Submit"> <a href="index.html" class="btn btn-default">Cancel</a> <br> </form> </div> </div> </div> </div> </div> </body> </html>
I deleted some parts of code that were not needed by the post and I changed the values with simple numbers so you already know that you want to use the days