【发布时间】:2016-07-02 17:07:39
【问题描述】:
//config.php 文件
<?php
define('hostname', 'localhost');
define('username', 'root');
define('password', '');
define('db_name', 'tutorial2');
?>
//connection.php文件
<?php
require_once 'config.php';
class DB_Connection {
private $connect;
function _construct(){
$this->connect = mysqli_connect(hostname, username, password, db_name) or die(" DB connection error");
}
public function get_connection() {
return $this->connect;
}
}
?>
//user_control.php 文件。我想在我的 phpMyadmin-mysql 数据库中插入“电子邮件”和密码地址。
<?php
require_once 'connection.php';
header('Content-Type: application/json ');
class User {
private $db;
private $connection;
function __construct(){
$this->db = new DB_Connection();
$this->connection = $this->db->get_connection();
}
public function does_user_exist($email,$password)
{
$query = "Select * from users where email = '$email' and password = '$password'";
$result = mysqli_query($this->connection, $query);
if (mysqli_num_rows($result) > 0){
$json[success] = ' Welcome '.$email;
echo json_encode($json);
mysqli_close($this->connection);
}else {
$query = " Insert into users(email,password) values ('$email','$password')";
$is_inserted = mysqli_query($this->connection, $query);
if ($is_inserted == 1){
$json[success] = ' Account created, welcome '.$email;
}else {
$json[error] = ' Wrong password ';
}
echo json_encode($json);
mysqli_close($this->connection);
}
}
}
$user = new User();
if (isset($_POST['email'],$_POST['password'])){
$email = $_POST['email'];
$password = $_POST['password'];
if (!empty($email) && !empty($password)){
$encrypted_password = md5($password);
$user -> does_user_exist($email,$encrypted_password);
}else {
echo json_encode("You must fill both fields");
}
}
?>
【问题讨论】: