所以我对这个主题有很多疑问,所以我要做的就是发布我正在尝试使用的代码,这样你们都可以更好地了解最新情况。我不是一个专业的PHP开发人员因为会有很多有缺陷或效率不高的代码。我的第一个文件是Conn.php,这个文件将处理与数据库的连接,我假设这个文件正确完成。
<?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";
}
?>
下一个文件MySQLDao.php这个文件将处理我所有的SQL查询,不确定是否所有这些都是正确的。
<?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;
}
}
?>
我的下一个文件将处理业务逻辑,以将用户注册详细信息存储到数据库表中。
<?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();
?>
最终文件将处理业务逻辑,以检查我们的数据库中是否存在提供用户名和密码的用户
<?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();
?>
首先,我很抱歉我的代码视图很糟糕,我对这个论坛的工作方式及其所提供的内容并不是百分之百的速度,这就是我的问题,一个问题,一个我觉得我的数据库中应该有另一个字段,比如一个随机整数并在注册时将其分配给一个人,这将是一个独特的字段,我将如何实现这样的东西?接下来就是,这是我在互联网上拼凑的东西所以你可以想象它有点偏离我需要做的事情,现在如何设置从我能看到的是有一个单一的注册带有大量表单的页面和所有信息一起提交,但我所拥有的是一个多页面注册系统,一个页面会询问您的电子邮件,然后下一个会询问您的姓名等等。我想知道我要对这段代码做什么,以使它继续将该信息提交给数据库中的同一用户,然后当下一个用户出现时,它会将信息放入数据库的那个部分而没有其他人& #39; S。
如果我做错了请告诉我而不是不喜欢这篇文章。我会尽我所能来解决我做错的事。