我正在处理某个页面[事件]并且正在做最奇怪的事情。我试图创建一个删除功能,它将字段名称(已删除)设置为1.这意味着数据不会完全丢失,get-events ajax页面会获取已删除= 0的事件。
但是,当您单击该按钮时,它会显示成功消息,但不会删除。我已经在SQLyog中测试了SQL语句,以确保我的SQL是正确的。我检查了PHP错误日志,没有错误..一切都匹配,如果我改变了我的SQL语句,它应该可以正常工作:
UPDATE events SET deleted = 1 WHERE eventid = :eventid
为:
UPDATE 1events SET deleted = 1 WHERE eventid = :eventid
然后它会出现一个错误,所以一切都在被阅读..它只是没有这样做..
这是我的班级构建:
Class Events{
public function __construct($EventId = null, $Array = null) {
if (isset($EventId)) {
$database = new Connection();
$database = $database->Connect();
$statement = $database->Prepare("SELECT * FROM events WHERE EventId = :EventId");
$statement->execute(array("EventId" => $EventId));
$Array = $statement->fetch(PDO::FETCH_ASSOC);
}
if (isset($Array)) {
foreach ($Array as $Key => $Value) {
$this->$Key = $Value;
}
}
}
这是我的删除功能:
public function DeleteEvent() {
try {
$database = new Connection();
$database = $database->Connect();
$statement = $database->Prepare("UPDATE events SET deleted = 1 WHERE EventId = :EventId");
$statement->execute(array("EventId" => $this->EventId ));
$this->Success = 1;
$this->Message = 'Event Deleted';
} catch (PDOException $e) {
$this->Success = 0;
$this->Message = "Error!: " . $e->getMessage();
}
}
我的删除ajax:
<?php
include $_SERVER['DOCUMENT_ROOT'].'/includes/galaxy-connect.php';
$a_json = array();
$a_json_row = array();
if (empty($_POST['EventId'])) {
$a_json_row["success"] = 0;
$a_json_row["message"] = 'Something Went Wrong';
} else {
$Event = new Events($_POST['EventId']);
$Event->DeleteEvent($_POST);
$a_json_row["success"] = $Event->Success;
$a_json_row["message"] = $Event->Message;
}
array_push($a_json, $a_json_row);
echo json_encode($a_json);
我的代码有什么问题吗?或者这是一个奇怪的错误?
答案 0 :(得分:1)
您正在调用构造函数
$Event = new Events($_POST['EventId']);
并调用方法
$Event->DeleteEvent($_POST);
但方法
public function DeleteEvent() {
try {
....
指的是$this->EventId
您的构造函数不会填充$this->EventId
,或者您的方法无法正确获取一个参数$_POST
(或两者都有...)
Events类的构造函数至少应该看起来像
class Events {
public function __construct($id) {
....
$this->EventId = $id;
答案 1 :(得分:0)
答案很简单!当我想要一个对象时,我将数据作为数组传递!
如果有人有类似的问题并找到它,请仔细检查您的代码将数据转换为对象,而不是数组: - )