简体   繁体   English

PHP更新将无法正确更新数据库

[英]PHP update won't properly update database

The following code is part of my ajax notification system and for some reason, it is working only 50%. 以下代码是我的ajax通知系统的一部分,由于某种原因,它只能工作50%。 When I call the code, it runs and then echo's either success or remove but it doesn't seem to change the database values. 当我调用代码时,它会运行,然后echo successremove但它似乎不会更改数据库值。 Any reason? 任何原因? I have tried putting my column names in quotes but that echo's an error. 我已经尝试将我的列名称放在引号中但是echo是一个错误。 Please help, thanks! 请帮忙,谢谢!

<?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';
}
?>

I know it is not the javascript, I have checked the network tab and it is sending the correct values. 我知道这不是javascript,我已经检查了网络选项卡,它正在发送正确的值。

If this is the start of the script, you have not called session_start() , and therefore $_SESSION['uname'] will contain an empty value. 如果这是脚本的开头,则您没有调用session_start() ,因此$_SESSION['uname']将包含空值。 The query succeeds because it is syntactically correct, but doesn't match any rows and therefore performs no update. 查询成功,因为它在语法上是正确的,但不匹配任何行,因此不执行任何更新。

session_start();
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']);

Echo $ insert和$ remove并找到缺少的值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM