PHP scripts are not working except the Connection - php

I am developing a login and registration application in android studio by looking at some of video tutorials,and the php scripts are 100% as in the tutorial and it was working at first time, then now its not working.I can't identify what's gone wrong.. I am new to php and android
In the tutorial it's purely working.
Tutorial link >>Registration app tutorial
spent more than two days for linking database, still stucked over, finally into stackoverflow, Hope for good Solution or Guidance. Thanks
init.php which is for Connection, and connection Success
<?php
$host = "localhost";
$user = "blood";
$password = "rifkan123";
$dbname = "userdb";
$con = mysqli_connect($host,$user,$password,$dbname);
if(!$con)
{
die("Error in Database Connection".mysqli_connect_error());
}
else
{
Echo "<h3> Database Connection Success !";
}
?>
login.php Script for login
<?php
$email = $_POST["email"];
$pass = $_POST["password"];
require "init.php";
$query = "Select * userinfo where email like. '".$email."';";
$result = mysqli_query($con,$query);
if(mysqli_num_rows($result)>0)
{
$response = array();
$code ="login_true";
$row = mysqli_fetch_array($result);
$name = $row[0];
$message ="Login Success ! Welcome ".$name;
array_push($response,array("code"=>$code,"message"=>$message));
echo json_encode(array(server_response=>$response));
}
else
{
$response = array();
$code ="login_false";
$message ="Login Failed ! Try Again";
array_push($response,array("code"=>$code,"message"=>$message));
echo json_encode(array(server_response=>$response));
}
mysqli_close($con);
?>
register.php for user registration
<?php
$name = $_POST["name"];
$email = $_POST["email"];
$pass = $_POST["password"];
require "init.php";
$query = "select * from userinfo where email like '".$email."';";
$result = mysqli_query($con,$query);
if(mysqli_num_rows($result)>0)
{
$response = array();
$code = "reg_false";
$message = "User Already Exist !";
array_push($response,array("code"=>$code,"message"=>$message));
echo json_encode(array("server_response"=>$response));
}
else
{
$query = "insert into userinfo values('".$name."','".$email."','".$pass."');";
$result = mysqli_query($con,$query);
if(!$result)
{
$response = array();
$code = "reg_false";
$message = "Registration Failed, Try Again!";
array_push($response,array("code"=>$code,"message"=>$message));
echo json_encode(array("server_response"=>$response));
}
else
{
$response = array();
$code = "reg_true";
$message = "Registration Success, Login to Continue!";
array_push($response,array("code"=>$code,"message"=>$message));
echo json_encode(array("server_response"=>$response));
}
mysqli_close($con);
}
?>
I have missed the FROM in login script and after adding it new error detected
Screen shot new Error

You forgot FROM clause in login.php
Here
$query = "Select * from userinfo where email like. '".$email."';";
Also consider to add %% with like

Related

How do I echo message at the top of the form, after performing insertion, update and delete on same page, while using header("location:$url");

How do I echo message at the top of the form, after performing insertion, update and delete on same page, while using header("location:$url");
if($_SERVER["REQUEST_METHOD"] == "GET"){
if(isset($_GET['id1'])){
$Id1 = base64_decode($_GET['id1']);
$qry = "SELECT Name,Description,Role FROM cms WHERE id='$Id1'";
$res = mysqli_query($conn, $qry);
$res1 = mysqli_fetch_assoc($res);
$uname = $res1['Name'];
$address = $res1['Description'];
$role1 = $res1['Role'];
}
}
if(isset($_POST['update']))
{
if(isset($_GET['id1']))
{
$id1=base64_decode($_GET['id1']);
$uname = $_POST['uname'];
$address = $_POST['address'];
$role = $_POST['role'];
$qry2 = "UPDATE cms SET Name ='$uname', Description = '$address',Role='$role' WHERE
id='$id1'";
$res2 = mysqli_query($conn,$qry2);
if(mysqli_error($conn))
{
echo "error";
} else {
$_SESSION['success'] = "Record Updated Successfully!";
header("location:admin.php");
}
}
}
?>
I tried running the above code in HTML snippet to display the echo message on my webpage but it does not echo the message with header tag

How does php and condition works

