0

I'm having a trouble of inserting my date & time in my database. I used jquery datepicker and for the time I used a select options tag so I just need them to be inserted into the database. This is my date table:

DATABASE:

CREATE TABLEdate_tbl( datevarchar(30) NOT NULL, timevarchar(30) NOT NULL, ordertypeint(10) NOT NULL ) ENGINE=InnoDB DEFAULT CHARSET=latin1;

HTML:

 <div class="col-md-4">
    <h5 class="control-label" style="font-family:Lucida Calligraphy;margin-left: -15px">Date:</h5>
    <input type="text" name="datepicker" class="form-control" placeholder='Select date' id="datepicker" style="margin-left: -15px"/>
    <script>
        $(function() {
        $('#datepicker').datepicker({
        minDate: 0,
        dateFormat: 'yy-mm-dd'
        });
        });
    </script>
 </div>
 <div class="col-md-4">
    <h5 for="inputDate" class="control-label" style="font-family:Lucida Calligraphy;margin-left: -15px">Time: </h5>
    <select type="text" name="time" id="time" class="form-control">
        <option>Select time</option>
        <option>08:00 AM</option>
        <option>09:00 AM</option>
        <option>10:00 AM</option>
        <option>11:00 AM</option>
        <option>01:00 PM</option>
        <option>02:00 PM</option>
        <option>03:00 PM</option>
        <option>04:00 PM</option>
        <option>05:00 PM</option>
     </select>
  </div>

PHP:

<?php 
   include('db.php');
   if(isset($_POST['reserve'])){
        $wdate = $_POST['datepicker'];
        $wtime = $_POST['time'];

        $wdate_array = array();
        $wtime_array = array();

        $insertDate=$conn->query("INSERT INTO date_tbl (date, time, ordertype) VALUES ('$wdate','$wtime',0)");

       $q=$conn->query("SELECT * FROM date_tbl");

       $dateRow=mysqli_num_rows($q);

           while($row=mysqli_fetch_array($q)){
               $wdate_array[]=$row['date'];
               $wtime_array[]=$row['time'];
           }
            if(array_intersect($wdate_array, $wtime_array)){
                echo "<p style='text-align:center;color:red;'><span class='fa fa-warning'></span> Date & Time Invalid!</p>";
            }else{
                echo "<p style='text-align:center;color:green;'><b></b> Date & Time are available!</p>";
            }
     }
?>

whenever i try to reserve it will only insert once and when i try again it doesn't insert anymore.

Louise M.
  • 15
  • 8
  • Don't use varchar to store dates. RDBMS have date/datetime/timestamp fields for a reason. – Devon Bessemer Sep 28 '17 at 13:03
  • Your `CREATE` statement doesn't match up with the columns in your `INSERT` statement – Patrick Q Sep 28 '17 at 13:03
  • @PatrickQ Fixed it. I just added it a while ago forgot to add there – Louise M. Sep 28 '17 at 13:08
  • @Devon even if my input type is text. Can i insert them in a date/time fields? – Louise M. Sep 28 '17 at 13:08
  • "when i try again it doesn't insert anymore" When you try to insert the _same_ data again or when you try to insert _any_ data again? – Patrick Q Sep 28 '17 at 13:11
  • @PatrickQ any data. If i use the same data again it also doesn't insert – Louise M. Sep 28 '17 at 13:14
  • You also have a syntax error. You are missing a closing double-quote at the end of your `echo` inside your `if` block. Are you actually copy-pasting your exact code or are you trying to transcribe it? – Patrick Q Sep 28 '17 at 13:20
  • @PatrickQ oh yeah, i changed it cause my original code is much longer. It's suppose to insert another data under the else there. – Louise M. Sep 28 '17 at 13:24
  • 1
    If we don't know what your actual code is, it's going to be really hard to help you. – Patrick Q Sep 28 '17 at 13:25
  • Sorry about that. I found another error on getting the value of the date and time. Fixed it again. That's pretty much what my code is i'm just having troubled with reserving the dates – Louise M. Sep 28 '17 at 13:31
  • Did you enable [error reporting?](https://stackoverflow.com/a/6575502/3828957), that may give away something more sever. Additionally the actual logic behind `if(array_intersect($wdate_array, $wtime_array)) {...}` seems flawed - afaik it will only be true if there is f.e. a time in both date/time arrays which should always be wrong, [escaping](https://stackoverflow.com/questions/10646142/what-does-it-mean-to-escape-a-string) is another issue - not for your current problem but - if you plan on something productive. Is the precondition `isset($_POST['reserve'])` met? – makadev Sep 28 '17 at 13:50

1 Answers1

1

Use the strtotime function to store your Post value to a variable and use date field in your database.

Sean Konig
  • 124
  • 1
  • 12