0
    <?php
include "session.php";
if(isset($_POST['permission'])) {
  $x = $_POST['x'];
  $reason = $_POST['reason'];
  $leaving = $_POST['leaving'];
  $returning = $_POST['returning'];



  $query = "INSERT INTO permissions (reason, leaving, returning, user, x) VALUES('$reason', '$leaving', '$returning', '$login_session', '$x')";
  $result = mysqli_query($db, $query);// or die('Error querying database!');
  if ($result === false) { die(mysqli_error($db)); }
  echo 'Thanks for Posting! Your request will be addressed as soon as possible!';
}

What is wrong with this code? The leaving and returning columns are of type- DATETIME. is that causing a problem?

<form method="post" action="" class="forms">
        Type: <select name="x" required>
          <option value="0">Leave</option>
          <option value="1">Other</option>
        </select>
        <input type="text" name="reason" placeholder="Reason" required><br>
        <input type="datetime-local" name="leaving"><br>
        <input type="datetime-local" name="returning"><br>
        <input type="submit" value="Request Permission" name="permission">
      </form>
11
  • Your question is not very clear, but if you want to insert the date to those fields, you can set their default values to CURRENT_TIMESTAMP. Commented Apr 9, 2017 at 6:36
  • @mzcoxfde The above code is giving me a sql syntax error. i think the datetime-local type and datetime column type is causing that... Commented Apr 9, 2017 at 6:44
  • I have posted the answer. In case you still face any issues, then share the exact error and line number where you are getting it. Commented Apr 9, 2017 at 6:53
  • @MohammedAkhtarZuberi You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'returning, user, x) VALUES('gotcha gotcha gotcha gotcha ', '2017-04-06T12:00', '' at line 1...this is the error i am facing Commented Apr 9, 2017 at 7:01
  • 1
    WARNING: When using mysqli you should be using parameterized queries and bind_param to add user data to your query. DO NOT use string interpolation or concatenation to accomplish this because you have created a severe SQL injection bug. NEVER put $_POST, $_GET or any user data directly into a query, it can be very harmful if someone seeks to exploit your mistake. Commented Apr 9, 2017 at 7:04

1 Answer 1

1

To achieve what you want, follow the below steps.

  1. Instead of having Fields type as "DATETIME" in the DB, have them simple VARCHAR with the length of around 99 in order to store full format date and time easily.
  2. Input Type "datetime-local" is actually not required. Simply use Input Type "date" is OK as you are not capturing the Time.

Once you are ready with the above fixes, amend your PHP code as below. If you want, you can simply copy/paste it as well.

<?php
    include "session.php";

    if(isset($_POST['permission'])) {
        $x = $_POST['x'];
        $reason = $_POST['reason'];
        $leaving = $_POST['leaving'];
        $returning = $_POST['returning'];

        $query = "INSERT INTO permissions (reason, leaving, returning, user, x) VALUES('$reason', '$leaving', '$returning', '$login_session', '$x')";
        $result = mysqli_query($db, $query);// or die('Error querying database!');

        if ($result === false) {
            die(mysqli_error($db));
        }

        echo 'Thanks for Posting! Your request will be addressed as soon as possible!';
    }
?>

Now to retrieve this date value in your required format, use the below code.

$fetched_date = "04-09-2017" //Assuming that this Sunday, April 9, 2017 is the fetched date and stored in "04-09-2017"

Now to convert it to Sunday, April 9, 2017 simply follow the below steps.

$fetched_date_uf = date_create($fetched_date);
$fetched_date_formatted = date_format($fetched_date_uf, 'l, F d, Y');

echo $fetched_date_formatted; //Sunday, April 9, 2017

Refer to this link for PHP Date Time Formats.

Sign up to request clarification or add additional context in comments.

1 Comment

This is not the answer of what he has asked.

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.