如果数据与数据库不匹配,如何为无效的用户名或密码抛出错误?(How to throw an error for Invalid username or password if the data is

编程入门 行业动态 更新时间:2024-10-27 06:27:37
如果数据与数据库不匹配,如何为无效的用户名或密码抛出错误?(How to throw an error for Invalid username or password if the data is not matched with the DB?)

我很新的PHP,我有一个基于MySql Php的后端连接到一个反应本机应用程序。

我正在处理无效的用户名和密码,但正确的用户名/密码与错误的用户名/密码,反之亦然是应用程序获取强制关闭(崩溃),因为它不能输入的数据与数据库中不匹配。

我能知道我该怎么做到这一点?

以下是我的相同的PHP代码。

<?php include 'DBConfig.php'; $con = mysqli_connect($HostName,$HostUser,$HostPass,$DatabaseName); $json = file_get_contents('php://input'); $obj = json_decode($json,true); $email = $obj['email']; $password = $obj['password']; $Sql_Query = "select * from UserRegistrationTable where email = '$email' and password = '$password' "; $check = mysqli_fetch_array(mysqli_query($con,$Sql_Query)); if(isset($check)){ $SuccessLoginMsg = 'Data Matched'; $SuccessLoginJson = json_encode($SuccessLoginMsg); echo $SuccessLoginJson ; $response=array(); array_push($response,array("name"=>$check[1], "email"=>$check[2], "password"=>$check[3]); echo json_encode(array("Details"=>$response)); } else{ $InvalidMSG = 'Invalid Username or Password Please Try Again' ; $InvalidMSGJSon = json_encode($InvalidMSG); echo $InvalidMSGJSon ; } mysqli_close($con); ?>

I am pretty new with php, I have a MySql Php based backend which connects to a react native application.

I am handling invalid username and password but right username/password with wrong username/password and vice versa is where the app is getting force close (Crashed) as it cannot the entered data are not matching with the one that is in the database.

Can I know how can I achieve this?

Below is my php code for the same.

<?php include 'DBConfig.php'; $con = mysqli_connect($HostName,$HostUser,$HostPass,$DatabaseName); $json = file_get_contents('php://input'); $obj = json_decode($json,true); $email = $obj['email']; $password = $obj['password']; $Sql_Query = "select * from UserRegistrationTable where email = '$email' and password = '$password' "; $check = mysqli_fetch_array(mysqli_query($con,$Sql_Query)); if(isset($check)){ $SuccessLoginMsg = 'Data Matched'; $SuccessLoginJson = json_encode($SuccessLoginMsg); echo $SuccessLoginJson ; $response=array(); array_push($response,array("name"=>$check[1], "email"=>$check[2], "password"=>$check[3]); echo json_encode(array("Details"=>$response)); } else{ $InvalidMSG = 'Invalid Username or Password Please Try Again' ; $InvalidMSGJSon = json_encode($InvalidMSG); echo $InvalidMSGJSon ; } mysqli_close($con); ?>

最满意答案

您好,我更正了您的代码友善地检查下面 -

<?php include 'DBConfig.php'; $con = mysqli_connect($HostName,$HostUser,$HostPass,$DatabaseName); $json = file_get_contents('php://input'); $obj = json_decode($json,true); $email = $obj['email']; $password = $obj['password']; $Sql_Query = "select * from UserRegistrationTable where email = '$email' and password = '$password' "; $res = mysqli_query($con,$Sql_Query); if(mysqli_num_rows($res)> 0){ $check = mysqli_fetch_array($res); $SuccessLoginMsg = 'Data Matched'; $SuccessLoginJson = json_encode($SuccessLoginMsg); echo $SuccessLoginJson ; $response=array(); array_push($response,array("name"=>$check[1], "email"=>$check[2], "password"=>$check[3]); echo json_encode(array("Details"=>$response)); } else{ $InvalidMSG = 'Invalid Username or Password Please Try Again' ; $InvalidMSGJSon = json_encode($InvalidMSG); echo $InvalidMSGJSon ; } mysqli_close($con); ?>

Hi i corrected your code kindly check below-

<?php include 'DBConfig.php'; $con = mysqli_connect($HostName,$HostUser,$HostPass,$DatabaseName); $json = file_get_contents('php://input'); $obj = json_decode($json,true); $email = $obj['email']; $password = $obj['password']; $Sql_Query = "select * from UserRegistrationTable where email = '$email' and password = '$password' "; $res = mysqli_query($con,$Sql_Query); if(mysqli_num_rows($res)> 0){ $check = mysqli_fetch_array($res); $SuccessLoginMsg = 'Data Matched'; $SuccessLoginJson = json_encode($SuccessLoginMsg); echo $SuccessLoginJson ; $response=array(); array_push($response,array("name"=>$check[1], "email"=>$check[2], "password"=>$check[3]); echo json_encode(array("Details"=>$response)); } else{ $InvalidMSG = 'Invalid Username or Password Please Try Again' ; $InvalidMSGJSon = json_encode($InvalidMSG); echo $InvalidMSGJSon ; } mysqli_close($con); ?>

更多推荐

本文发布于:2023-07-28 23:40:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1310263.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:抛出   何为   不匹配   用户名   错误

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!