PHP MySQL错误,无法在XAMPP上注册参数并将其发布到本地服务器
我将学习本教程: 我得到: {“error”:true,“error_msg”:“缺少必需的参数(名称、电子邮件或密码)!” 在POSTMAN和POST上运行register.php时,它在RAW上给出了相同的错误 使用POST方法在POSTMAN上放置:时,我遇到的唯一错误是: {“error”:true,“error_msg”:“缺少必需的参数(名称、电子邮件或密码)!” 并在给定param值时:。 我得到一个致命错误:关于方法prepare()的布尔值 下面是php脚本的所有文件 Config.phpPHP MySQL错误,无法在XAMPP上注册参数并将其发布到本地服务器,php,mysql,xampp,Php,Mysql,Xampp,我将学习本教程: 我得到: {“error”:true,“error_msg”:“缺少必需的参数(名称、电子邮件或密码)!” 在POSTMAN和POST上运行register.php时,它在RAW上给出了相同的错误 使用POST方法在POSTMAN上放置:时,我遇到的唯一错误是: {“error”:true,“error_msg”:“缺少必需的参数(名称、电子邮件或密码)!” 并在给定param值时:。 我得到一个致命错误:关于方法prepare()的布尔值 下面是php脚本的所有文件 Conf
<?php
$db_host = "localhost";
$db_username = "root";
$db_name = "android_api";
@mysql_connect("$db_host","$db_username") or die("Could not connect to MySQL");
@mysql_select_db("$db_name") or die("Could not find database");
?>
DB_Connect.php
<?php
class DB_Connect {
private $conn;
// Connecting to database
public function connect() {
require_once 'include/Config.php';
// Connecting to mysql database
//$this->conn = new mysqli($db_host,$db_username,$db_name);
$this->conn = (@mysql_connect("$db_host","$db_username") or die("Could not connect to MySQL"))&&(@mysql_select_db("$db_name") or die("Could not find database"));
// return database handler
return $this->conn;
}
}
?>
DB_Functions.php
<?php
class DB_Functions {
private $conn;
// constructor
function __construct() {
require_once 'include/DB_Connect.php';
// connecting to database
$db = new Db_Connect();
$this->conn = $db->connect();
}
// destructor
function __destruct() {
}
/**
* Storing new user
* returns user details
*/
public function storeUser($name, $email, $password) {
$uuid = uniqid('', true);
$hash = $this->hashSSHA($password);
$encrypted_password = $hash["encrypted"]; // encrypted password
$salt = $hash["salt"]; // salt
$stmt = $this->conn->prepare("INSERT INTO users(unique_id, name, email, encrypted_password, salt, created_at) VALUES(?, ?, ?, ?, ?, NOW())");
$stmt->bind_param("sssss", $uuid, $name, $email, $encrypted_password, $salt);
$result = $stmt->execute();
$stmt->close();
// check for successful store
if ($result) {
$stmt = $this->conn->prepare("SELECT * FROM users WHERE email = ?");
$stmt->bind_param("s", $email);
$stmt->execute();
$user = $stmt->get_result()->fetch_assoc();
$stmt->close();
return $user;
} else {
return false;
}
}
/**
* Get user by email and password
*/
public function getUserByEmailAndPassword($email, $password) {
$stmt = $this->conn->prepare("SELECT * FROM users WHERE email = ?");
$stmt->bind_param("s", $email);
if ($stmt->execute()) {
$user = $stmt->get_result()->fetch_assoc();
$stmt->close();
// verifying user password
$salt = $user['salt'];
$encrypted_password = $user['encrypted_password'];
$hash = $this->checkhashSSHA($salt, $password);
// check for password equality
if ($encrypted_password == $hash) {
// user authentication details are correct
return $user;
}
} else {
return NULL;
}
}
/**
* Check user is existed or not
*/
public function isUserExisted($email) {
$stmt = $this->conn->prepare("SELECT email from users WHERE email = ?");
$stmt->bind_param("s", $email);
$stmt->execute();
$stmt->store_result();
if ($stmt->num_rows > 0) {
// user existed
$stmt->close();
return true;
} else {
// user not existed
$stmt->close();
return false;
}
}
/**
* Encrypting password
* @param password
* returns salt and 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;
}
}
?>
Register.php
<?php
require_once 'include/DB_Functions.php';
$db = new DB_Functions();
// json response array
$response = array("error" => FALSE);
if (isset($_POST['name']) && isset($_POST['email']) && isset($_POST['password'])) {
// receiving the post params
$name = $_POST['name'];
$email = $_POST['email'];
$password = $_POST['password'];
// check if user is already existed with the same email
if ($db->isUserExisted($email)) {
// user already existed
$response["error"] = TRUE;
$response["error_msg"] = "User already existed with " . $email;
echo json_encode($response);
} else {
// create a new user
$user = $db->storeUser($name, $email, $password);
if ($user) {
// user stored successfully
$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 failed to store
$response["error"] = TRUE;
$response["error_msg"] = "Unknown error occurred in registration!";
echo json_encode($response);
}
}
} else {
$response["error"] = TRUE;
$response["error_msg"] = "Required parameters (name, email or password) is missing!";
echo json_encode($response);
}
?>
即使传递了所有参数,我仍然会得到以下错误:
{“error”:true,“error\u msg”:“缺少必需的参数(名称、电子邮件或密码)!}。在register.php中尝试
var\u dump($\u POST)代码>输出是什么?数组(1){[--WebKitFormBoundaryZ92bfuT2eROzniDs内容配置:_form-data;_name“]=>string(284)”“name”Arnav-----WebKitFormBoundaryZ92bfuT2eROzniDs内容配置:表单数据;name=“email”Arnav。kaushal800@gmail.com----WebKitFormBoundaryZ92bfuT2eROzniDs内容处置:表单数据;名称=”password“password”--WebKitFormBoundaryZ92bfuT2eROzniDs--“}{”error:“error_msg:“缺少所需的参数(名称、电子邮件或密码)!}因此它获取参数、名称、电子邮件和密码,但再次获得相同的错误警告:使用错误抑制YOLO运算符(@
)隐藏代码中的问题,使调试问题变得更加复杂。这是最后手段,只能在特殊情况下使用。您应该为用户显示一条错误消息,记录一个问题,启动某种重试,或者同时执行所有这些操作。警告:不要使用在PHP7中删除的过时界面。替换类和指南类有助于解释最佳实践。这里没有参数,这在代码中有严重的错误。转义任何和所有用户数据,特别是从$\u POST
或$\u GET
转义。