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 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)