bilcker
bilcker

Reputation: 1130

Delete from data base with 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();});
            }   
    });
        }
    });
});

Upvotes: 2

Views: 130

Answers (2)

Cayde 6
Cayde 6

Reputation: 607

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.

Upvotes: 4

Jack jdeoel
Jack jdeoel

Reputation: 4584

PHP know inside "" is string not variable,but enclose by '' can print variable

I advice you to use '' in sql query.

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

OR

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

Upvotes: 0

Related Questions