JSON works on localhost but not on server -mysqlndrivers are updated - php

I am creating a web service for android and PHP registration process.
I am following this tutorial http://www.androidhive.info/2012/01/android-login-and-registration-with-php-mysql-and-sqlite/
The Issue is: My JSON works on localhost but not on a server. On a server it gives me this error but it stores the data on database successfully.
Fatal error: Call to undefined method mysqli_stmt::get_result() in /home/pmc/public_html/zeusonline.me/zeus/include/DB_Functions.php on line 45
and the line 45 is:
$user = $stmt->get_result()->fetch_assoc();
Code samples->
Register.php file
<?php
require_once 'include/DB_Functions.php';
$db = new DB_Functions();
// json response array
$response = array("status" => TRUE);
if (isset($_GET['username']) && isset($_GET['email']) &&
isset($_GET['country']) && isset($_GET['phone_number']) &&
isset($_GET['password']) && isset($_GET['lat']) && isset($_GET['lon'])) {
// receiving the post params
$username = $_GET['username'];
$email = $_GET['email'];
$country = $_GET['country'];
$phone_number = $_GET['phone_number'];
$password = $_GET['password'];
$lat = $_GET['lat'];
$lon = $_GET['lon'];
// check if user is already existed with the same email
if ($db->isUserExisted($email)) {
// user already existed
$response["status"] = FALSE;
$response["error_msg"] = "User already existed with " . $email;
echo json_encode($response);
}
else if ($db->isUserExisted($phone_number)) {
// user already existed
$response["status"] = FALSE;
$response["error_msg"] = "User already existed with " . $phone_number;
echo json_encode($response);
}
else {
// create a new user
$user = $db->storeUser($username, $email, $country , $phone_number,
$password, $lat, $lon);
if ($user) {
// user stored successfully
$response["status"] = TRUE;
$response["user"]["uid"] = $user["unique_id"];
$response["user"]["username"] = $user["username"];
$response["user"]["email"] = $user["email"];
$response["user"]["country"] = $user["country"];
$response["user"]["phone_number"] = $user["phone_number"];
$response["user"]["country"] = $user["country"];
$response["user"]["height"] = $user["height"];
$response["user"]["weight"] = $user["weight"];
$response["user"]["is_number_verified"] =
$user["is_number_verified"];
$response["user"]["is_safe"] = $user["is_safe"];
$response["user"]["is_login"] = $user["is_login"];
$response["user"]["lat"] = $user["lat"];
$response["user"]["lon"] = $user["lon"];
$response["user"]["created_at"] = $user["created_at"];
echo json_encode($response);
} else {
// user failed to store
$response["Status"] = False;
$response["error_msg"] = "Unknown error occurred in registration!";
echo json_encode($response);
}
}
} else {
$response["Status"] = TRUE;
$response["error_msg"] = "Required parameters are missing!";
echo json_encode($response);
}
?>
DB_Functions.php file
<?php
class DB_Functions {
private $conn;
// constructor
function __construct() {
require_once 'DB_Connect.php';
// connecting to database
$db = new Db_Connect();
$this->conn = $db->connect();
}
// destructor
function __destruct() {
}
/**
* Storing new user
* returns user details
*/
public function storeUser($username, $email, $country, $phone_number,
$password, $lat, $lon) {
$uuid = uniqid('', true);
$height = 0;
$weight = 0;
$is_number_verified = False;
$is_safe = True;
$is_login = False;
$hash = $this->hashSSHA($password);
$encrypted_password = $hash["encrypted"]; // encrypted password
$salt = $hash["salt"]; // salt
$stmt = $this->conn->prepare("INSERT INTO users (unique_id, username,
email, country, phone_number, password, salt, height, weight,
is_number_verified, is_safe, is_login, lat, lon, created_at) VALUES(?, ?, ?,
?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?,NOW())");
$stmt->bind_param("ssssssssssssss", $uuid, $username, $email, $country,
$phone_number, $encrypted_password, $salt, $height, $weight,
$is_number_verified, $is_safe, $is_login, $lat, $lon);
$result = $stmt->execute();
$stmt->close();
// check for successful store
if ($result) {
$stmt = $this->conn->prepare("SELECT * FROM users WHERE phone_number
= ?");
$stmt->bind_param("s", $phone_number);
$stmt->execute();
$user = $stmt->get_result()->fetch_assoc();
$stmt->close();
return $user;
} else {
return false;
}
}
}
?>
The URL for testing :
http://pakmanzil.com/zeusonline.me/zeus/register.php?username=bilal&email=bi#gmail.com&country=england&phone_number=03333524145&password=123&lat=0.0&lon=0.0
Change the email and phone_number to make it works please :)

