1

PDO 関数を使用して MySQL サーバーに正しく接続する際に問題が発生しています。データベースから必要なものを照会できず、どの PDO 関数を使用すればよいかよくわかりません。この結果、0 を取得します。データベース経由で入力したパスワードとユーザー名を確認し、情報が正しい場合にセッションを開始する if ステートメントを作成したいと考えています。

ここに私の更新されたコードがあります:

<?php
// if a value is given
if (isset($_POST['username' && 'password'));
{

    // starts the session created if login info is correct
    session_start();

    // connectivity to MySQL server
    $db = new PDO('mysql:host=host;dbname=name', 'username', 'password');

    // information entered in form made into a variable
    $username = PDO::quote($_POST['username']); 
    $password = PDO::quote($_POST['password']);

    // after pressing login, checking if the variables exist in the database
    if ($_POST['button'] == 'Login')
    {
        $query = $db->query("SELECT COUNT(*) FROM login WHERE username=:username AND password=:password");
        $query->bindValue(':username', $username, PDO::PARAM_STR);
        $query->bindValue(':password', $password, PDO::PARAM_STR);
        $query->execute();

        // Check the number of rows that match the SELECT statement 
        if($query = $db->fetch(PDO::FETCH_OBJ)) 
        {
            echo "No records found";
        }
        else
        {
            header("Location: members.php");
            $_SESSION['username'] = $_POST['username'];
        }
    }   
    // After pressing register, stores variable and adds it in register field
    else if ($_POST['register'] == 'Register')
    {
        header("Location: register.php");
        $_SESSION['usernamereg'] = $_POST['username'];
    }

    // If no button is pressed, send to index
    else
    {
        header("Location: http://www.gjertgjersund.com");
    }

// closes the if value is given statement
}
?>

<!DOCTYPE html>
<html>
<head>
    <title> Folder </title>
    <link rel="stylesheet" type="text/css" href="frontpage.css">
</head>
<body>

    <div id="box">
    <div id="wrap">


        <center>
        <img src="./img/logo.png" alt="logo">

        <form action='index.php' method='POST' autocomplete='off'>

        <div class="usernameform">
        <input type='text' name='username' style='border: none; font-size: 20px;'>
        </div>

        <br />

        <div class="passwordform">
        <input type='password' name='password' style='border: none; font-size: 20px;'>
        </div>

        <br />

        <div class="registerlogin">
        <input type='submit' name='button' value='Login' class='input'>
        <input type='submit' name='register' value='Register' class='inputtwo'>
        </div>

        </form>
        </center>

    </div>
    </div>

</body>
</html>
4

3 に答える 3

0

一部のデータベースは、SELECT ステートメントによって返される行数を返す場合があります。ただし、この動作はすべてのデータベースで保証されているわけではなく、依存するべきではありません。マニュアルを参照してください。COUNT(*)次のクエリのようにandを使用fetchColumn()してエミュレートできrowCount()ます。

$query = $db->query("SELECT COUNT(*) FROM login WHERE username=:username AND password=:password");
$query->bindValue(':username', $username, PDO::PARAM_STR);
$query->bindValue(':password', $password, PDO::PARAM_STR);
$query->execute();
    // Check the number of rows that match the SELECT statement 
    if($query->fetchColumn() == 0) {
        echo "No records found";
     }else{
            //CODE FOR Success 
            //Etc
    }
于 2013-06-10T12:37:31.203 に答える