Php 使用PDO登录不起作用
我正在使用PDO并创建一个登录脚本,但该脚本无法工作?这是我的密码Php 使用PDO登录不起作用,php,mysql,pdo,Php,Mysql,Pdo,我正在使用PDO并创建一个登录脚本,但该脚本无法工作?这是我的密码 <?php session_start(); include 'conn.php'; if(isset($_POST) && count($_POST)>0) { try { $user=$_POST['user']; $pass = $_POST['pass']; $secure=password_verify($pass,PASSWORD_DEFAULT); $qu
<?php session_start();
include 'conn.php';
if(isset($_POST) && count($_POST)>0) {
try
{
$user=$_POST['user'];
$pass = $_POST['pass'];
$secure=password_verify($pass,PASSWORD_DEFAULT);
$query = "SELECT COUNT(*) FROM signup WHERE Username=:users AND Password=:pass";
$result=$conn->prepare($query);
$result->execute(array(':users'=>$user,':pass'=>$secure));
if($result->fetchColumn()>0)
{
// Set username session variable
$_SESSION['user'] = $user;
// Jump to secured page
header('location:index.php');
}
else
{
header("location:signin.php");
}
}
catch(PDOException $e){
echo 'ERROR', $e->getMessage();
}
}
?>
为什么它不起作用?我也尝试了Select*from注册,但也没有帮助,有人能告诉我在上面的代码中替换什么吗
我的conn.php代码是
<?php
$user='root';
$password='';
$dns='mysql:host=localhost;dbname=leave_system';
try {
$conn=new pdo($dns,$user,$password);
} catch(PDOException $e) {
echo 'ERROR: ' . $e->getMessage();
}
?>
我的register.php代码是
<?php session_start();
include 'conn.php';
$fname=$_POST['fname'];
$lname=$_POST['lname'];
$user=$_POST['uname'];
$email=$_POST['email'];
$depart=$_POST['department'];
$id=$_POST['id'];
$phone=$_POST['phone'];
$pass=$_POST['pass'];
$cpass=$_POST['cpass'];
$address=$_POST['address'];
$securepassword=password_hash($cpass,PASSWORD_DEFAULT);
$qry="INSERT INTO signup (First_Name,Last_Name,Username,Email,Department,Employe_Id,Phone,Password,Address) VALUES (:fname,:lname,:user,:email,:depart,:id,:phone,:securepassword,:address)";
$stm=$conn->prepare($qry);
$stm->execute(array(':fname'=>$fname,':lname'=>$lname,':user'=>$user,':email'=>$email,':depart'=>$depart,':id'=>$id,':phone'=>$phone,':securepassword'=>$securepassword,':address'=>$address));
if($stm)
{
header('location:index.php');
}
?>
它不工作怎么办?为了缩小问题范围,您对其进行了多大程度的调试?你有错误吗?conn.php
的内容是什么?请发布它们。不会显示错误,登录也不会工作。。。我的连接代码是…请编辑以发布上面的连接代码,而不是在注释中。请参阅。您的prepare()/execute()
包装在try/catch
中,但是PDO在默认情况下不会抛出异常(初始连接除外)。您必须将其设置为通过PDO::ERRMODE_EXCEPTION
抛出异常,否则它将自动失败。