Related

Registration is not showing responded message

I'm working on Php web services which are working on localhost when I upload it to Cpanel server it is not responding. it is used is registered but response message is not showing while checking with postman
DB_Function has the main function where it is connected to table to insert data.Which is working.
DB_Function.php
<?php
class DB_Functions {
private $conn;
// constructor
function __construct() {
require_once 'DB_Connect.php';
// connecting to database
$db = new Db_Connect();
$this->conn = $db->connect();
}
// destructor
function __destruct() {
}
/**
* Storing new user
* returns user details
*/
public function storeUser($name, $email, $password, $phone, $address, $address_2,$education, $position, $gender, $bank_account_no, $experience, $company_name, $company_temp, $references_description, $amount ) {
//$uuid = uniqid('', true);
$hash = $this->hashSSHA($password);
$encrypted_password = $hash["encrypted"]; // encrypted password
$salt = $hash["salt"]; // salt
$stmt = $this->conn->prepare("INSERT INTO emp_registration( full_name, email, password, salt, phone, address, address_2, education, position, gender, bank_account_no, experience, company_name, company_temp,references_description,amount, created_at) VALUES(?,?, ?, ?, ?, ?, ?, ?,?,?,?,?,?,?,?,?, NOW())");
$stmt->bind_param("sssssssssssssssi", $name, $email, $encrypted_password, $salt, $phone, $address, $address_2,$education, $position, $gender, $bank_account_no, $experience, $company_name, $company_temp, $references_description, $amount );
$result = $stmt->execute();
$stmt->close();
// check for successful store
if ($result) {
$stmt = $this->conn->prepare("SELECT * FROM emp_registration WHERE email = ?");
$stmt->bind_param("s", $email);
$stmt->execute();
$user = $stmt->get_result()->fetch_assoc();
$stmt->close();
return $user;
} else {
return false;
}
}
/**
* Get user by email and password
*/
public function getUserByEmailAndPassword($email, $password) {
$stmt = $this->conn->prepare("SELECT * FROM emp_registration WHERE email = ?");
$stmt->bind_param("s", $email);
if ($stmt->execute()) {
$user = $stmt->get_result()->fetch_assoc();
$stmt->close();
// verifying user password
$salt = $user['salt'];
$encrypted_password = $user['password'];
$hash = $this->checkhashSSHA($salt, $password);
// check for password equality
if ($encrypted_password == $hash) {
// user authentication details are correct
return $user;
}
} else {
return NULL;
}
}
/**
* Check user is existed or not
*/
public function isUserExisted($email) {
$stmt = $this->conn->prepare("SELECT email from emp_registration WHERE email = ?");
$stmt->bind_param("s", $email);
$stmt->execute();
$stmt->store_result();
if ($stmt->num_rows > 0) {
// user existed
$stmt->close();
return true;
} else {
// user not existed
$stmt->close();
return false;
}
}
/**
* Encrypting password
* #param password
* returns salt and encrypted password
*/
public function hashSSHA($password) {
$salt = sha1(rand());
$salt = substr($salt, 0, 10);
$encrypted = base64_encode(sha1($password . $salt, true) . $salt);
$hash = array("salt" => $salt, "encrypted" => $encrypted);
return $hash;
}
/**
* Decrypting password
* #param salt, password
* returns hash string
*/
public function checkhashSSHA($salt, $password) {
$hash = base64_encode(sha1($password . $salt, true) . $salt);
return $hash;
}
}
?>
In Emp_registration it is sending store_user fuction in DB_Functions class but Response is not comming from it on server.
Emp_registration.php
<?php
require_once 'include/DB_Function.php';
$db = new DB_Functions();
// json response array
$response = array("error" => FALSE);
if (isset($_POST['name']) && isset($_POST['email']) && isset($_POST['password']) && isset($_POST['phone']) && isset($_POST['address']) && isset($_POST['address_2']) &&
isset($_POST['education']) && isset($_POST['position']) && isset($_POST['gender']) && isset($_POST['bank_account_no']) && isset($_POST['experience']) && isset($_POST['company_name']) && isset($_POST['company_temp']) && isset($_POST['references_description'])
&& isset($_POST['amount']) ) {
// receiving the post params
$name = $_POST['name'];
$email = $_POST['email'];
$password = $_POST['password'];
$phone = $_POST['phone'];
$address = $_POST['address'];
$address_2 = $_POST['address_2'];
$education = $_POST['education'];
$position = $_POST['position'];
$gender = $_POST['gender'];
$bank_account_no = $_POST['bank_account_no'];
$experience = $_POST['experience'];
$company_name = $_POST['company_name'];
$company_temp = $_POST['company_temp'];
$references_description = $_POST['references_description'];
$amount = intval( $_POST['amount']);
// check if emp_registration is already existed with the same email
if ($db->isUserExisted($email)) {
// emp_registration already existed
$response["error"] = TRUE;
$response["error_msg"] = "User already existed with " . $email;
echo json_encode($response);
} else {
// create a new emp_registration
$emp_registration = $db->storeUser($name, $email, $password, $phone, $address, $address_2, $education, $position, $gender, $bank_account_no, $experience, $company_name, $company_temp, $references_description, $amount);
if ($emp_registration) {
// emp_registration stored successfully
$response["error"] = FALSE;
$response["emp_id"] = $emp_registration["id"];
$response["emp_registration"]["full_name"] = $emp_registration["full_name"];
$response["emp_registration"]["email"] = $emp_registration["email"];
$response["emp_registration"]["created_at"] = $emp_registration["created_at"];
$response["emp_registration"]["updated_at"] = $emp_registration["updated_at"];
echo json_encode($response);
} else {
// emp_registration failed to store
$response["error"] = TRUE;
$response["error_msg"] = "Unknown error occurred in registration!";
echo json_encode($response);
}
}
} else {
$response["error"] = TRUE;
$response["error_msg"] = "Required parameters (name, email or password) is missing!";
echo json_encode($response);
}
?>
.htaccess
RewriteEngine on
RewriteCond %{HTTP_HOST} ^web\.ddagroindore\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.web\.ddagroindore\.com$
RewriteRule ^/?$ "http\:\/\/ddagroindore\.com\/webservice" [R=301,L]

