使用jquery删除/更新数据库中的行

时间:2015-10-29 11:43:22

标签: php jquery mysql

在网站上,用户可以选择将其他用户添加为朋友。现在我想选择从好友列表中删除一些用户。问题是我不知道如何使用jquery部分来完成它。

这是删除按钮

<a href="#" class="delete" id="'.$row['id'].'"><i class="fa fa-times pull-right"></i></a>

然后这是jquery部分

$(document).ready(function() {

$('.delete').click(function() {
    var parent = $(this).closest('media-heading');
    $.ajax({
        type: 'get',
        url: 'misc/friendRemove.php', 
        data: 'ajax=1&delete=' + $(this).attr('id'),
        beforeSend: function() {
            parent.animate({'backgroundColor':'#fb6c6c'},300);
        },
        success: function() {
            parent.fadeOut(300,function() {
                parent.remove();
            });
        }
    });         
});

$('.delete').confirm({
    text: "Are you sure you want to delete?",
    title: "Confirmation required",

    confirmButton: "Yes",
    cancelButton: "No",
    post: true,
    confirmButtonClass: "btn-danger",
    cancelButtonClass: "btn-default",
    dialogClass: "modal-dialog modal-lg"
});     
});

friendRemove.php

    if(isset($_POST['id']) {  

    $friend_id = $_POST['id'];

            $value = $pdo->prepare('UPDATE user_friends SET isDeleted = `1` and isActive = `0` WHERE friend_id= ?');
            $value->bindParam(1, $friend_id, PDO::PARAM_INT);               
            $value->execute();
            $result = $value->fetch();    

}

我哪里错了?

Chrome中的控制台我到目前为止收到此错误

friendRemove.php?ajax=1&delete=37 500 (Internal Server Error)

更新:我是jquery的新手,我在一个教程中使用了这个...

更新:这是表,如果它的问题,但我甚至无法从phpmyadmin中的sql查询更新它

CREATE TABLE IF NOT EXISTS `user_friends` (
  `friend_id` int(11) NOT NULL,
  `user_id` int(11) NOT NULL,
  `isActive` tinyint(1) NOT NULL DEFAULT '0',
  `isDeleted` tinyint(1) NOT NULL DEFAULT '0',
  `friendsSince` datetime NOT NULL,
   PRIMARY KEY (`friend_id`,`user_id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;

更新:查询应该是这样的

UPDATE user_friends SET isDeleted = 1, isActive = 0 WHERE friend_id = ?

而不是

UPDATE user_friends SET isDeleted = 1 AND isActive = 0 WHERE friend_id = ?

3 个答案:

答案 0 :(得分:2)

你的ajax类型是

 type: 'get',

因此,您必须使用GET并关闭POST函数

的问题,而不是isset
if(isset($_GET['id'])) {  // here closing issue also

    $friend_id = $_GET['id'];

backtick

的值中移除isDeleted and isActive
 $value = $pdo->prepare('UPDATE user_friends SET isDeleted = 1 and isActive = 0 WHERE friend_id= ?');

所以你完整的PHP代码将是

if (isset($_GET['delete'])) {

    $friend_id = $_GET['delete'];
    $value = $pdo->prepare('UPDATE user_friends SET isDeleted = 1 and isActive = 0 WHERE friend_id= ?');
    $value->bindParam(1, $friend_id, PDO::PARAM_INT);
    $value->execute();
}

答案 1 :(得分:1)

除了发出GET请求之外,如果您在网址请求中查找id变量,则必须明确设置...现在您正在寻找{{1}但是看起来你应该从如何通过AJAX请求设置URL参数中寻找$_POST['id']

答案 2 :(得分:1)

从代码中删除'。

$value = $pdo->prepare('UPDATE user_friends SET isDeleted = `1` and isActive = `0` WHERE friend_id= ?'

修改后的行:

$value = $pdo->prepare('UPDATE user_friends SET isDeleted = 1 and isActive = 0 WHERE friend_id= ?'