0

Hello PHP Friends,

I think this is just if else statement problem but im kinda lose to it.
The whole code is succesful in deleting the data from database the problem is i wanted to put a script that remind the user if he/she really sure to delete the data. By the way this is the script.

<?php

    // connect to the database
        include 'Connect.php';

    // confirm that the 'student_id' variable has been set
        if (isset($_GET['student_id']) && is_numeric($_GET['student_id']))
        {
    // get the 'student_id' variable from the URL
        $student_id = $_GET['student_id'];

    // delete record from database
        if ($stmt = $mysql->prepare("DELETE FROM student_information WHERE student_id = ? LIMIT 1")) 
        {
            $stmt->bind_param("i",$student_id); 
            $stmt->execute();
            $stmt->close();
        }
        else
        {
            echo "ERROR: could not prepare SQL statement.";
        }
        $mysql->close();

    // redirect user after delete is successful
        header("Location: Admin_Home.php");
    }
    else
    // if the 'student_id' variable isn't set, redirect the user
        {
            header("Location: Admin_Home.php");
        }
?>

Please help me modify these codes and where to put the missing line/s of codes.

4
Contributors
8
Replies
52
Views
3 Years
Discussion Span
Last Post by broj1
1

Hi one simple solution is use windows.confirm javascript just put your delete query inside the js like

<script>
    if (window.confirm('are you sure you want to delete this data?')) {

    .. your delete query    
    }else{
    do anything
    }
</script>
0

hello broj1,

I have read you link site but Im just beginner in PHP and i dont have enough knowledge about Ajax. If it would be fine for you please help me how to start the said script.

0

by the way i have created this script in ajax search while the link to delete is present,,,here are the lines of codes where the DELETE Link is shown.

        $searchResults .= "  <td><a href='Student_View.php?id={$student_id}'>View</a> </td>\n";
        $searchResults .= "  <td><a href='Admin_Edit_Student_Info.php?id={$student_id}'>Update</a></td>\n";
        $searchResults .= "  <td><a href='Admin_Delete_Student.php?id={$student_id}'>Delete</a></td>\n";
        $searchResults .= "</tr>\n";

The Delete on the third line is the link to delete the data but i wanted to insert something here some sort of an ALERT or WARN before deleting the student's data.

Please advise.

0

After looking closer at your code I figured out that you do not need neither javascript nor ajax. Before posting my version of code I have to warn you that deleting records using GET method is a bad practice considering the semantics and security. Make sure only authenticated users can get to this script. Here is the code (take it as a concept):

// connect to the database
include 'Connect.php';

// confirm that the 'student_id' variable has been set
if (isset($_GET['student_id']) && is_numeric($_GET['student_id']))
{
    // if user clicked on a Delete buttton, then delete the record and redirect
    if(isset($_POST['confirm-delete'])) {

        // first remove the delete element form the $_POST
        unset($_POST['confirm-delete']);

        // get the 'student_id' variable from the URL
        $student_id = intval($_GET['student_id']);

        // delete record from database
        if ($stmt = $mysql->prepare("DELETE FROM student_information WHERE student_id = ? LIMIT 1"))
        {
            $stmt->bind_param("i",$student_id);
            $stmt->execute();
            $stmt->close();
            $mysql->close();

            header("Location: Admin_Home.php");
            exit();           
        }
        else
        {
            $mysql->close();
            // if error redirect to the error page
            header("Location: Error.php");
            exit();
        }
    }

    // prompt and a form containing the confirm button
    echo "<p>Do you realy want to delete the record?</p>";
    echo '<form action="#" method="post">';
    echo '<input type="submit" name="confirm-delete" value="Delete">';
    echo '</form>';
}
else
    // if the 'student_id' variable isn't set, redirect the user
{
    header("Location: Admin_Home.php");
}

As you see I used a form with a button and a POST method, so I can first check whether a button was clicked. You should add a cancel button as well and redirect without deleting if pressed.

0

Nice advise their broj1. The whole thing works now.

Thank you so much for your time and expertise.
This thread is considered solved.

0

Good to hear it works as you expected. Please mark this as solved so it is officially out of the way :-)

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.