mysql - JSON and PHP Error Occurred in registration- Android Studio -
when ever try register user, below error , not insert data database , table on localhost wamp server , database saved on phpmyadmin, when register user below error message occurs if user fails register. have 4 php files index.php error coming not register user on phpmyadmin database no data gets entered database.
error log
07-23 15:23:10.833 2133-2395/com.bradvisor.bradvisor e/json﹕ no database selected 07-23 15:23:10.833 2133-2395/com.bradvisor.bradvisor e/json parser﹕ error parsing data org.json.jsonexception: value no of type java.lang.string cannot converted jsonobject
php files
index.php
<?php /** php api login, register, changepassword, resetpassword requests , email notifications. **/ if (isset($_post['tag']) && $_post['tag'] != '') { // tag $tag = $_post['tag']; // include database handler require_once 'include/db_functions.php'; $db = new db_functions(); // response array $response = array("tag" => $tag, "success" => 0, "error" => 0); // check tag type if ($tag == 'login') { // request type check login $email = $_post['email']; $password = $_post['password']; // check user $user = $db->getuserbyemailandpassword($email, $password); if ($user != false) { // user found // echo json success = 1 $response["success"] = 1; $response["user"]["fname"] = $user["firstname"]; $response["user"]["lname"] = $user["lastname"]; $response["user"]["email"] = $user["email"]; $response["user"]["uname"] = $user["username"]; $response["user"]["uid"] = $user["unique_id"]; $response["user"]["created_at"] = $user["created_at"]; echo json_encode($response); } else { // user not found // echo json error = 1 $response["error"] = 1; $response["error_msg"] = "incorrect email or password!"; echo json_encode($response); } } else if ($tag == 'chgpass'){ $email = $_post['email']; $newpassword = $_post['newpas']; $hash = $db->hashssha($newpassword); $encrypted_password = $hash["encrypted"]; // encrypted password $salt = $hash["salt"]; $subject = "change password notification"; $message = "hello user,\n\nyour password sucessfully changed.\n\nregards,\nbradvisor team."; $from = "contact@bradvisor.com"; $headers = "from:" . $from; if ($db->isuserexisted($email)) { $user = $db->forgotpassword($email, $encrypted_password, $salt); if ($user) { $response["success"] = 1; mail($email,$subject,$message,$headers); echo json_encode($response); } else { $response["error"] = 1; echo json_encode($response); } // user existed - error response } else { $response["error"] = 2; $response["error_msg"] = "user not exist"; echo json_encode($response); } } else if ($tag == 'forpass'){ $forgotpassword = $_post['forgotpassword']; $randomcode = $db->random_string(); $hash = $db->hashssha($randomcode); $encrypted_password = $hash["encrypted"]; // encrypted password $salt = $hash["salt"]; $subject = "password recovery"; $message = "hello user,\n\nyour password sucessfully changed. new password $randomcode . login new password , change in user panel.\n\nregards,\nbradvisor team."; $from = "contact@bradvisor.com"; $headers = "from:" . $from; if ($db->isuserexisted($forgotpassword)) { $user = $db->forgotpassword($forgotpassword, $encrypted_password, $salt); if ($user) { $response["success"] = 1; mail($forgotpassword,$subject,$message,$headers); echo json_encode($response); } else { $response["error"] = 1; echo json_encode($response); } // user existed - error response } else { $response["error"] = 2; $response["error_msg"] = "user not exist"; echo json_encode($response); } } else if ($tag == 'register') { // request type register new user $fname = $_post['fname']; $lname = $_post['lname']; $email = $_post['email']; $uname = $_post['uname']; $password = $_post['password']; // check if user existed // store user $user = $db->storeuser($fname, $lname, $email, $uname, $password); if ($user) { // user stored $response["success"] = 1; $response["user"]["fname"] = $user["firstname"]; $response["user"]["lname"] = $user["lastname"]; $response["user"]["email"] = $user["email"]; $response["user"]["uname"] = $user["username"]; $response["user"]["uid"] = $user["unique_id"]; $response["user"]["created_at"] = $user["created_at"]; echo json_encode($response); } else { // user failed store $response["error"] = 1; $response["error_msg"] = "json error occured in registartion"; echo json_encode($response); } } else { $response["error"] = 3; $response["error_msg"] = "json error"; echo json_encode($response); } } else { echo "bradvisor login api"; } ?>
db_connect.php file
<?php class db_connect { // constructor function __construct() { } // destructor function __destruct() { // $this->close(); } // connecting database public function connect() { require_once 'include/config.php'; // connecting mysql $con = mysqli_connect(db_host, db_user, db_password); // selecting database mysqli_select_db($con, "db_database"); // return database handler return $con; } // closing database connection public function close() { mysqli_close(); } } ?>
userfunction.php file
<?php class db_functions { private $db; //put code here // constructor function __construct() { require_once 'db_connect.php'; // connecting database $db = new db_connect(); $this->db = $db->connect(); } // destructor function __destruct() { } /** * random string sent mail reset password */ public function random_string() { $character_set_array = array(); $character_set_array[] = array('count' => 7, 'characters' => 'abcdefghijklmnopqrstuvwxyz'); $character_set_array[] = array('count' => 1, 'characters' => '0123456789'); $temp_array = array(); foreach ($character_set_array $character_set) { ($i = 0; $i < $character_set['count']; $i++) { $temp_array[] = $character_set['characters'][rand(0, strlen($character_set['characters']) - 1)]; } } shuffle($temp_array); return implode('', $temp_array); } public function forgotpassword($forgotpassword, $newpassword, $salt){ $result = mysqli_query($this->db, "update `users` set `encrypted_password` = '$newpassword',`salt` = '$salt' `email` = '$forgotpassword'"); if ($result) { return true; } else { return false; } } /** * adding new user mysqli database * returns user details */ public function storeuser($fname, $lname, $email, $uname, $password) { $uuid = uniqid('', true); $hash = $this->hashssha($password); $encrypted_password = $hash["encrypted"]; // encrypted password $salt = $hash["salt"]; // salt $result = mysqli_query($this->db,"insert `users`(`uid`, `unique_id`, `firstname`, `lastname`, `username`, `email`, `encrypted_password`, `salt`, `created_at`) values('$uuid', '$fname', '$lname', '$email', '$uname', '$encrypted_password', '$salt', now())"); // check successful store if ($result) { // user details $uid = mysqli_insert_id($this->db); // last inserted id $result = mysqli_query($this->db, "select * users uid = $uid"); // return user details return mysqli_fetch_array($result); } else { return false; } } /** * verifies user email , password */ public function getuserbyemailandpassword($email, $password) { $result = mysqli_query($this->db, "select * users email = '$email'") or die(mysqli_error($this->db)); // check result $no_of_rows = mysqli_num_rows($result); if ($no_of_rows > 0) { $result = mysqli_fetch_array($result); $salt = $result['salt']; $encrypted_password = $result['encrypted_password']; $hash = $this->checkhashssha($salt, $password); // check password equality if ($encrypted_password == $hash) { // user authentication details correct return $result; } } else { // user not found return false; } } /** * check user existed or not */ public function isuserexisted($email) { $result = mysqli_query($this->db, "select email users email = '$email'"); $no_of_rows = mysqli_num_rows($result); if ($no_of_rows > 0) { // user existed return true; } else { // user not existed return false; } } /** * encrypting password * returns salt , 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 * returns hash string */ public function checkhashssha($salt, $password) { $hash = base64_encode(sha1($password . $salt, true) . $salt); return $hash; } } ?>
from question comments:
column count doesn't match value count @ row 1
the error message says it: in query, list number of columns, , list different number of values. remove uid
columns list (i assume has auto_increment
attribute).
$result = mysqli_query($this->db,"insert `users`(`unique_id`, `firstname`, `lastname`, `username`, `email`, `encrypted_password`, `salt`, `created_at`) values('$uuid', '$fname', '$lname', '$uname', '$email', '$encrypted_password', '$salt', now())");
Comments
Post a Comment