php null values on bind_result

I have this PHP file stored i a server. It creates a new user for the DB. The registration is success but the response message is always NULL.
Here is my register.php file where i post the values
<?php
require_once 'DB_Functions.php';
$db = new DB_Functions();
// json response array
$response = array("error" => FALSE);
if (($_POST['name']) && ($_POST['surname']) && ($_POST['email']) && ($_POST['password']) && ($_POST['telephone'] && ($_POST['country']) ) )
{
// receiving the post params
$name = $_POST['name'];
$surname = $_POST['surname'];
$email = $_POST['email'];
$password = $_POST['password'];
$telephone = $_POST['telephone'];
$country = $_POST['country'];
// check if user is already existed with the same email
if ($db->isOwnerExisted($email)) {
// user already existed
$response["error"] = TRUE;
$response["error_msg"] = "User already exists with " . $email;
echo json_encode($response);
} else {
// create a new user
$user = $db->storeOwner($name, $surname, $email, $password, $telephone, $country);
if ($user) {
// user stored successfully
$response["error"] = FALSE;
$response["oid"] = $user["oid"];
$response["user"]["name"] = $user["name"];
$response["user"]["surname"] = $user["surname"];
$response["user"]["country"] = $user["country"];
$response["user"]["email"] = $user["email"];
$response["user"]["password"] = $user["password"];
$response["user"]["telephone"] = $user["telephone"];
echo json_encode($response);
} else {
// user failed to store
$response["error"] = TRUE;
$response["error_msg"] = "Unknown error occurred in registration!";
echo json_encode($response);
}
}
} else {
$response["error"] = TRUE;
$response["error_msg"] = "Required parameters are missing!";
echo json_encode($response);
}
?>
And the storeOwner function
public function storeOwner($name, $surname, $email, $password, $telephone, $country) {
$hash = $this->hashSSHA($password);
$encrypted_password = $hash["encrypted"]; // encrypted password
$salt = $hash["salt"]; // salt
$stmt = $this->conn->prepare("INSERT INTO owner (oid, name, surname, country, email, password, salt, telephone) VALUES(?, ?, ?, ?, ?, ?, ?, ?)");
$stmt->bind_param("isssssss", $oid, $name, $surname, $country, $email, $encrypted_password, $salt, $telephone);
$result = $stmt->execute();
$stmt->close();
// check for successful store
if ($result) {
$stmt = $this->conn->prepare("SELECT * FROM owner WHERE email = ?");
$stmt->bind_param("s", $email);
$stmt->execute();
$user = $stmt->bind_result($user['oid'], $user['name'], $user['surname'], $user['country'], $user['email'], $user['password'], $user['salt'], $user['telephone']);
while ($stmt->fetch()) {
//printf("%s %s\n", $email, $password);
}
$stmt->close();
return $user;
} else {
return false;
}
}
The output is something like
{"error":false,"uid":null,"user":{"name":null,"surname":null,"country":null,"email":null,"password":null,"telephone":null}}
Why is every field null?
When you're fetching the user, you're currently overwriting the bound results with the response from that method:
$user = $stmt->bind_result($user['oid'], ...);
The method $stmt->bind_result() returns a boolean (true on success and false on error). So your code first sets the values and when that's done, it overwrites them with the result from the method (the boolean).
It should be:
$user = []; // You should create the array before using it.
$stmt->bind_result($user['oid'], ...);

