// 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文件。我想插入&#34;电子邮件&#34;和密码地址到我的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");
}
}
?>
之后
答案 0 :(得分:0)
原因如下:
if (!empty($email) && !empty($password)){
$encrypted_password = md5($password);
$user -> does_user_exist($email,$encrypted_password);
删除空格:
$user->does_user_exist($email,$encrypted_password);
确保在所有文件上开始<?php
标记之前没有可用的空格。
另外,请确保$json
数组密钥在' (single quotes)
范围内,如下所示:
$json['success'] = ' Welcome '.$email;
$json['error'] = ' Wrong password ';
答案 1 :(得分:0)
它通过更新邮递员为我工作,我有一个很老的版本,就像他们对我说的那样。