如何使用PHP按钮删除MySql数据库行

时间:2015-07-14 11:28:39

标签: php mysql

我有一个后端网站设置,在一个有组织的表格中显示我网站上的所有用户,我应该能够从php页面编辑和删除用户。但是我无法使用删除功能,这是代码。

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

信息 当我单击data_display.php中的删除按钮时,我确实收到了javascript警告,以确认我确实要从数据库中删除该用户,但实际上没有任何操作。

1 个答案:

答案 0 :(得分: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();

}

您正在发送recordId作为参数。