Issues with my android login/registration PHP server

I need help with below codes,I can't figure out why I can't register an account on db.
Below my PHP scripts:
update_user_info.php
<?php
class update_user_info {
public function StoreUserInfo($fullname, $matno, $dept, $phone, $email, $password) {
$hash = $this->hashFunction($password);
$encrypted_password = $hash["encrypted"]; // encrypted password
$salt = $hash["salt"]; // salt
$stmt = $this->conn->prepare("INSERT INTO users(fullname, matno, dept, phone, email, encrypted_password, salt, created_at) VALUES(?, ?, ?, ?, ?, ?, ?, Now())");
$stmt->bind_param("ssssssss", $fullname, $matno, $dept, $phone, $email, $encrypted_password, $salt, $created_at);
$result = $stmt->execute();
$stmt->close();
// check for successful store
if ($result) {
$stmt = $this->conn->prepare("SELECT fullname, matno, dept, phone, email, encrypted_password, salt FROM users WHERE matno = ?");
$stmt->bind_param("s", $matno);
$stmt->execute();
$stmt-> bind_result($token2,$token3,$token4,$token5,$token6,$token7,$token8);
while ( $stmt-> fetch() ) {
$user["fullname"] = $token2;
$user["matno"] = $token3;
$user["dept"] = $token4;
$user["phone"] = $token5;
$user["email"] = $token6;
}
$stmt->close();
return $user;
} else {
return false;
}
}
public function hashFunction($password) {
$salt = sha1(rand());
$salt = substr($salt, 0, 10);
$encrypted = base64_encode(sha1($password . $salt, true) . $salt);
$hash = array("salt" => $salt, "encrypted" => $encrypted);
return $hash;
}
public function VerifyUserAuthentication($matno, $password) {
$stmt = $this->conn->prepare("SELECT fullname, matno, dept, phone, email, encrypted_password, salt FROM users WHERE matno = ?");
$stmt->bind_param("s", $matno);
if ($stmt->execute()) {
$stmt-> bind_result($token2,$token3,$token4,$token5,$token6,$token7,$token8);
while ( $stmt-> fetch() ) {
$user["fullname"] = $token2;
$user["matno"] = $token3;
$user["dept"] = $token4;
$user["phone"] = $token5;
$user["email"] = $token6;
$user["encrypted_password"] = $token7;
$user["salt"] = $token8;
}
$stmt->close();
// verifying user password
$salt = $token8;
$encrypted_password = $token7;
$hash = $this->CheckHashFunction($salt, $password);
// check for password equality
if ($encrypted_password == $hash) {
// user authentication details are correct
return $user;
}
} else {
return NULL;
}
}
public function checkHashFunction($salt, $password) {
$hash = base64_encode(sha1($password . $salt, true) . $salt);
return $hash;
}
public function CheckExistingUser($matno) {
$stmt = $this->conn->prepare("SELECT matno from users WHERE matno = ?");
$stmt->bind_param("s", $matno);
$stmt->execute();
$stmt->store_result();
if ($stmt->num_rows > 0) {
// user existed
$stmt->close();
return true;
} else {
// user not existed
$stmt->close();
return false;
}
}
}
?>
login.php
<?php
require_once 'update_user_info.php';
$db = new update_user_info();
// json response array
$response = array("error" => FALSE);
if (isset($_POST['matno']) && isset($_POST['password'])) {
// receiving the post params
$matno = $_POST['matno'];
$password = $_POST['password'];
// get the user by email and password
$user = $db->VerifyUserAuthentication($matno, $password);
if ($user != false) {
// user is found
$response["error"] = FALSE;
$response["uid"] = $user["unique_id"];
$response["user"]["fullname"] = $user["fullname"];
$response["user"]["email"] = $user["email"];
$response["user"]["matno"] = $user["matno"];
$response["user"]["dept"] = $user["dept"];
$response["user"]["phone"] = $user["phone"];
echo json_encode($response);
} else {
// user is not found with the credentials
$response["error"] = TRUE;
$response["error_msg"] = "Login credentials are wrong. Please try again!";
echo json_encode($response);
}
} else {
// required post params is missing
$response["error"] = TRUE;
$response["error_msg"] = "Required parameters email or password is missing!";
echo json_encode($response);
}
?>
running above on postman and puttin all the required parameters shows below error:
["error_msg"] = "Required parameters email or password is missing!";
register.php
<?php
require_once 'update_user_info.php';
$db = new update_user_info();
// json response array
$response = array("error" => FALSE);
if (isset($_POST['fullname']) && isset($_POST['matnum']) && isset($_POST['depart']) && isset($_POST['phone']) && isset($_POST['email']) && isset($_POST['passworded'])) {
// receiving the post params
$fullname = $_POST['fullname'];
$matno = $_POST['matnum'];
$email = $_POST['email'];
$dept = $_POST['depart'];
$phone = $_POST['phone'];
$password = $_POST['passworded'];
// check if user is already existed with the same email
if ($db->CheckExistingUser($matno)) {
// user already existed
$response["error"] = TRUE;
$response["error_msg"] = "User already existed with " . $matno;
echo json_encode($response);
} else {
// create a new user
$user = $db->StoreUserInfo($fullname, $matno, $dept, $phone, $email, $password);
if ($user) {
// user stored successfully
$response["error"] = FALSE;
$response["user"]["fullname"] = $user["fullname"];
$response["user"]["matno"] = $user["matno"];
$response["user"]["dept"] = $user["dept"];
$response["user"]["phone"] = $user["phone"];
$response["user"]["email"] = $user["email"];
echo json_encode($response);
} else {
// user failed to store
$response["error"] = TRUE;
$response["error_msg"] = "Unknown error occurred in registration!";
echo json_encode($response);
}
}
} else {
$response["error"] = TRUE;
$response["error_msg"] = "Required parameters (fullname, email or password) is missing!";
echo json_encode($response);
}
?>
Running above code in postman with all params filled shows below error:
$response["error_msg"] = "Required parameters (fullname, email or password) is missing!";
I must be doing something wrong. Thank you for your help.
issues resolved. on postman i needed to select x-wwww-form-urlencoded under body option for my script to work. thanks

