php - 使用 DB 登录 PHP

标签 php mysql

我正在尝试使用 MySQL 进行登录,但它不起作用。基本上我正在尝试检查针对我的数据库发布的登录名和密码,但由于某种原因它不起作用。有人可以给我提示吗?

登录.php

include "conexao.php";

$result = mysql_query("SELECT * FROM usuario WHERE login =  '".$_POST['login']."' AND senha = '".$_POST['senha']."'") or die (mysql_error());

while ($row = mysql_fetch_assoc($result)) {

session_start();
if ($_POST['login'] && $_POST['senha']) {
    if ($row['login'] == $_POST['login'] && $row['senha'] == $_POST['senha']) {
        $_SESSION['login'] = $row['login'];
        $_SESSION['senha'] = $row['senha'];
        header("Location: index.php");

    } else {
        unset ($_SESSION['login']);
        unset ($_SESSION['senha']);
        header("Location: login2.php?i=n");
    }
}
}

HTML 表单

<form method="post" action="login.php" class="cbp-mc-form" autocomplete="off">

<label for="login">Login</label>
<input type="text" name="login" id="login" /><br />
<label for="senha">Senha</label>
<input type="password" name="senha" id="senha" /><br />
<center><input class="cbp-mc-submit" type="submit" value="Login""/></center>
</form>

最佳答案

亲爱的兄弟试试下面的代码,(我编辑了你的代码) 我希望它适用于您的情况,但如果您在生产中使用相同的代码,请注意清理。

我为您编辑的代码如下(如果仍然不起作用,则您的数据库连接可能存在一些错误)。

PHP 脚本:

<?php
session_start(); // better to start the session in the begining, 
                 //in some cases it doesn't work in the mid of the document'
include 'conexao.php';

if (isset($_POST['login']) && isset($_POST['senha'])) // check if both the form fields are set or not
{
    // Values coming from the user through FORM
    $login_form = $_POST['login'];
    $senha_form = $_POST['senha'];

    // query the database only when user submit the form with all the fields filled
    $result = mysql_query("SELECT * FROM usuario WHERE login='$login_form' AND senha='$senha_form'") or die (mysql_error());
    while ($row = mysql_fetch_assoc($result)) 
    {
        // values coming from Database
        $login_db = $row['login'];
        $senha_db = $row['senha'];
    }

    // compare the values from db to the values from form
    if ($login_form == $login_db && $senha_form == $senha_db) 
    {
        // Set the session only if user entered the correct username and password
        // it doesn't make sense to set session even if the user entered wrong values
        $_SESSION['login'] = $login_db;
        $_SESSION['senha'] = $senha_db;
        header("Location: index.php");
    }
    else
    {
        header("Location: login2.php?i=n");
    }

}
?>

HTML:(正是复制的 html)

<form method="post" action="login.php" class="cbp-mc-form" autocomplete="off">
<label for="login">Login</label>
<input type="text" name="login" id="login" /><br />
<label for="senha">Senha</label>
<input type="password" name="senha" id="senha" /><br />
<center><input class="cbp-mc-submit" type="submit" value="Login""/></center>
</form>

关于php - 使用 DB 登录 PHP,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22278252/

相关文章:

mysql - Spark 应用程序中的 Hive 中的 "Unable to alter partition"

php - Eloquent : Joining Many To Many, 获取一个字段

mysql - 如何在 IF 或 CASE WHEN 语句中选择多个值

javascript - fabric js 从 mysql 数据库加载单个对象到 Canvas

php - 在 mysqli 中带参数执行

php - 如何更改 PHP 中的 NLS_DATE_FORMAT

php - PHP 中的对象比较

php - mySQL 在数组中搜索 id

php - Laravel:从另一个数据库表获取数据

php - 使用php从mysqli数据库查询构建多维数组