doushou9028 2016-02-19 21:17
浏览 87
已采纳

使用PHP,Ajax从数据库中删除

Hi I am trying to run a delete script that will delete the record from my database using ajax and php. Used without the ajax javascript file the delete_file.php script works fine and is removed from the database.(So I am asssuming the problem lies in the javascript file somewhere.) Once I add the javascript file only it appears to run and delete_file.php does notk. Basically I pass three id's through to delete_file.php find them all in their respective tables then use those variables to find the right file to delete. Any Help is greatly appreciated. I need a fresh pair of eyes, thanks

What I am Clicking [![enter image description here][1]][1]

html

<?php echo'<li class="col student_file">
    <a href="delete_file/delete_file.php?student='.$student_id.'&agent='.$agency_id.'&file='.$file_id.'" class="delete-file"><i class="fa fa-times-circle-o"></i></a>
    <a class="the-file student_'.$student_id.'" id="file_'.$file_id.'" href="/application/student/file_management'.$file_path.'" target="_blank"><i class="fa fa-file-archive-o"></i></i></i>'.$file_name.' <span>'.$file_size.'</span></a>
</li>

delete_file_ajax.js

$(document).ready(function() {
"use strict";
$(".delete-file").click(function() {
    var container = $(this).parent();
    var id = $('.the-file').attr("id");
    var string = 'id='+ id ;
    if(confirm("Are you sure you want to delete this?"))    {
    $.ajax({
        type: "POST",
        url: "delete_file/delete_file.php",
        data: string,
        cache: false,
        success: function(){
            container.slideUp('slow', function() {$(this).remove();});
    }

        });
    }
    return false;
    });
});

delete_file.php

<?php
session_start();

//Connect to Database
require_once('../../../../db_config.php');
$db_connect = connectDB($mysqli) or die(mysqli_error());
//First lets make sure the user is allowed 
require_once('../../../../auth/admin_session.php');

//Create our session on session_id
$session_id = $_SESSION['ADMIN_ID'];

//Get the IDs
$s_id = $_GET['student'];
$a_id = $_GET['agent'];
$f_id = $_GET['file'];


//Lets find the Id of the Agency User
$session_query = mysqli_query($db_connect, "SELECT * FROM agency WHERE agency_id = $a_id");
$session_row = mysqli_fetch_assoc($session_query);
$session_num_rows = mysqli_num_rows($session_query);

$agency_id = $session_row['agency_id'];

//Lets find the Id of the Agency User
$student_session_query = mysqli_query($db_connect, "SELECT * FROM students WHERE student_id = $s_id");
$student_session_row = mysqli_fetch_assoc($student_session_query);
$student_session_num_rows = mysqli_num_rows($student_session_query);

$student_id = $student_session_row['student_id'];

//Lets find the Id of the File we want to delete
$file_session_query = mysqli_query($db_connect, "SELECT * FROM uploaded_files WHERE file_id = $f_id AND agency_id = $a_id AND student_id = $s_id");
$file_session_row = mysqli_fetch_assoc($file_session_query);
$file_session_num_rows = mysqli_num_rows($file_session_query);

$file_id = $file_session_row['file_id'];

if(!mysqli_connect_errno()){
    $stmt = $db_connect->prepare("DELETE FROM uploaded_files WHERE file_id = ? AND agency_id = ? AND student_id = ?") or die('We Could not locate the file you wish to delete');
    $stmt->bind_param('iii', $file_id, $agency_id, $student_id);
    $stmt->execute();
    $stmt->close();
}
?>

Solution

html

echo '<form class="delete-student-file" action="delete_file/delete_file.php" method="post">';
    echo '<li class="col student_file">';
        echo '<input type="hidden" name="student-id" value="'.$student_id.'">';
        echo '<input type="hidden" name="agency-id" value="'.$agency_id.'">';
        echo '<input type="hidden" name="file-id" value="'.$file_id.'">';
        echo'<a class="the-file student_'.$student_id.'" id="file_'.$file_id.'" href="/application/student/file_management'.$file_path.'" target="_blank"><i class="fa fa-file-pdf-o"></i>'.$file_name.' <span>'.$file_size.'</span></a>';                          
        echo '<button class="delete-file" name="submit"><i class="fa fa-times-circle-o"></i></button>';
        echo'</li>';
echo'</form>';

delete_file.php

//Get the IDs
$s_id = $_POST['student-id'];
$a_id = $_POST['agency-id'];
$f_id = $_POST['file-id'];

delete_file_ajax.js

$(document).ready(function() {
    "use strict";
    $(".delete-file").click(function(e) {
        e.preventDefault();
        var container = $(this).parent();
        var formData = $('.delete-student-file').serialize();
        if(confirm("Are you sure you want to delete this?"))    {
        $.ajax({
            type: "POST",
            url: "delete_file/delete_file.php",
            data: formData,
            cache: false,
            beforeSend: function(){
                container.animate({'backgroundColor': '#fb6c6c'}, 300);
            },
            success: function(){
                container.slideUp('slow', function() {$(this).remove();});
            }   
    });
        }
    });
});
  • 写回答

2条回答 默认 最新

  • dongyou2305 2016-02-19 21:28
    关注

    It looks like your problem is sending the ajax call as POST and requesting it as GET. Try this:

     $.ajax({
        type: "GET",
        url: "delete_file/delete_file.php",
        data: string,
        cache: false,
        success: function(){
            container.slideUp('slow', function() {$(this).remove();});
    }
    

    Personally, I would suggest changing your PHP to accept POST rather than GET, but that is just my opinion.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效