Click here to Skip to main content
15,902,276 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm coding a CRUD functions with php (Web Service) to connect my android application to Mysql database , i getting an error message mean that username and password are incorrect when i try executing the code of my php login function .

DbOperation.php
PHP
function createUser($id_user, $nom, $prenom, $email, $password){
	//	$password = md5($password);
		$stmt = $this->con->prepare("INSERT INTO users (id_user, nom, prenom, email, password) VALUES (?, ?, ?, ?, ?)");
		$stmt->bind_param("issss", $id_user, $nom, $prenom, $email, $password);
		if($stmt->execute())
			return true; 
		return false; 
	}
			public function userLogin($id_user, $password){
			$password = md5($password);
			$stmt = $this->con->prepare("SELECT id_user FROM users WHERE id_user = ? AND password = ?");
			$stmt->bind_param("is",$id_user,$password);
			$stmt->execute();
			$stmt->store_result(); 
			return $stmt->num_rows > 0; 
		}
	public function getUserById($id_user){
			$stmt = $this->con->prepare("SELECT * FROM users WHERE id_user = ?");
			$stmt->bind_param("i",$id_user);
			$stmt->execute();
			return $stmt->get_result()->fetch_assoc();
		}



UserLogin.php

PHP
 <?php 

require_once '../includes/DbOperation.php';

$response = array(); 

if($_SERVER['REQUEST_METHOD']=='POST'){
	if(isset($_POST['id_user']) and isset($_POST['password'])){
		$db = new DbOperation(); 

		if($db->userLogin($_POST['id_user'], $_POST['password'])){
			$user = $db->getUserById($_POST['id_user']);
			$response['error'] = false; 
			$response['id_user'] = $user['id_user'];
			$response['password'] = $user['password'];
		}else{
			$response['error'] = true; 
			$response['message'] = "Invalid Id or Password";			
		}

	}else{
		$response['error'] = true; 
		$response['message'] = "Required fields are missing";
	}
}

echo json_encode($response);


Api.php

PHP
case 'getuserbyid':
				isTheseParametersAvailable(array('id_user'));
				$db = new DbOperation();
				$response['error'] = false; 
				$response['message'] = 'Request successfully completed';
				$response['users'] = $db->getUserById($_POST['id_user']);
			break;


What I have tried:

I try to execute the code of UserLogin.php on postman , i enter values but i get this message :

Invalid Id or Password

But my id_user and password are correct
Posted
Updated 23-Dec-18 1:18am
v2

1 solution

Don't use MD5, use proper password hashing as provided by PHP. See php password hash - Google Search[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900