如果数据与数据库不匹配,如何为无效的用户名或密码抛出错误?(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); ?>更多推荐
发布评论