php - Cannot store the value in MySQL database -
i creating android code snippet takes values user input, store them in mysql , finish registration. here's part i'm having trouble with.
registeruser method in android source code
private void registeruser(final string email, final string name, final string password, final string dob) { // tag used cancel request string tag_string_req = "req_register"; pdialog.setmessage("registering"); showdialog(); stringrequest strreq = new stringrequest(request.method.post, appconfig.url_register, new response.listener<string>() { @override public void onresponse(string response) { log.d(tag, "register response: " + response.tostring()); hidedialog(); try { jsonobject jobj = new jsonobject(response); boolean error = jobj.getboolean("error"); if(!error) { // user stored in mysql // store user in sqlite string useruid = jobj.getstring("uid"); jsonobject user = jobj.getjsonobject("user"); string username = user.getstring("name"); string useremail = user.getstring("email"); string userdob = user.getstring("dob"); string usercreated_at = user.getstring("created_at"); // inserting row in users table db.adduser(username, useremail, userdob, useruid, usercreated_at); // lauch login activity intent intent = new intent(registeractivity.this, mainscreenactivity.class); startactivity(intent); finish(); } else { // error occurred in registration. error message. string errormsg = jobj.getstring("error_msg"); toast.maketext(getapplicationcontext(), errormsg, toast.length_long).show(); } } catch(jsonexception e) { e.printstacktrace(); } } }, new response.errorlistener() { @override public void onerrorresponse(volleyerror error) { log.e(tag, "registration error: " + error.getmessage()); toast.maketext(getapplicationcontext(), error.getmessage(), toast.length_long).show(); hidedialog(); } }) { @override protected map<string, string> getparams() { // posting params register url map<string, string> params = new hashmap<string, string>(); params.put("tag", "register"); params.put("name", name); params.put("email", email); params.put("password", password); params.put("dob", dob); return params; } }; // adding request to request queue appcontroller.getinstance().addtorequestqueue(strreq, tag_string_req); } and here php files deals registration process.
index.php
<?php /** * file handle api requests * accepts , post * * each request identified tag * response json data /** * check post request */ if (isset($_post['tag']) && $_post['tag'] != '') { // tag $tag = $_post['tag']; // include db handler require_once 'include/db_functions.php'; $db = new db_functions(); // response array $response = array("tag" => $tag, "error" => false); // 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 $response["error"] = false; $response["uid"] = $user["unique_id"]; $response["user"]["name"] = $user["name"]; $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 not found // echo json error = 1 $response["error"] = true; $response["error_msg"] = "incorrect email or password!"; echo json_encode($response); } } else if ($tag == 'register') { // request type register new user $name = $_post['name']; $email = $_post['email']; $password = $_post['password']; $dob = $_post['dob']; // check if user existed if ($db->isuserexisted($email)) { // user existed - error response $response["error"] = true; $response["error_msg"] = "user existed"; echo json_encode($response); } else { // store user $user = $db->storeuser($name, $email, $password, $dob); if ($user) { // user stored $response["error"] = false; $response["uid"] = $user["unique_id"]; $response["user"]["name"] = $user["name"]; $response["user"]["email"] = $user["email"]; $response["user"]["dob"] = $user["dob"]; $response["user"]["created_at"] = $user["created_at"]; $response["user"]["updated_at"] = $user["updated_at"]; echo json_encode($response); } else { // user failed store $response["error"] = true; $response["error_msg"] = "error occured in registartion"; echo json_encode($response); } } } else { // user failed store $response["error"] = true; $response["error_msg"] = "unknow 'tag' value. should either 'login' or 'register'"; echo json_encode($response); } } else { $response["error"] = true; $response["error_msg"] = "required parameter 'tag' missing!"; echo json_encode($response); } ?> db_functions.php
<?php class db_functions { private $db; //put code here // constructor function __construct() { require_once 'db_connect.php'; // connecting database $this->db = new db_connect(); $this->db->connect(); } // destructor function __destruct() { } /** * storing new user * returns user details */ public function storeuser($name, $email, $password, $dob) { $uuid = uniqid('', true); $hash = $this->hashssha($password); $encrypted_password = $hash["encrypted"]; // encrypted password $salt = $hash["salt"]; // salt $result = mysql_query("insert users(unique_id, name, email, encrypted_password, dob, salt, created_at) values('$uuid', '$name', '$email', '$encrypted_password', '$dob', '$salt', now())"); // check successful store if ($result) { // user details $uid = mysql_insert_id(); // last inserted id $result = mysql_query("select * users uid = $uid"); // return user details return mysql_fetch_array($result); } else { return false; } } /** * user email , password */ public function getuserbyemailandpassword($email, $password) { $result = mysql_query("select * users email = '$email'") or die(mysql_error()); // check result $no_of_rows = mysql_num_rows($result); if ($no_of_rows > 0) { $result = mysql_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 = mysql_query("select email users email = '$email'"); $no_of_rows = mysql_num_rows($result); if ($no_of_rows > 0) { // user existed return true; } else { // user not existed return false; } } /** * encrypting password * @param 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 * @param salt, password * returns hash string */ public function checkhashssha($salt, $password) { $hash = base64_encode(sha1($password . $salt, true) . $salt); return $hash; } } ?> after running programme, found few problems.
although managed register, cannot go mainscreenactivity class put in code. think it's because there problems in registration process.
i checked out mysql database table, , dob column not working fine. despite storing other user information (name, email , password), dob column stores null.
Comments
Post a Comment