I'm retrieving users from my database with this php code below.
The problem is that when I execute the query without the AND condition, it works fine. With the AND condition it gives me always the else result.
include("config.php");
$con=mysqli_connect($host,$username,$pwd,$db) or die('Unable to connect');
if(mysqli_connect_error($con))
{
echo "Failed to Connect to Database ".mysqli_connect_error();
}
$contact = mysqli_real_escape_string($con, $_GET['contact']);
$password = mysqli_real_escape_string($con, $_GET['motdepasse']);
$sql = "SELECT * FROM bonfoodUtilisateurs WHERE contact = '$contact' AND motdepasse = '$password'";
$result = mysqli_query($con,$sql);
$response = array();
if (mysqli_num_rows($result)!=0) {
$response['code'] = '1';
$response['message'] = 'success message.';
echo json_encode($response);
mysqli_close($con);
}else{
$response['code'] = '2';
$response['message'] = 'error message.';
echo json_encode($response);
mysqli_close($con);
}
?>
I'm expecting to get a result like ```{"code":"2","message":"error message."}
if the user's ```contact and ```password don't exist in the data table
otherwise get ```{"code":"1","message":"success message."}

How to throw an error for Invalid username or password if the data is not matched with the DB?

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);
?>
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);
?>

I have created a admin panel in which user registration but it always showing me something went wrong error message

<?php
if(isset($_POST['btn-signup'])) {
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "test";
$conn = new mysqli($servername, $username, $password, $dbname);
if($conn->connect_error)
{
die("Connection failed :" . $conn->connect_error);
}
$uname = trim($_POST['uname']);
$email = trim($_POST['email']);
$upass = trim($_POST['pass']);
$mobile = trim($_POST['mobile']);
$fee = trim($_POST['fee']);
$uname = strip_tags($uname);
$email = strip_tags($email);
$upass = strip_tags($upass);
$mobile = strip_tags($mobile);
$fee = strip_tags($fee);
$role = "user";
// check email exist or not
$query = "SELECT email FROM users WHERE email='$email'";
$result = mysqli_query($query);
$count = mysqli_num_rows($result); // if email not found then proceed
if ($count==0) {
$query = "INSERT INTO users(username,email,password,mobile,fee,role) VALUES('$uname','$email','$upass','$mobile','$fee','$role')";
$res = mysqli_query($query);
if ($res) {
$errTyp = "success";
$errMSG = "successfully registered, you may login now";
} else {
$errTyp = "danger";
$errMSG = "Something went wrong, try again later..." .mysql_error();
}
} else {
$errTyp = "warning";
$errMSG = "Sorry Email already in use ...";
}
mysqli_close($conn);
}
?>
whenever i click on submit button it always give me "something went wrong error" even i check for the error that has to be "sorry email already in use" but it always showing "something went wrong".
i use session also. i am newbie in php and creating a session based login system for different role such as admin and student.
please give me solution as soon as possible thank you :)
Use only "mysqli_" or "mysql_", recommended is "mysqli_"
You are connecting database with "mysqli_" and fetching the result set using "mysql_"
Learn the Difference MYSQL vs MYSQLi vs PDO
I have seen your code.
Please check the syntax of Insert query once , print the query and run in mysql db and see if it run fine or not.
I think this will resolve your issue.

PHP password_verify always returns false

I´ve been working on an Android app which registers and logins users to a remote MySQL database. I have double-double checked my Android code and the information sent from the app to PHP.
Apparently, the problem is somewhere in the login.php file. Users are registered successfully, but I am unable to login after that.
My DB has 3 fields:
id / int(11)
email / varchar(255)
password /char(60)
Registering a user is giving me 60 characters hashes for the password field. So far, so good. But then when I try to login, I always get a failure response.
I'm using https://github.com/ircmaxell/password_compat since I'm limited to PHP 5.4
Here is my login PHP code:
<?php
if($_SERVER['REQUEST_METHOD']=='POST'){
$email = $_POST['email'];
$passwordFromPost = $_POST['password'];
require_once('dbconnect.php');
$sql = "SELECT password FROM users WHERE email = '$email'";
$hash = mysqli_query($con, $sql);
require_once('lib/password.php');
if (password_verify($passwordFromPost, $hash)) {
echo 'success';
} else {
echo 'failure';
}
}
?>
And just in case, here is my registration PHP code:
<?php
if($_SERVER['REQUEST_METHOD']=='POST'){
$email = $_POST['email'];
$passwordFromPost = $_POST['password'];
require_once('lib/password.php');
$hash = password_hash($passwordFromPost, PASSWORD_BCRYPT);
if($hash) {
require_once('dbconnect.php');
$sql = "INSERT INTO users (email, password) VALUES ('$email','$hash')";
if(mysqli_query($con, $sql)){
echo "Registered succesfully";
} else {
echo "Unable to register the account";
}
} else {
echo 'error';
}
} else {
echo 'error';
}
?>
Any help will be much appreciated. Thank you.
You forgot to fetch your row. As such you were not verifying against a string.
<?php
$result = mysqli_query($con, $sql);
$row = mysqli_fetch_array($result);
password_verify($passwordFromPost, $row['password']);
You need to fetch the row:
<?php
if($_SERVER['REQUEST_METHOD']=='POST'){
$email = $_POST['email'];
$passwordFromPost = $_POST['password'];
require_once('dbconnect.php');
$sql = "SELECT password FROM users WHERE email = '$email'";
$hash = mysqli_query($con, $sql);
while ($row = mysqli_fetch_row($hash)) {
require_once('lib/password.php');
if (password_verify($passwordFromPost, $row["password"])) {
echo 'success';
} else {
echo 'failure';
}
}
}
?>
Reference: http://php.net/manual/en/mysqli-result.fetch-row.php
Your login php file should be
<?php
if($_SERVER['REQUEST_METHOD']=='POST'){
$email = $_POST['email'];
$passwordFromPost = $_POST['password'];
require_once('dbconnect.php');
$sql = "SELECT password FROM users WHERE email = '$email'";
$rst = mysqli_query($con, $sql);
$hash = mysqli_fetch_row($rst);
require_once('lib/password.php');
if (password_verify($passwordFromPost, $hash['password'])) {
echo 'success';
} else {
echo 'failure';
}
}
?>

Categories