gpt4 book ai didi

php - 如何从mysql数据库中删除缩略图

转载 作者:行者123 更新时间:2023-11-28 23:25:05 25 4
gpt4 key购买 nike

我需要一点帮助,我正在尝试从 mysql 数据库中删除缩略图及其标题、超链接和嵌入视频。我创建了一个后端管理系统来帮助我删除内容。但是,出于某种原因,我收到一条错误消息,指出 undefined variable :id。我不明白为什么我不断收到一条错误消息,指出变量 id 未定义。错误消息出现在我试图从表中删除图像的那一行。这是错误信息:

注意:C:\xampp\htdocs\display_image\removescore.php 第53行 undefined variable :id。

我该如何解决这个问题?

这是 admin.php 代码:

<?php
require_once('authorize.php');
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Guitar Wars - High Scores Administration</title>
<link rel="stylesheet" type="text/css" href="style.css" />
</head>
<body>
<h2>Remove Thumbnail Administration</h2>
<p>Below is a list of all thumbnails. Use this page to remove thumbnails as needed.</p>
<hr />

<?php
require_once('appvars.php');
require_once('connectvars.php');

// Connect to the database
$conn = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);

// Retrieve the score data from MySQL
$query = "SELECT * FROM table1 ORDER BY name DESC, caption ASC";
$data = mysqli_query($conn, $query);

/// Loop through the array of score data, formatting it as HTML
echo '<table>';
echo '<tr><th>Name</th><th>Date</th><th>Score</th><th>Action</th></tr>';
while ($row = mysqli_fetch_array($data)) {
// Display the thumbnails data
echo '<tr class="scorerow"><td><strong>' . $row['name'] . '</strong></td>';
echo '<td>' . $row['caption'] . '</td>';
//echo '<td>' . $row['score'] . '</td>';
echo '<td><a href="removethumbnail.php?id=' . $row['id'] . '&amp;image=' . $row['image1'] . '&amp;name=' . $row['name'] .
'&amp;Link=' . $row['imagelink'] . '&amp;caption=' . $row['caption'] .
'&amp;video=' . $row['video'] . '">Remove</a>';

echo '</td></tr>';
}
echo '</table>';

mysqli_close($conn);
?>

</body>
</html>

这是 removethumbnail.php:

<?php
require_once('authorize.php');
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Remove Thumbnail</title>
<link rel="stylesheet" type="text/css" href="style.css" />
</head>
<body>
<h2>Thumbnail removed</h2>

<?php
require_once('appvars.php');
require_once('connectvars.php');


if (isset($_GET['id']) && isset($_GET['name']) && isset($_GET['caption']) && isset($_GET['Link']) && isset($_GET['video']) && isset($_GET['image'])) {
// Grab the data from the GET
$id = $_GET['id'];
$name = $_GET['name'];
$caption = $_GET['caption'];
$image = $_GET['image'];
$video = $_GET['video'];
$Link = $_GET['Link'];

}
else if (isset($_POST['id']) && isset($_POST['name']) && isset($_POST['caption']) && isset($_POST['Link']) && isset($_POST['video']) && isset($_POST['image'])) {
// Grab the data from the POST
$id = $_POST['id'];
$name = $_POST['name'];
$caption = $_POST['caption'];
$image = $_POST['image'];
$video = $_POST['video'];
$Link = $_POST['Link'];
}
else {
echo '<p class="error">Sorry, no thumbnail was specified for removal.</p>';
}

if (isset($_POST['submit'])) {
if ($_POST['confirm'] == 'Yes') {
// Delete the image file from the server
@unlink(TN_UPLOADPATH . $image);

// Connect to the database
$dbc = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);


// Delete the thumbnail from the database. This line 53 where the error is happening
$query = "DELETE FROM table1 WHERE id = $id LIMIT 1";
mysqli_query($dbc, $query);
mysqli_close($dbc);

// Confirm success with the user
echo '<p>Thumbnail removed</p>';

}
else {
echo '<p class="error">The THUMBNAIL was not removed.</p>';
}
}
else if (isset($id) && isset($name) && isset($caption) && isset($video)) {
echo '<p>Are you sure you want to delete the following Thumbnails?</p>';
echo '<p><strong>Name: </strong>' . $name . '<br /><strong>Date: </strong>';
echo '<br /><strong>Caption: </strong>' . $caption . '</p>';
echo '<form method="post" action="removethumbnail.php">';
echo '<input type="radio" name="confirm" value="Yes" /> Yes ';
echo '<input type="radio" name="confirm" value="No" checked="checked" /> No <br />';
echo '<input type="submit" value="Submit" name="submit" />';
echo '<input type="hidden" name="id" value="' . $id . '" />';
echo '<input type="hidden" name="name" value="' . $name . '" />';
echo '<input type="hidden" name="caption" value="' . $caption . '" />';
echo '</form>';
}

echo '<p><a href="admin.php">&lt;&lt; Back to admin page</a></p>';

?>

</body>
</html>

最佳答案

为什么会出现错误

removethumbnail.php脚本,您总是开始尝试获取您的参数,并且只有当它们全部存在时才会填充相应的变量。所以:

  • 第一次调用时,您有效地从 GET 中获取了 6 个参数,然后检查确认。
  • 显然此时情况并非如此,因此您输出确认表单,添加隐藏输入以更新仅上述 6 个参数中的 3 个
  • 然后再次调用脚本:因为它无法获取 6 个参数,没有填充任何变量,所以触发 Undefined variable: id消息。

具有讽刺意味的是,请注意,您应该在第一次遇到 undefined variable 时收到通知,即 $image .但是您没有收到有关它的错误消息,因为您轻率地使用了 @正如@tadman 已经指出的那样,抑制错误。

如何避免...但不仅如此

显然,根据上面的解释,您必须为确认表单中缺少的 3 个参数添加隐藏输入,这足以使脚本按预期工作。

但还有一些需要注意的地方:在当前结构中,您的脚本会寻找确认,然后在出现confirm == 'yes' 时启动真正的流程。 即使它不能抓取参数
否则,尽管缺少参数,错误也不会触发。

最后一点:无论如何,您应该注意到您自己的消息“抱歉,没有指定要删除的缩略图。”在 PHP 错误通知之前打印。

关于php - 如何从mysql数据库中删除缩略图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39711862/

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