duanpo8329 2015-10-29 11:43
浏览 106
已采纳

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

On the site users have option to add other users as a friend. Now I want to make option to delete some user from the friend list. The problem is that I don't know how exactly to do it with jquery part.

This is the button for remove

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

Then this is the jquery part

$(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"
});     
});

and the 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();    

}

Where I'm wrong?

On Console in the Chrome I get this error so far

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

UPDATE: I'm very new into jquery and I used this from one tutorial..

UPDATE: this is the table if it's matter but I can't update it even from sql query in the phpmyadmin

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: Query should be like this

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

instead of this

UPDATE user_friends SET isDeleted = 1 AND isActive = 0 WHERE friend_id = ?
  • 写回答

3条回答 默认 最新

  • duanrong5927 2015-10-29 11:47
    关注

    Your ajax type is

     type: 'get',
    

    So instead of POST you have to use GET and closing issue of isset function

    if(isset($_GET['id'])) {  // here closing issue also
    
        $friend_id = $_GET['id'];
    

    Remove backtick from value of isDeleted and isActive

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

    SO you full php code would be

    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();
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 为啥画版图在Run DRC会出现Connect Error?可我Calibre的hostname和计算机的hostname已经设置成一样的了。
  • ¥20 网站后台使用极速模式非常的卡
  • ¥20 Keil uVision5创建project没反应
  • ¥15 mmseqs内存报错
  • ¥15 vika文档如何与obsidian同步
  • ¥15 华为手机相册里面的照片能够替换成自己想要的照片吗?
  • ¥15 陆空双模式无人机飞控设置
  • ¥15 sentaurus lithography
  • ¥100 求抖音ck号 或者提ck教程
  • ¥15 关于#linux#的问题:子进程1等待子进程A、B退出后退出(语言-c语言)