So I have a lot of questions over this subject so what I will do is post the code that I am trying to work with so you can all get a better understanding of whats going on. I am not a professional php developer therefor there will be a lot of flawed or not efficient code. My first file is Conn.php this file will handle the connection to the database, I assume this file is done correctly.
<?php
class Conn {
public static $dbhost = "localhost";
public static $dbuser = "user name to database";
public static $dbpass = "password to access database";
public static $dbname = "database name";
}
?>
The next file MySQLDao.php this file will handle all my sql queries, not really sure if all this is done right.
<?php
class MySQLDao {
var $dbhost = null;
var $dbuser = null;
var $dbpass = null;
var $conn = null;
var $dbname = null;
var $result = null;
function __construct() {
$this->dbhost = Conn::$dbhost;
$this->dbuser = Conn::$dbuser;
$this->dbpass = Conn::$dbpass;
$this->dbname = Conn::$dbname;
}
public function openConnection() {
$this->conn = new mysqli($this->dbhost, $this->dbuser, $this->dbpass, $this->dbname);
if (mysqli_connect_errno())
echo new Exception("Could not establish connection with database");
}
public function getConnection() {
return $this->conn;
}
public function closeConnection() {
if ($this->conn != null)
$this->conn->close();
}
public function getUserDetails($email)
{
$returnValue = array();
$sql = "select * from users where user_email='" . $email . "'";
$result = $this->conn->query($sql);
if ($result != null && (mysqli_num_rows($result) >= 1)) {
$row = $result->fetch_array(MYSQLI_ASSOC);
if (!empty($row)) {
$returnValue = $row;
}
}
return $returnValue;
}
public function getUserDetailsWithPassword($email, $userPassword)
{
$returnValue = array();
$sql = "select id,user_email from users where user_email='" . $email . "' and user_password='" .$userPassword . "'";
$result = $this->conn->query($sql);
if ($result != null && (mysqli_num_rows($result) >= 1)) {
$row = $result->fetch_array(MYSQLI_ASSOC);
if (!empty($row)) {
$returnValue = $row;
}
}
return $returnValue;
}
public function registerUser($email, $password)
{
$sql = "insert into users set user_email=?, user_password=?";
$statement = $this->conn->prepare($sql);
if (!$statement)
throw new Exception($statement->error);
$statement->bind_param("ss", $email, $password);
$returnValue = $statement->execute();
return $returnValue;
}
}
?>
The next file that I have will handle business logic to store user registration details into a database table.
<?php
require("Conn.php");
require("MySQLDao.php");
$email = htmlentities($_POST["email"]);
$password = htmlentities($_POST["password"]);
$returnValue = array();
if(empty($email) || empty($password))
{
$returnValue["status"] = "error";
$returnValue["message"] = "Missing required field";
echo json_encode($returnValue);
return;
}
$dao = new MySQLDao();
$dao->openConnection();
$userDetails = $dao->getUserDetails($email);
if(!empty($userDetails))
{
$returnValue["status"] = "error";
$returnValue["message"] = "User already exists";
echo json_encode($returnValue);
return;
}
$secure_password = md5($password);
$result = $dao->registerUser($email,$secure_password);
if($result)
{
$returnValue["status"] = "Success";
$returnValue["message"] = "User is registered";
echo json_encode($returnValue);
return;
}
$dao->closeConnection();
?>
The final file will handle business logic to check if user with provided user name and password exist in our database
<?php
require("Conn.php");
require("MySQLDao.php");
$email = htmlentities($_POST["email"]);
$password = htmlentities($_POST["password"]);
$returnValue = array();
if(empty($email) || empty($password))
{
$returnValue["status"] = "error";
$returnValue["message"] = "Missing required field";
echo json_encode($returnValue);
return;
}
$secure_password = md5($password);
$dao = new MySQLDao();
$dao->openConnection();
$userDetails = $dao->getUserDetailsWithPassword($email,$secure_password);
if(!empty($userDetails))
{
$returnValue["status"] = "Success";
$returnValue["message"] = "User is registered";
echo json_encode($returnValue);
} else {
$returnValue["status"] = "error";
$returnValue["message"] = "User is not found";
echo json_encode($returnValue);
}
$dao->closeConnection();
?>
To start off I am sorry for my poor code views here I'm not 100% up to speed on how this forum works and all it has to offer, with that out of the way here a my questions, one I feel like I should have another field in my database, like one that will make a random integer and assign it to a person upon registering, this will be the unique field, how would I implement something like that? The next this is, this is something that I have pieced together on the internet so are you can imagine its a little off from what I need it to do, how this is setup now from what I can see is that there is a singular registration page with a massive form and all the info is submitted together, but what i have is a multiple page registration system, one page will ask for your email, then the next will ask for your name and so on traveling through many pages. I want to know what I would have to do to this code to make it keep submitting that info to the same user in the database, then when the next user comes along it will put there info into there section on the database and no one else's.
Please if I am doing something wrong please tell me instead of disliking this post. I will do my best to fix what I am doing wrong.