我正在创建一个ajax通知,这是我的系统的一部分,允许用户收藏或存档该通知。问题是下面的 php 代码不起作用,并且查询中没有错误,因为 or die
不返回任何内容。返回的只是错误
。这就是它所回响的全部。我知道 javascript 是正确的并且发送了正确的信息,因为我已经检查了网络选项卡以查看。我是否遗漏了任何重大错误?
<?php
require_once('.conf.php');
$notid = mysql_real_escape_string($get['notification_id']);
$username = mysql_real_escape_string($_SESSION['uname']);
$action = mysql_real_escape_string($get['action']);
if ($action == 'add') {
$insert = mysql_query("UPDATE updates SET object_fav = 1 WHERE username = '$username' AND id = '$notid'") or die('Could not connect: ' . mysql_error());
echo 'success';
} elseif($action == 'sub') {
$remove = mysql_query("UPDATE updates SET object_fav = 0 WHERE username = '$username' AND id = '$notid'") or die('Could not connect: ' . mysql_error());
echo 'remove';
} else {
echo 'error';
}
?>
最佳答案
PHP 没有名为 $get
的默认数组。也许您打算使用 the $_GET
superglobal.
$action = mysql_real_escape_string($_GET['action']);
$notid = mysql_real_escape_string($_GET['notification_id']);
当 $action
在 if/else
链中不匹配时,它会打印 error
,因为变量设置不正确。
请确保您在开发时已打开 display_errors
和 error_reporting(E_ALL);
。 undefined variable $get
会在屏幕上显示警告。
ini_set('display_errors', 1);
error_reporting(E_ALL);
关于php - 简单的 PHP 无法运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9677045/