Cannot insert data using bind param

In my application I am using 2 php files where one contains all the funtions required for the application and the another is to retrive and send the data back to the user
My first php file is DB_Functions.php
which contains the following code
<?php
class DB_Functions {
private $conn;
// constructor
function __construct() {
require_once 'DB_Connect.php';
// connecting to database
$db = new Db_Connect();
$this->conn = $db->connect();
}
/**
* Storing new user
* returns user details
*/
public function storeUser($name,$sex,$dob,$email,$college,$password,$latitude,$longitude,$pass) {
$uuid = uniqid('', true);
$hash = $this->hashSSHA($password);
$encrypted_password = $hash["encrypted"]; // encrypted password
$salt = $hash["salt"]; // salt
$stmt = $this->conn->prepare("INSERT INTO CFLASH_USERS(Name, sex, dob, mail, college, password, latitude, longitude, pass, salt, created_at) VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, NOW())");
$stmt->bind_param("ssssssssss", $name, $sex, $dob, $email, $college, $password, $latitude, $longitude, $pass, $salt);
$result = $stmt->execute();
$stmt->close();
// check for successful store
if ($result) {
$stmt = $this->conn->prepare("SELECT * FROM CFLASH_USERS WHERE mail = ?");
$stmt->bind_param("s", $email);
$stmt->execute();
$user = $stmt->get_result()->fetch_assoc();
$stmt->close();
return $user;
} else {
return false;
}
}
/**
* Encrypting password
* #param password
* returns salt and encrypted password
*/
public function hashSSHA($password) {
$salt = sha1(rand());
$salt = substr($salt, 0, 10);
$encrypted = base64_encode(sha1($password . $salt, true) . $salt);
$hash = array("salt" => $salt, "encrypted" => $encrypted);
return $hash;
}
/**
* Decrypting password
* #param salt, password
* returns hash string
*/
public function checkhashSSHA($salt, $password) {
$hash = base64_encode(sha1($password . $salt, true) . $salt);
return $hash;
}
}
?>
My second php file Register.php contains
<?php
require_once 'include/DB_Functions.php';
$db = new DB_Functions();
// json response array
$response = array("error" => FALSE);
if (isset($_POST['name']) && isset($_POST['email']) && isset($_POST['password'])) {
// receiving the post params
$name = $_POST['name'];
$sex = $_POST['sex'];
$dob = $_POST['dob'];
$email = $_POST['email'];
$college = $_POST['college'];
$password = $_POST['password'];
$latitude = $_POST['latitude'];
$longitude = $_POST['longitude'];
$pass = $_POST['pass'];
// create a new user
$user = $db->storeUser($name, $sex, $dob, $email, $college ,$password, $latitude, $longitude, $pass);
if ($user) {
// user stored successfully
$response["error"] = FALSE;
$response["uid"] = $user["mail"];
$response["user"]["name"] = $user["Name"];
$response["user"]["sex"] = $user["sex"];
$response["user"]["dob"] = $user["dob"];
$response["user"]["email"] = $user["mail"];
$response["user"]["college"] = $user["college"];
$response["user"]["created_at"] = $user["created_at"];
$response["user"]["updated_at"] = $user["updated_at"];
echo json_encode($response);
} else {
// user failed to store
$response["error"] = TRUE;
$response["error_msg"] = "Unknown error occurred in registration!";
echo json_encode($response);
}
} else {
$response["error"] = TRUE;
$response["error_msg"] = "Required parameters (name, email or password) is missing!";
echo json_encode($response);
}
?>
My table structure is
I am unable to insert the data into it. I have checked for the post meathod to be wrong but it not wrong. So I thought that there will be an error with bind_param(). Please let me know where the error is and a solution to rectify it.
Please explain me about "Call to undefined method mysqli_stmt::get_result()"
You are likely missing MySQLND. That method is only available with that driver.
http://php.net/manual/en/mysqli-stmt.get-result.php

