有人可以舉例說明如何使用PDO檢查數據庫中是否存在電子郵件和密碼? (對於DUMMIES)。

[英]Can someone make an example of how to check if email and password exists in database with PDO? (FOR DUMMIES).


Heres is all relevant code. What I want to do is sent some parameters to a php file using ajax, and check if they are in db. If not echo and error message and if they are echo success message. I don't know how to retrieve all result from the database and compare them with the ones I input. Thank you.

Heres是所有相關的代碼。我想要做的是使用ajax將一些參數發送到php文件,並檢查它們是否在db中。如果不是echo和錯誤消息,並且它們是echo成功消息。我不知道如何從數據庫中檢索所有結果,並將它們與我輸入的結果進行比較。謝謝。

AJAX

$( document ).ready(function() {    
  $("input[name=send_login]").click(function() {
         var value_email_login = $("#email_login").val() ;
         var value_password_login = $("#password_login").val() ;

         if(value_email_login == '' && value_password_login == ''){
             $(".info_message_login").show();
             $(".info_message_login").html("Campos Vacios");
         }else{
             $.ajax({
                 type: 'POST',
                 url: 'php/login.php',
                 data: {email: value_email_login, password: value_password_login},
                 success: function(){
                     $(".info_message_login").show();
                     $(".info_message_login").text("Bienvenido...");
                 },
                 error: function(response){
                     $(".info_message_login").show();
                     $(".info_message_login").text(response.responseText);
                  },
             });
         }
   });
});

FORM

<form class="" action="" method="post">
    <div class="form-group">
      <input class="form-control" type="text" name="email" id="email_login" placeholder="Email"/>
    </div>
    <div class="form-group">
      <input class="form-control" type="password" name="password" id="password_login" placeholder="Password"/>
    </div>
    <div class="form-group">
      <input class=" btn btn-default " type="button" name="send_login" value="Send"/>
    </div>
    <div class="form-group">
      <input class=" btn btn-default " type="reset" name="reset_login" value="Reset"/>
    </div>
    <div class="info_message_login" style="display:none;">

    </div>
</form>

PHP FILE

 <?php
session_start();

require_once "db.php";

$email = trim($_POST['email']);
$password = trim($_POST['password']);

$sql_login = 'SELECT * FROM users WHERE email = :email AND password = :password';
$statement = $connection->prepare($sql_login);

$statement->bindValue(':email', $email);
$statement->bindValue(':password', $password);


$statement->execute()

1 个解决方案

#1


$statement = $connection->prepare(<<<SQL
    SELECT * 
    FROM users 
    WHERE email = :email 
    AND password = :password
SQL
);

$statement->execute(array(":email" => $email, 
                          ":password" => $password));

if($statement->fetch()) {
    $_SESSION['login'] = 'ok';
}

I let you hash passwords with sha1 or whatever you want

我讓你用sha1或任何你想要的密碼哈希密碼


注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2015/05/04/e2103c6537338dbf5ce375bd4e03ba8b.html



 
粤ICP备14056181号  © 2014-2021 ITdaan.com