簡體   English   中英

嘗試連接 php 中的數據庫,但出現語法錯誤

[英]Trying to connect a database in php, but get syntax error

我正在嘗試使用我在 PHPMyAdmin 中創建的數據庫創建登錄頁面,但是每次運行它時,我都會在 login.php 的第 2 行收到語法錯誤,我不知道為什么它不起作用。 我確定我正確輸入了代碼。 當我運行它時,我得到一個錯誤:

解析錯誤:語法錯誤,第 2 行的 login.php 中出現意外的 ':'

login.php

<?php
    $dsn = 'mysql:host=localhost;dbname=dbase';
    $username = 'user1';
    $password = 'pass';

    $error = "";

    try{
        $db = new PDO ($dsn, $username, $password);
    }

    catch (PDOException $e){
        $error = $e->getMessage();
    }
// end of database connect

    $user = $_POST['user'];
    $pass = $_POST['pass'];

    $query = 'SELECT * FROM users WHERE user = :user AND pass = :pass';

    $statement = $db->prepare($query);
    $statement->bindValue(':user', $user);
    $statement->bindValue(':pass', $pass);
    $statement->execute();
    $valid = ($statement->rowCount() == 1);
    $result = $statement->fetch();
    $statement->closeCursor();

    if ($valid){
        $greeting = $result['email'];
        $permit = ", login success";
    }

    else{
        $greeting = $user;
        $permit = ", invalid credentials";
    }

?>
<!DOCTYPE html>

<html>
    <head>
        <title>Scheduler</title>
    </head>

    <body>
        <h1><?php echo $greeting.$permit; ?></h1>
    </body>
</html>

loginpage.php

<html>
    <head>
        <title>Scheduler</title>
    </head>

    <body>
        <form action = 'login.php' method = "post">
            <h1>EZ-Scheduler</h1>

            <h2>Please login to view rosters</h2>

            <table>
                <tr>
                    <td>Username:</td>
                    <td><input name = "user" type = "text" size = "25"></td>    
                </tr>
                <tr>
                </tr>   
                <tr>
                    <td>Password:</td>
                    <td><input name = "pass" type = "password" size = "25"></td>
                </tr>
            </table>    
                <p><input type = "submit" name = "button" value = "Login"></p>
            </table>
        </form>
    </body>
</html>

試試這個東西看看

<?php
    $username = 'user1';
    $password = 'pass';

    $error = "";

    try{
        $db = new PDO ('mysql:host=localhost;dbname=dbase', $username, $password);
    }

    catch (PDOException $e){
        $error = $e->getMessage();
    }
// end of database connect

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM