0

I have an backend website setup that displays all the users on my site in an organised table, I should be able to edit and delete the users from the php page. However I cannot get the delete function to work, here is the code.

Data_Display.php

<?php
    include('session.php');
?>
<?php include ("db.php"); ?>
<?php
    $sql = "SELECT * FROM username ORDER BY UserNameID DESC";

    $query = mysql_query($sql) or die(mysql_error());

    if (isset($_GET['UserNameID'])) {

        $id = mysql_real_escape_string($_GET['UserNameID']);
        $sql_delete = "DELETE FROM users WHERE id = '{$UserNameID}'";
        mysql_query($sql_delete) or die(mysql_error());

        header("location: data_display.php");
        exit();

    }

?>
<!DOCTYPE html>
<html lang="en">

<head>
    <link rel="icon" type="image/ico" href="favicon.ico">
    <title>Network TV - All Records</title>
    <meta charset="utf-8" />
    <link rel="stylesheet" type="text/css" href="style.css">
</head>

<body >
    <div class="container">
        <div class="content">
            <h1>Network TV Users and User control panel</h1>
            <br>
            <div class="toolbar">
                <a href="form_display.php">Add New Person</a>
                <a href="\1\index.php">Home</a>
            </div>
            <br>
        </div>
    </div>
    <div class="container">     
        <div class="content">
            <?php if (mysql_num_rows($query)) { ?>
                <?php while ($rows = mysql_fetch_assoc($query)) { ?>
            <div class="separator"></div>
            <h2><b>User reference:</b> <?php echo $rows['UserNameID']; ?></h2>
            <h2><b>Name:</b><?php echo $rows['name']; ?></h2>
            <h2><b>Email address:</b> <?php echo $rows['email']; ?></h2>
            <h2><b>Gender:</b> <?php echo $rows['sex']; ?></h2>
            <h2><b>Profile Picture:</b> <?php echo $rows['imagelink']; ?></h2>
            <div class="toolbar">
                <a href="form_edit_display.php?id=<?php echo urlencode($rows['UserNameID']); ?>">Edit</a>
                <a href="javascript:void(0);" onclick="confirmDelete('Are you sure you want to delete the record #<?php echo $rows['UserNameID']; ?>? This operation cannot be undone.', 'data_display.php?recordId=<?php echo urlencode($rows['UserNameID']); ?>');">Delete</a>
            </div>
            <?php } /* End Loop */ ?>
            <div class="separator"></div>
            <?php } else { ?>
            <div class="separator"></div>
            <h2>There are no records to display</h2>
            <div class="separator"></div>
            <?php } /* End Rows Checking */?>
        </div>
    </div>
    <div class="container">
        <br>
        <br>
        <br>
        <br>
        <br>
    </div>
    <script>
        function confirmDelete ( message, url ) 
        {
            var confirmation = confirm ( message );

            if ( confirmation == true ) {
                window.location = url;
            } else {
                return false;
            }
        }
    </script>
</body>

</html>

Session.php

<?php
// Establishing Connection with Server by passing server_name, user_id and password as a parameter
$connection = mysql_connect("localhost", "root", "Oliver");
// Selecting Database
$db = mysql_select_db("users", $connection);
if(!isset($_SESSION)){session_start();}
// Storing Session
$user_check=$_SESSION['login_user'];
// SQL Query To Fetch Complete Information Of User
$ses_sql=mysql_query("select username from username where username='$user_check'", $connection);
$row = mysql_fetch_assoc($ses_sql);
$login_session =$row['username'];
if(!isset($login_session)){
    mysql_close($connection); // Closing Connection
    header('Location: home.php'); // Redirecting To Home Page
}
?>

db.php

<?php
    $connection = mysql_connect('localhost', 'root', 'Oliver');
    mysql_select_db('users', $connection) or die(mysql_error());
?>

Information When I click the delete button in data_display.php, I do receive the javascript alert to confirm that I do want to delete the user from the database, but nothing actually happens.

8
  • 2
    Your query would be $sql_delete = "DELETE FROM users WHERE id = '{$id}'"; bcoz you assign your get value to $id $id = mysql_real_escape_string($_GET['UserNameID']); Commented Jul 14, 2015 at 11:30
  • Hello Oliver, I think deleting a file using url query string is a dangerous thing to do... you might want to use button for that.. Commented Jul 14, 2015 at 11:32
  • @Saty when I do that it doesn't work and I am using a button thanks Commented Jul 14, 2015 at 11:43
  • changed it to $sql_delete = "DELETE FROM users WHERE id = '{$id}'"; and still it does not work Commented Jul 14, 2015 at 11:46
  • 1
    What is your url???? Commented Jul 14, 2015 at 11:50

1 Answer 1

1
if (isset($_GET['recordId'])) {
    $id = mysql_real_escape_string($_GET['recordId']);
    $sql_delete = "DELETE FROM users WHERE id = '{$id}'";
    mysql_query($sql_delete) or die(mysql_error());

    header("location: data_display.php");
    exit();

}

You are sending recordId as parameter.

Sign up to request clarification or add additional context in comments.

2 Comments

A bit more information would be appreciated, what do I do witis code?
Notice: Undefined variable: UserNameID in C:\xampp\htdocs\1\admin\data_display.php on line 15 Unknown column 'id' in 'where clause' thanks for the help

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.