Gives out {"error":true,"error_msg":"User already existed with abc#gmail.com"} even though user doesn't exists

I have tried out a code for user registration..problem is it gives me {"error":true,"error_msg":"User already existed with abc#abc.com"} even though the user doesn't exists in database..plzz help me out of this..pardon me if am wrong some were..!
here gose my /DB_Function.php/code
<?php
class DB_Functions {
private $db;
// constructor
function __construct() {
try {
$hostname = "localhost";
$dbname = "miisky";
$dbuser = "root";
$dbpass = "";
$this->db = new PDO("mysql:host=$hostname;dbname=$dbname", $dbuser, $dbpass);
}
catch(PDOException $e)
{
echo $e->getMessage();
}
}
/**
* Storing new user
* returns user details
*/
public function storeUser($fname, $lname, $email, $password, $mobile) {
try {
$hash = md5($password);
$sql = "INSERT INTO users(fname, lname, email, password, mobile, created_at) VALUES ('$fname', '$lname', '$email', '$hash', '$mobile', NOW())";
$dbh = $this->db->prepare($sql);
if($dbh->execute()){
// get user details
$sql = "SELECT * FROM users WHERE email = '$email' LIMIT 1";
$dbh = $this->db->prepare($sql);
$result = $dbh->execute();
$rows = $dbh->fetch();
$n = count($rows);
if($n){
return $rows;
}
}
}
catch (Exception $e) {
echo 'Error accessing database: ' . $e->getMessage();
}
return false;
}
public function isUserExisted($email) {
try{
$sql = "SELECT email FROM users WHERE email = $email LIMIT 1";
$dbh = $this->db->prepare($sql);
$result = $dbh->execute();
$rows = $dbh->fetch();
$n = count($rows);
if($n>0){
return true;
}else{
return;
}
}
catch (Exception $e) {
echo 'Error accessing database: ' . $e->getMessage();
}
}
}
?>
And here gose my /*register.php code */
<?php
require_once 'DB_Functions.php';
$db = new DB_Functions();
// json response array
$response = array("error" => false);
if (isset($_POST['fname']) && isset($_POST['lname']) && isset($_POST['email']) && isset($_POST['password']) && isset($_POST['mobile'])) {
// receiving the post params
$fname = $_POST['fname'];
$lname = $_POST['lname'];
$email = $_POST['email'];
$password = $_POST['password'];
$mobile = $_POST['mobile'];
if ($db->isUserExisted($email)) {
// user already existed
$response["error"] = true;
$response["error_msg"] = "User already existed with " . $email;
echo json_encode($response);
} else {
// create a new user
$user = $db->storeUser($fname, $lname, $email, $password, $mobile);
if ($user) {
// user stored successfully
$response["error"] = false;
$response["uid"] = $user["id"];
$response["user"]["fname"] = $user["fname"];
$response["user"]["lname"] = $user["lname"];
$response["user"]["email"] = $user["email"];
$response["user"]["created_at"] = $user["created_at"];
$response["user"]["updated_at"] = $user["updated_at"];
echo json_encode($response);
} else {
// user failed to store
$response["error"] = true;
$response["error_msg"] = "Unknown error occurred in registration!";
echo json_encode($response);
}
}
} else {
$response["error"] = true;
$response["error_msg"] = "Required parameters (fname, lname, email, password or mobile) is missing!";
echo json_encode($response);
}
?>
You should return true or false depending upon whether the user has been found in the database or not, plus there's small syntax error in your isUserExisted() function. Your isUserExisted() function should be like this:
// your code
public function isUserExisted($email) {
try{
$sql = "SELECT email FROM users WHERE email = '$email' LIMIT 1";
$dbh = $this->db->prepare($sql);
$result = $dbh->execute();
if($dbh->fetch()){
return true;
}else{
return false;
}
}catch (Exception $e) {
die('Error accessing database: ' . $e->getMessage());
}
}
// your code
You have the wrong query Syntax, Use the following:
INSERT INTO users(fname, lname, email, password, mobile, created_at)
VALUES ($fname, $lname, $email, $hash, $mobile, NOW())
When we use a php variable in double qoute then the value of that variable appear, while in a single quote exact that variable name appear. For example:
$x = "hello";
echo "The value is $x"; // The value is hello
echo 'The value is $x'; // The value is $x
Now you can see where you can correct your code.
You need to add quotes in your email and return false if email not found in your database. It is better to use bindParam and rowCount() to count number of rows return from your query
$sql = "SELECT email FROM users WHERE email = :email LIMIT :val ";
$dbh = $this->db->prepare($sql);
$dbh->bindParam(':email', $email, PDO::PARAM_STR);
$dbh->bindParam(':val', 1, PDO::PARAM_INT);
$dbh->execute();
$n = $dbh->rowCount();
if($n>0){
return TRUE;// return true here
}else{
return FALSE;// return false if not found in database
}

Categories