当我试图通过数据库插入数据时,它显示“Uncaught Error:从上下文调用私有方法DbOperations :: insertData()”。
我的插入数据PHP代码:
<?php
require_once '../includes/DbOperations.php';
$response = array();
if($_SERVER['REQUEST_METHOD']=='POST'){
if (isset($_POST['airport']) and isset($_POST['location']) and isset($_POST['space']) and isset($_POST['flight'])) {
//operate the data further//
$db = new DbOperations ();
$result = $db->insertData($_POST['airport'], $_POST['location'], $_POST['space'], $_POST['flight']);
if($result == 1){
$response['error'] = false;
$response['message'] = "Task Inserting Successfully!!";
} elseif ($result == 2) {
$response['error'] = true;
$response['message'] = "Some error occured please try again!!";
} elseif ($result == 0) {
$response['error'] = true;
$response['message'] = "Data already Inserted!!";
}
} else {
$response['error'] = true;
$response['message'] = "Required fields are missing!!";
}
}else {
$response['error'] = true;
$response['message'] = "Invalid Request!!";
}
echo json_encode($response);
?>
我的DBoperation:
<?php
class DbOperations{
private $con;
function __construct(){
require_once dirname(__FILE__).'/DbConnect.php';
$db = new DbConnect();
$this->con = $db->connect();
}
/*CRUD -> C -> CREATE */
public function createUser($username, $pass, $email){
if($this->isUserExist($username,$email)){
return 0;
}else{
$password = md5($pass);
$stmt = $this->con->prepare("INSERT INTO `users` (`id`, `username`, `password`, `email`) VALUES (NULL, ?, ?, ?);");
$stmt->bind_param("sss",$username,$password,$email);
if($stmt->execute()){
return 1;
} else {
return 2;
}
}
}
public function userLogin($username, $pass){
$password = md5($pass);
$stmt = $this->con->prepare("SELECT id FROM users WHERE username = ? AND password = ?");
$stmt->bind_param("ss", $username, $password);
$stmt->execute();
$stmt->store_result();
return $stmt->num_rows > 0;
}
public function getUserByUsername ($username){
$stmt = $this->con->prepare ("SELECT * FROM users WHERE username = ?");
$stmt->bind_param("s", $username);
$stmt->execute();
return $stmt->get_result()->fetch_assoc();
}
private function isUserExist($username, $email){
$stmt = $this->con->prepare("SELECT id FROM users WHERE username = ? OR email = ?");
$stmt->bind_param("ss", $username, $email);
$stmt->execute();
$stmt->store_result();
return $stmt->num_rows > 0;
}
/*CRUD -> C -> CREATE */
private function insertData($airport, $location, $space , $flight){
if($this->isDataExist($airport, $location, $space , $flight)){
return 0;
} else {
$stmt = $this->con->prepare("INSERT INTO `users_detail` (`id`, `airport`, `location`, `space`, `flight`, `userid`) VALUES (NULL, ?, ?, ?, ?, NULL);");
$stmt->bind_param("ssss",$airport, $location, $space , $flight);
if($stmt->execute()){
return 1;
} else {
return 2;
}
}
}
}
?>
答案 0 :(得分:0)
您正在尝试在课堂外调用私有方法,这是不可能的,您可以在官方PHP methods visibility docs上阅读有关方法可见性的更多信息。
宣布 public的类成员可以随处访问。声明受保护的成员只能在类本身以及继承和父类中访问。声明为 private的成员只能由定义成员的类访问。
如果要使用类外的方法,则应将方法声明为public而不是private:
public function insertData($airport, $location, $space , $flight){
... your code
}
答案 1 :(得分:0)
您无法调用类
之外的私有方法或字段通过如下定义函数insertData() public
public function insertData() {
************
}
或使用
从构造函数中调用它function __construct($airport, $location, $space , $flight){
$db = new DbConnect();
$this->con = $db->connect();
$this->insertData($airport, $location, $space , $flight);
}
运行如下:
$db = new DbOperations($_POST['airport'], $_POST['location'], $_POST['space'], $_POST['flight']);