簡體   English   中英

使用PHP,Ajax從數據庫中刪除

[英]Delete from data base with PHP, Ajax

嗨,我正在嘗試運行一個刪除腳本,該腳本將使用ajax和php從我的數據庫中刪除記錄。 在不使用ajax javascript文件的情況下使用delete_file.php腳本可以正常工作,並將其從數據庫中刪除。(因此,我認為問題出在javascript文件的某個地方。)一旦我添加了javascript文件,它似乎就可以運行了,並且delete_file.php不。 基本上,我將三個id傳遞給delete_file.php,在它們各自的表中找到它們,然后使用這些變量找到要刪除的正確文件。 任何幫助是極大的贊賞。 我需要一雙新鮮的眼睛,謝謝

我要點擊的內容 [![在此處輸入圖片描述] [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();
}
?>

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

看來您的問題是將ajax調用作為POST發送並將其作為GET請求。 嘗試這個:

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

就個人而言,我建議將您的PHP更改為接受POST而不是GET,但這只是我的意見。

PHP知道“”內的字符串不是變量,但是用“括起來”可以打印變量

我建議您在SQL查詢中使用“”。

$session_query = mysqli_query($db_connect, "SELECT * FROM agency WHERE agency_id = '$a_id'");

要么

$session_query = mysqli_query($db_connect, "SELECT * FROM agency WHERE agency_id = '".$a_id."'");

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM