为什么仅在页面重新加载时才出现SQL错误?

时间:2018-11-09 02:48:57

标签: php html mysqli bulma

当我第一次打开页面时,一切看起来都很好。湖泊下拉列表将自动填充码头下拉列表。像这样。

Good image

但是如果由于某种原因,我在未填写所有信息的情况下点击了“提交”按钮,将给我一个错误,湖泊不再自动填充码头。看起来像这样

good image

sql错误指出了这一点,但不知道我在做什么错。

 $lakelist=mysqli_query($link,"SELECT * FROM lakes ORDER BY lake ASC");

这是该页面的完整php代码。

    <?php
// Initialize the session
session_start();

// Check if the user is logged in, if not then redirect him to login page
if(!isset($_SESSION["loggedin"]) || $_SESSION["loggedin"] !== true){
    header("location: login.php");
    exit;
}

 //Include config file
require_once "config.php";
?>
<!doctype html>
<html lang="en">
    <head>
        <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
        <meta name="viewport" content="width=device-width, initial-scale=1">

        <title>Wake Up Rentals | Jetski Rental</title>

        <meta name="description" content="" />
        <meta name="keywords" content="" />

        <link rel="canonical" href="/" />

        <link rel="stylesheet" href="/bsite/assets/app.css" type="text/css">
        <link href="https://fonts.googleapis.com/css?family=Montserrat:300,400,600" rel="stylesheet">
        <link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.4.1/css/all.css" integrity="sha384-5sAR7xN1Nv6T6+dT2mhtzEpVJvfS3NScPQTrOxhwjIuvcA67KV2R5Jz6kr4abQsz" crossorigin="anonymous">
        <script type="text/javascript" src="http://code.jquery.com/jquery-1.10.0.min.js"></script>
        <script  type="text/javascript" src="/bsite/assets/javascript/crlist.js"> </script>
        <script>
        $(document).ready(function() {


            initCs("");
        });
        </script>
    </head>

            <?php

// Define variables and initialize with empty values
$lake = $days = $dock = $date = $numberofsports = $numberofdeluxe = $smallTube = $largeTube = $wakeboard= "";
$lake_err = $days_err = $date_err = $numberofsports_err = $numberofdeluxe_err =$smallTube_err = $largeTube_err = $wakeboard_err = "";

// Processing form data when form is submitted
if($_SERVER["REQUEST_METHOD"] == "POST"){



        //Validate lake
        if(empty (trim ($_POST["lake"]))){
            $lake_err = "Please select a lake";
        }else{
            $lake = trim($_POST["lake"]);
        } 

        //Validate days
        if(empty (trim ($_POST["days"]))){
            $days_err = "Please select number of days";
        }else{
            $days = trim($_POST["days"]);
        } 

        //Validate date
        if(empty (trim ($_POST["date"]))){
            $date_err = "Please select a start date";
        }else{
            $date = trim($_POST["date"]);
        } 

        //Validate number of sports
     if (!in_array(trim($_POST["numberofsports"]), ["0", "1", "2", "3", "4"])) {
            $numberofsports_err = "Please select an amount";
        }else{
            $numberofsports = trim($_POST["numberofsports"]);
        }


        //Validate nmber of deluxe
             if (!in_array(trim($_POST["numberofdeluxe"]), ["0", "1", "2"])) {
            $numberofdeluxe_err = "Please select an amount";
        }else{
            $numberofdeluxe = trim($_POST["numberofdeluxe"]);
        }

//add info here for the check boxes.
if ( !isset( $_POST[ "smallTube" ] ) ) {
    $smallTube = "no";
} else {
    $smallTube = "yes";
}

//add info here for the check boxes.
if ( !isset( $_POST[ "largeTube" ] ) ) {
    $largeTube = "no";
} else {
    $largeTube = "yes";
}

//add info here for the check boxes.
if ( !isset( $_POST[ "wakeboard" ] ) ) {
    $wakeboard = "no";
} else {
    $wakeboard = "yes";
}

        //dock
                //Validate dock
        if(empty (trim ($_POST["ndock"]))){
            $dock_err = "Please select number of days";
        }else{
            $dock = trim($_POST["ndock"]);
        } 

    if(empty($lake_err) && empty($days_err) && empty($date_err) && empty($numberofsports_err) && empty($numberofdeluxe_err) ){

        // Prepare an insert statement
        $sql = "INSERT INTO prerental (email, firstname, lastname, address, phonenumber,lake,days,date,numberofsports,numberofdeluxe,smalltube,largetube,wakeboard,dock) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"; 
        if($stmt = mysqli_prepare($link, $sql)){
            // Bind variables to the prepared statement as parameters
            mysqli_stmt_bind_param($stmt, "ssssssssssssss", $param_email,  $param_firstname, $param_lastname, $param_address, $param_phonenumber, $param_lake, $param_days, $param_date, $param_numberofsports, $param_numberofdeluxe, $param_smalltube, $param_largeTube, $param_wakeboard, $param_dock);

            // Set parameters
            $param_email = $_SESSION["email"];
            $param_firstname = $_SESSION["fname"];
            $param_lastname = $_SESSION["lname"];
            $param_address = $_SESSION["address"];
            $param_phonenumber = $_SESSION["phone"];
            $param_lake = $lake;
            $param_days = $days;
            $param_date = $date;
            $param_numberofsports = $numberofsports;
            $param_numberofdeluxe = $numberofdeluxe;
            $param_smalltube = $smallTube;
            $param_largeTube = $largeTube;
            $param_wakeboard = $wakeboard;
            $param_dock = $dock;


            // Attempt to execute the prepared statement
            if(mysqli_stmt_execute($stmt)){
                // Redirect to login page
                header("location: FinalChecks.php");
            } else{
                echo "Something went wrong. Please try again later.";
            }
        }

        // Close statement
        mysqli_stmt_close($stmt);
    }

    // Close connection
    mysqli_close($link);
}
?>
    <body>
        <header>
            <div class="container">
                <nav class="navbar" role="navigation" aria-label="main navigation">
                    <div class="navbar-brand">
                        <a class="navbar-item c-logo" href="/">
                            <img src="/bsite/assets/images/logo.png" alt="Wake Up Rentals">
                            <span class="e-wakeup">Wake Up</span> <span class="e-rentals">Rentals</span>
                        </a>

                        <a role="button" class="navbar-burger" aria-label="menu" aria-expanded="false" data-target="navMenu">
                            <span aria-hidden="true"></span>
                            <span aria-hidden="true"></span>
                            <span aria-hidden="true"></span>
                        </a>
                    </div>
                    <div id="navMenu" class="navbar-menu">
                        <div class="navbar-end">
                            <a href="/why-us" class="navbar-item">Why Wake Up?</a>
                            <a href="/rentals.php" class="navbar-item">Our Rentals</a>
                            <a href="/lakes.php" class="navbar-item">Locations</a>
                            <a href="/book.php" class="navbar-item">Book Now</a>
                            <a href="/contact" class="navbar-item">Contact</a>
                            <a href="/login.php" class="navbar-item">Login</a>
                        </div>
                    </div>
                </nav>
            </div>
        </header>

        <section class="section">
            <div class="container">
                <div class="columns">
                    <div class="column">
                        <h1>Hi, <b><?php echo htmlspecialchars($_SESSION["fname"]); ?></b>. Your ready to order.</h1>
                        <p>
                        <?php echo htmlspecialchars($_SESSION["fname"]); ?><br>
                        <?php echo htmlspecialchars($_SESSION["lname"]); ?><br>
                        <?php echo htmlspecialchars($_SESSION["email"]); ?><br>
                        <?php echo htmlspecialchars($_SESSION["address"]); ?><br>
                        <?php echo htmlspecialchars($_SESSION["phone"]); ?><br>
                        </p>    
                        <p>
                         <a href="sign-uplakenotify.php" class="button is-success">Sign up for lake notification</a>
                        <br>
                        <br>
                        <br>
                        <a href="sign-upDiscount.php" class="button is-success">Sign up for Discounts</a>
                        <br>
                        <br>
                        <br>
                        <a href="reset-password.php" class="button is-danger">Reset Your Password</a>
                        <br>
                        <br>
                        <br>
                        <a href="Logout.php" class="button is-danger">Sign Out of Your Account</a>
                        </p>
                    </div>
                    <div class="column">
                        <form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>" method="post"> 
                            <label><b>Get your instint quote here!</b></label>

                                <p class = "help is-danger"><?php echo $lake_err; ?></p>

                                <?php //Drop down example
                                    $lakelist=mysqli_query($link,"SELECT * FROM lakes ORDER BY lake ASC");
                                        echo"<div class='field'>";
                                        echo"<div class='control'>";
                                        echo"<label class='label'>  Which lake do you want to go to?</label>";
                                        echo"<div class='select'>";
                                        echo "<select name='lake' id='lake' onchange=\"reload(this.form)\" title='lake you want'><option value='0'>Select Lake</option>";
                                        while($clist=mysqli_fetch_array($lakelist))
                                        {
                                          echo "<option value='$clist[lake]'>$clist[lake]</option>"."<br/>";
                                        }
                                        echo"</select>";
                                    mysqli_close($link);
                                 ?>
                                 </div>
                                 </div>
                            </div>
                             <div class="field">
                                 <div class="control">
                                 <label class="label">Select Dock</label>
                                     <div class="select">
                                        <select name="ndock" id="ndock"></select>
                                    </div>
                                 </div>
                             </div>

                             <div class="field">
                                 <div class="control">
                                    <label class="label">How many days would you like?</label>
                                     <div class="select">

                                        <select name="days">
                                            <option value="">Select...</option>
                                            <option value="1">1 Day </option>
                                            <option value="2">2 Day </option>
                                            <option value="3">3 Day </option>
                                            <option value="Week">1 week </option>
                                        </select>
                                        <p class = "help is-danger"><?php echo $days_err; ?></p>
                                    </div>
                                </div>
                            </div>

                            <div class="field">
                                <div class="control">
                                <label class="label">What day would you like to start the rental on [Month/day/year]?</label>
                                    <input width="50%" type="text" name="date">
                                    <p class = "help is-danger"><?php echo $date_err; ?></p>
                                </div>
                            </div>

                            <div class="field">
                                <div class="control">
                                <label class="label">Number of sport jet skis to rent?</label>
                                    <div class="select">

                                        <select class="select" name="numberofsports">
                                            <option value="0">0 </option>
                                            <option value="1">1 </option>
                                            <option value="2">2 </option>
                                            <option value="3">3 </option>
                                            <option value="4">4 </option>
                                        </select>
                                        <p class = "help is-danger"><?php echo $numberofsports_err; ?></p>
                                    </div>
                                </div>
                            </div>


                            <div class="field">
                                <div class="control">
                                <label class="label">Number of Deluxe jet skis to rent?</label>
                                    <div class="select">

                                        <select class="form-control" name="numberofdeluxe">
                                            <option value="0">0 </option>
                                            <option value="1">1 </option>
                                            <option value="2">2 </option>
                                        </select>
                                        <p class = "help is-danger"><?php echo $numberofdeluxe_err; ?></p>
                                    </div>
                                </div>
                            </div>

                            <div class="field">
                                <div class="control">
                                    <label class="checkbox">
                                        <input type="checkbox" name="smallTube" value="yes"/>Small Tube<br>
                                    </label>
                                    <p class = "help is-danger"><?php echo $smallTube_err; ?></p>
                                </div>
                            </div>


                            <div class="field">
                                <div class="control">
                                    <label class="checkbox">
                                        <input type="checkbox" name="largeTube" value="yes">Large Tube<br>
                                    </label>
                                    <p class = "help is-danger"><?php echo $largeTube_err; ?></p>
                                </div>
                            </div>

                            <div class="field">
                                <div class="control">
                                    <label class="checkbox">
                                        <input type="checkbox" name="wakeboard" value="yes">WakeBoard<br>
                                    </label>
                                    <p class = "help is-danger"><?php echo $wakeboard_err; ?></p>
                                </div>
                            </div>
                            <div class="field is-grouped is-grouped-centered">
                                <div class="control">
                                    <button class="button is-primary">Get Online Quote</button>
                                </div>
                                <div class="control">
                                    <button type="reset" class="button is-danger">Reset</button>
                                </div>
                            </div>
                        </form>  
                    </div>
                    <div class="column">
                        <h2><b>Lake Notifacation</b></h2>
                        <p> You can select to get emails when we are at your lake of choice! When we are at your 
                        lake of choice you can rent Jet Skis by the hour. Only 60$ per hour. Sign up now by clicking on 
                        "Sign up for lake notification".  You can turn this feature on or off at anytime.</p>

                        <h2><b>Sign up for Discounts</b></h2>
                        <p>You can get really good discounts by signing up for discounts. It will make sure when ever a 
                        discount is aviable , you recive it!  We will not send spam, only good discounts for Jet Ski rentals!
                        Sign up now!</p>
                    </div>

                </div>
            </div>
        </section>

        <footer class="footer">
            <div class="container">
                <div class="columns">
                    <div class="column">
                        <ul>
                            <li><a href="facebook">Facebook</a></li>
                            <li><a href="/contact">Contact</a></li>
                            <li><a href="/blog">Blog</a></li>
                            <li><a href="/privacy-policy">Privacy policy</a></li>
                        </ul>
                    </div>
                    <div class="column">
                        <h4>Wake Up Rentals</h4>
                        <p>Address</p>
                        <p><a href="tel:+18005596580">+1800 559 6580</a></p>
                    </div>
                    <div class="column">
                    </div>
                </div>
                <div class="columns">
                    <div class="column e-copyright">Copyright &copy; 2018, Jet Skies R US</div>
                </div>
            </div>
        </footer>
        <script type="text/javascript" src="/bsite/assets/app.js"></script>
    </body>
</html>

请注意,在页面重新加载之前,此方法一直有效,所以我要寻求帮助。另一个问题说它在本地但不在服务器上工作。在那种情况下,更大的事情已经改变。页面重新加载时我的位置在哪里。

1 个答案:

答案 0 :(得分:0)

您正在使用mysqli_close($ link)关闭连接; -如果表单已提交,则调用该方法,但是检查提交是否成功的方法不在检查范围之内。您只需要将其立即放在msysqli_stmt_close($ stmt);

之后