首页 > 解决方案 > “警告:mysqli::prepare(): Couldn't fetch mysqli”但是我没有这条线?

问题描述

当我尝试继续我的“book.php”项目文件之一时,在尝试预订时间段时收到此错误消息:https ://gyazo.com/1b23f9af8aca75a1a692cb94ccb3b59d 。请注意,在 HTTP ERROR 5OO 之前我确实有一个错误,但这是因为我有一个未知变量 $mysqli 来连接到数据库,但是我没有定义它,因为我决定使用 'include("db .php")' 然后我必须引用 $con 才能连接到数据库。这是 book.php 的代码:

<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);

include("auth_session.php");
include("db.php");

if(isset($_GET['date'])){
    $date = $_GET['date'];
    $stmt = $con->prepare("SELECT * FROM bookings WHERE date = ?");
    $stmt->bind_param('s', $date);
    $bookings = array();
    if($stmt->execute()){
        $result = $stmt->get_result();
        if($result->num_rows>0){
            while($row = $result->fetch_assoc()){
                $bookings[] = $row['timeslot'];
            }

            $con->close();
        }
    }
}

if(isset($_POST['submit'])){
    $username = $_SESSION["username"];
    $timeslot = $_POST['timeslot'];
    $barber = $_POST['barber'];
    $stmt = $con->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 = $con->prepare("INSERT INTO bookings (username, timeslot, date, barber) VALUES (?, ?, ?, ?)");
            $stmt->bind_param('ssss', $username, $timeslot, $date, $barber);
            $stmt->execute();
            $msg = "<div class='alert alert-success'>Booking Successful</div>";
            $bookings[] = $timeslot;
            $stmt->close();
            $con->close();
        }
    }
}

$duration = 60;
$cleanup = 0;
$start = "10:00";
$end = "17: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">
    <link href="#" rel="stylesheet">
    <link rel="stylesheet" href="#">
</head>

<body>
    <nav class="#">
        <div class="#">
            <div class="#">
                <button type="button" class="#" data-toggle="collapse" data-target="#navbar-collapse">
                    <span class="#">Toggle navigation</span>
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                </button>
                <a class="#" href="/dashboard.php">Modern Haircut Designs</a>
            </div>

            <div class="#" id="#">
                <ul class="#">
                    <li class="actif"><a href="/dashboard.php">Home</a></li>
                    <li><a href="/dashboard.php#services">Services</a></li>
                    <li><a href="/dashboard.php#team">Our Team</a></li>
                    <li><a href="/user-bookings.php">View Appointments</a></li>
                    <li><a href="/calendar.php">Book Appointment</a></li>
                    <li><a href="/logout.php">Logout</a></li>
                </ul>
            </div>
        </div>
    </nav>



    <div class="#" style="padding: 100px 0px 0px 0px;">
        <h1 class="#">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>
            <?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: <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="">Timeslot</label>
                                    <input type="text" readonly name="timeslot" class="form-control" id="timeslot" class="form-control" required>
                                </div>
                                <div class="form-group">
                                    <label for="">Username</label>
                                    <input type="text" readonly name="name" class="form-control" placeholder=<?php echo($_SESSION["username"]); ?> required>
                                </div>
                                <div class="form-group">
                                    <label for="">Barber</label>
                                    <p></p>
                                    <select name="barber" id="barber">
                                        <option value="Ryan Karim">Ryan</option>
                                        <option value="Janit Sudeesh">Janit</option>
                                        <option value="Sayith Sotheeswaran">Sayith</option>
                                        <option value="Mirella Barry">Mirella</option>
                                    </select>
                                </div>
                                <div class="form-group pull-right">
                                    <button class="btn btn-primary" type="submit" name="submit">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>

<footer style="position:absolute; bottom: 0; width: 100%;">
    crafted with &hearts; in # by <a href="#">#</a>
</footer>

</html>

这是 auth_session.php 的代码:

<?php
    session_start();
    if(!isset($_SESSION["username"])) {
        header("Location: login.php");
        exit();
    }
?>

这是 db.php 的代码:

<?php
    $con = mysqli_connect("#","#","","#");
    if (mysqli_connect_errno()){
        echo "Failed to connect to MySQL: " . mysqli_connect_error();
    }
?>

标签: phpmysqlmysqliappointment

解决方案


推荐阅读