gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 12:06:24 25 4
gpt4 key购买 nike

我有一个后端网站设置,它在一个有组织的表格中显示我网站上的所有用户,我应该能够从 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 警报,以确认我确实想要从数据库中删除该用户,但实际上什么也没发生。

最佳答案

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 作为参数。

关于php - 如何使用 PHP 按钮删除 MySql 数据库行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31405323/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com