PHP PDO登录系统

标签 php mysql forms authentication

我正在尝试让我的登录表单与 php 和 mysql 数据库一起使用。 这一刻表格似乎没有做任何事情。 当我点击提交时,它会重新加载没有登录表单的登录页面。

数据库结构:

Database structure

代码如下:

<?php

require_once 'header.php';
$current = 'login.php';
require_once 'navigatie.php';


if(isset($_POST['login'])){

    $username = $_POST["gebruiker"];
    $password = $_POST["psw"];

    try{
        $stmt = $db->prepare("SELECT * FROM gebruiker WHERE (naam = :gebruiker AND wachtwoord = :psw)");
            $stmt ->bindParam(":naam", $username);
        $stmt ->bindParam(":psw", $password);
        $stmt ->execute();
        $db = null;
        $result=$stmt->fetch();
        if($stmt->rowCount() == 1)
        {
            //gebruiker bestaat
            header("location: index.php");
            session_start();
        }
        else{
            echo "gebruiker bestaat niet";
        }

    }
    catch (PDOException $e){
        $message = $e . "Failed";
    }

}
?>

<main>

    <div class="row">
    <form role="form" class="col s12" method="POST" action="login.php" >
        <div class="row">
            <div class="container">

                <div class="input-field col s6">
                    <i class="material-icons prefix">face</i>
                    <input name="gebruiker" id="icon_prefix" type="text" class="validate">
                    <label for="icon_prefix">Naam</label>
                </div>


                <div class="input-field col s6">
                    <i class="material-icons prefix">lock</i>
                    <input name="psw" id="icon_psw" type="password" class="validate">
                    <label for="icon_psw">password</label>
                </div>

                <div class="col s12" >
                    <button id="submit" class="btn waves-effect waves-light" type="submit" name="login">Login
                        <i class="material-icons right">send</i>
                    </button>

                </div>
            </div>
    </form>
    </div>

最佳答案

下面这两行有些东西:

$stmt = $db->prepare("SELECT * FROM gebruiker WHERE (naam = :gebruiker AND wachtwoord = :psw)");
$stmt ->bindParam(":naam", $username);

你试图绑定(bind)一个名为 :naam 的参数,但这个词似乎是你的 SQL 语句中的一个列。

在分析您的脚本并假设 gebruiker 正在从 POST 请求接收后,这些行应该是:

$stmt = $db->prepare("SELECT * FROM gebruiker WHERE (naam = :gebruiker AND wachtwoord = :psw)");
$stmt ->bindParam(":gebruiker", $username); //<-- Look at Param name binded

关于PHP PDO登录系统,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41282803/

相关文章:

javascript - 多个表单的 jquery 验证之前的 ajax 表单提交

php - URL 重写 - 仅适用于数字

php - 如何使用 PHP Mysql 查询获取 mysql 表中的 json 值

mysql - 将最新时间与同一行中的正确数据分组

mysql - Grails 3 urlMappingsReport 失败

java - Spring 数据 - JPA,缺少 ID

javascript - 提交时仅在 jQuery 中显示一次消息

php - 日期格式无效

PHP 显示 MySQL 搜索的搜索结果

forms - 实体形式问题与 Symfony2 关系多对多