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.

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>

If you wish to use the above soultion by ehpratah you will have to use ajax since queries can not be performed by javascript itself. You can use a jquery Ajax method.

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.

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.

<a href='page.php' onClick="return confirm('Are you sure?')">

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.

Nice advise their broj1. The whole thing works now.

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

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