When I try to call the PHP file I get the error "crbug/1173575, non-JS module files deprecated." in the console. Unfortunately I do not know where to look for the error. (I have deleted the launch.json which is created by vscode).
I get a few PHP warnings while debugging the file.
The call of the file (without the faulty PHP part) worked when I commented out the part with the database connection via msqli. But the connection test shows "Connected successfully" in the debug console.
Warnings:
PHP Warning: Undefined variable $date in /var/www/html/sarbuschhardt/calendar/timeslots.php on line 77
PHP Warning: Undefined variable $bookings in /var/www/html/sarbuschhardt/calendar/timeslots.php on line 87
PHP Fatal error: Uncaught TypeError: in_array(): Argument #2 ($haystack) must be of type array, null given in /var/www/html/sarbuschhardt/calendar/timeslots.php:87
Stack trace:
#0 /var/www/html/sarbuschhardt/calendar/timeslots.php(87): in_array()
#1 {main}
thrown in /var/www/html/sarbuschhardt/calendar/timeslots.php on line 87
Code:
<?php
include "private/dbconnection.inc.php";
include "utils.php";
$conn = mysqli_connect($host, $user, $password, $database);
//Connection test
if(!$conn) {
die("Error: " . mysqli_error_connect());
}
if($conn)
echo "Connected successfully\n";
if(isset($_GET['date'])){
$date = $_GET['date'];
$stmt = $mysqli->prepare("select * from bookings where date = ?");
$stmt->bind_param('s', $date);
$bookings = array();
if($stmt->execute()){
$result = $stmt->get_result();
}
}
if(isset($_POST['submit'])){
$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'>Termin ist bereits vergeben.</div>";
}else{
$stmt = $mysqli->prepare("INSERT INTO bookings (date, timeslot) VALUES (?,?)");
$stmt->bind_param('ss', $date, $timeslot);
$stmt->execute();
$msg = "<div class='alert alert-success'>Buchung erfolgreich.</div>";
$bookings[] = $timeslot;
$stmt->close();
$mysqli->close();
}
}
}
$duration = 15;
$start = "09:00";
$end = "18:00";
function timeslots($duration, $start, $end){
$start = new DateTime($start);
$end = new DateTime($end);
$interval = new DateInterval("PT".$duration."M");
$slots = array();
for($intStart = $start; $intStart<$end; $intStart->add($interval)){
$endPeriod = clone $intStart;
$endPeriod->add($interval);
if($endPeriod>$end){
break;
}
$slots[] = $intStart->format("H:i")." - ". $endPeriod->format("H:i");
}
return $slots;
}
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Terminauswahl</title>
<link rel="stylesheet" href="calendar.css">
</head>
<body>
<h1>Terminauswahl für den <?php echo date('d.m.Y', strtotime($date)); ?></h1>
<div class="row">
<div class="col-md-12">
<?php echo(isset($msg))?$msg:""; ?>
</div>
<?php $timeslots = timeslots($duration, $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" disabled="disabled"><?php echo $ts; ?></button>
<?php }else{ ?>
<button class="btn btn-success book" data-timeslot="<?php echo $ts; ?>"><?php echo $ts; ?></button>
<?php } ?>
</div>
</div>
<?php } ?>
</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">×</button>
<h4 class="modal-title">Terminbuchung für: <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="">Termin</label>
<input readonly type="text" class="form-control" id="timeslot" name="timeslot">
</div>
<div class="form-group pull-right">
<button name="submit" type="submit" class="btn btn-primary">Buchung bestätigen</button>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
</div>
<script>
$(".book").click(function(){
var timeslot = $(this).attr('data-timeslot');
$("#slot").html(timeslot);
$("#timeslot").val(timeslot);
$("#myModal").modal("show");
});
</script>
</body>
</html>