-2

So I am working on a simple booking calendar time slot project.

I organize my form in a separate page view so I can just insert it on my main php.

I'm running a PHP script and continue to receive errors like: enter image description here

What is the meaning of these error messages?

Why do they appear all of a sudden?

Here's how i construct my code:

    <?php
$mysqli = new mysqli('localhost', 'root', '', 'bookingcalendar');
if(isset($_GET['date'])){
    $date = $_GET['date'];
}

if(isset($_POST['submit'])){
    $name = $_POST['name'];
    $email = $_POST['email'];
    $timeslot = $_POST['timeslot'];
    $stmt = $mysqli->prepare("select * from bookings where date=? AND timeslot=?");
    $stmt->bind_param('ss', $date,$timeslot);
    if($stmt->execute()){
        $result = $stmt->get_result();
        if($result->num_rows>0){
            $msg = "<div class='alert alert-danger'>Already Booked</div>";
        }else{
            $stmt = $mysqli->prepare("INSERT INTO bookings (name, email, date, timeslot) VALUES (?,?,?,?)");
            $stmt->bind_param('ssss', $name, $email, $date, $timeslot);
            $stmt->execute();
            $msg = "<div class='alert alert-success'>Booking Successfull</div>";
            $bookings[] = $timeslot;
            $stmt->close();
            $mysqli->close();
        }
    }
    
}

$duration = 10;
$cleanup = 0;
$start = "09:00";
$end = "15:00";

function timeslots($duration, $cleanup, $start, $end){
    $start = new DateTime($start);
    $end = new DateTime($end);
    $interval = new DateInterval("PT".$duration."M");
    $cleanupInterval = new DateInterval("PT".$cleanup."M");
    $slots = array();

    for($intStart = $start; $intStart<$end; $intStart->add($interval)->add($cleanupInterval)){
        $endPeriod = clone $intStart;
        $endPeriod->add($interval);
        if($endPeriod>$end){
            break;
        }
        
        $slots[] = $intStart->format("H:iA")." - ". $endPeriod->format("H:iA");
        
    }
    
    return $slots;
}

?>
<!doctype html>
<html lang="en">

  <head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">

    <title></title>

    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
   
  </head>

  <body>
    <div class="container">
        <h1 class="text-center">Book for Date: <?php echo date('m/d/Y', strtotime($date)); ?></h1><hr>
        <div class="row">
           <div class="col-md-12">
                <?php echo(isset($msg))?$msg:""; ?>
                    </div>
                        <?php $timeslots = timeslots($duration, $cleanup, $start, $end); 
                            foreach($timeslots as $ts){
                        ?>
                        <div class="col-md-2">
                            <div class="form-group">
                                <?php if(in_array($ts, $bookings)){ ?>
                                <button class="btn btn-danger"><?php echo $ts; ?></button>
                                <?php }else{ ?>
                                <button class="btn btn-success book" data-timeslot="<?php echo $ts; ?>"><?php echo $ts; ?></button>
                                <?php }  ?>
                            </div>
                        </div>
                    </div>
                <?php } ?>
        </div>
    </div>
    <div id="myModal" class="modal fade" role="dialog">
        <div class="modal-dialog">

            <!-- Modal content-->
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal">&times;</button>
                    <h4 class="modal-title">Booking for: <span id="slot"></span></h4>
                </div>
                <div class="modal-body">
                    <div class="row">
                        <div class="col-md-12">
                            <form action="" method="post">
                               <div class="form-group">
                                    <label for="">Time Slot</label>
                                    <input readonly type="text" class="form-control" id="timeslot" name="timeslot">
                                </div>
                                <div class="form-group">
                                    <label for="">Name</label>
                                    <input required type="text" class="form-control" name="name">
                                </div>
                                <div class="form-group">
                                    <label for="">Email</label>
                                    <input required type="email" class="form-control" name="email">
                                </div>
                                <div class="form-group pull-right">
                                    <button name="submit" type="submit" class="btn btn-primary">Submit</button>
                                </div>
                            </form>
                        </div>
                    </div>
                </div>
                
            </div>

        </div>
    </div>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>
    <script>
        $(".book").click(function(){
            var timeslot = $(this).attr('data-timeslot');
            $("#slot").html(timeslot);
            $("#timeslot").val(timeslot);
            $("#myModal").modal("show");
        });
    </script>
</body>

</html>

Any idea what I am missing?

How do I fix them?

Dharman
  • 26,923
  • 21
  • 73
  • 125
niiro kun
  • 9
  • 1
  • i keep getting error in this line i already defined it am i missing something? – niiro kun Jun 02 '22 at 04:47
  • `$bookings` is not defined anywhere in your code – Alon Eitan Jun 02 '22 at 04:52
  • how do i fix them? @AlonEitan – niiro kun Jun 02 '22 at 04:53
  • 1
    I guess by doing `$bookings = ;` – Alon Eitan Jun 02 '22 at 04:55
  • You say you already defined `$bookings`, but I can't see it anywhere. Please point out where you think you've defined it. Also, [Please do not upload images of code/data/errors when asking a question.](//meta.stackoverflow.com/q/285551) – MatBailie Jun 02 '22 at 05:03
  • Your $bookings is located inside the if(isset($_POST['submit'])), so if you try to access the page (GET Method), then the $bookings, will not be defined, since, the $bookings, is defined when isset($_POST['submit'], so you need to declare the default value $bookings – Existed Mocha Jun 02 '22 at 05:13

1 Answers1

0

Make sure below points

Initialize values with there possible if like you can do

$bookings = [];
  1. your error clearly says that $bookings is not an array here

  2. you are populating $bookings inside else so there may be cases when it will go inside if block and no $bookings exists in that case you will get error if you have not checked whether it exists or not before its use

Passionate Coder
  • 6,650
  • 2
  • 16
  • 36