dongpao1905 2015-10-08 17:27
浏览 7

UPDATE表与AJAX PHP没有初始化

im calling my AJAX functioin like this:

for (i = 0; i < remove.length; i++) {

    remove[i].addEventListener("click", function () {
        removeName = $(this).attr("id");

        $.ajax({
            type: "POST",
            url: "ajax.php",
            data: {deleteName: removeName}
        });
    });

}

In my ajax.php i have a function like this:

  if (filter_input(INPUT_POST, 'deleteName') != "") {
    $messageManager->deleteMessages(filter_input(INPUT_POST, 'deleteName'));
}

My deleteMessages() function looks like this:

public function deleteMessages($deleteName) {
    $sql = 'UPDATE ' . self::TABLE . ' SET rdelete=? WHERE receiver = ? AND sender=? OR sdelete=? WHERE receiver=? AND sender=?';
    $stmt = $this->dbh->prepare($sql);
    $stmt->execute(array(1, $_SESSION['user']->getName(), $deleteName, 1, $deleteName, $_SESSION['user']->getName()));
}

I have tried to use file_put_contents() in the deleteMessages() function to check if it gets accessed, but there is no file being created.

The variable that AJAX is sending, is indeed filled with the desired string. But some how in the ajax.php the POST is not being recognized... but only for "deleteName", everything else works fine.

Is my syntax wrong? im lost!

Actually i want to set rdelete=1 where receiver=me and sender=friend and set sdelete=1 where receiver=friend and sender=me

  • 写回答

1条回答 默认 最新

  • doujiao1981 2015-10-08 17:35
    关注

    Your SQL is wrong. You have TWO where clauses, which is illegal.

    WHERE receiver = ? AND sender=? OR sdelete=? WHERE receiver=? AND sender=?
    ^^^^--- clause #1                            ^^^^^---clause #2
    

    It should be something more like:

    WHERE ((receiver = ?) AND (sender = ?)) OR (sdelete =1) OR  ((receiver =? AND (sender = ?))
    

    Note the extra () sets. You've got and and or clauses in there, which means you'll have to use () to enforce the logic evaluation order. The above is my best guess as to what you're trying to accomplish, but probably isn't what you really want.

    And if you had even bare bones error handling on your DB calls, you'd have been informed about the syntax errors. By default, MySQLi and PDO simply default to silent failure, and returning boolean false. Never EVER assume success. Always assume failure, check for failure, and treat success as a pleasant surprise.

    评论

报告相同问题?

悬赏问题

  • ¥15 远程桌面文档内容复制粘贴,格式会变化
  • ¥15 关于#java#的问题:找一份能快速看完mooc视频的代码
  • ¥15 这种微信登录授权 谁可以做啊
  • ¥15 请问我该如何添加自己的数据去运行蚁群算法代码
  • ¥20 用HslCommunication 连接欧姆龙 plc有时会连接失败。报异常为“未知错误”
  • ¥15 网络设备配置与管理这个该怎么弄
  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题