Carl Max Carl Max - 2 months ago 12
MySQL Question

How to remove thumbnail from mysql database

I need a little help, I am trying to deleted a thumbnail image along with its caption, hyperlink, and embeded video from a mysql database. I have created a back end admin system to help me delete content. However, for some reason, I am getting an error message saying Undefined variable: id. I do not understand why I keep getting an error message saying the variable id is Undefined. The error message is on the line where I'm trying to delete the image from the table. This is the error message:

Notice: Undefined variable: id in C:\xampp\htdocs\display_image\removescore.php on line 53.

How do I fix this problem?

This is the admin.php code:

<?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>


This is the 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>

Answer

Why the error happens

In the removethumbnail.php script, you always begin trying to grab your params, and the corresponding variables are populated only if they're all present. So:

  • When it's first called you effectively get the 6 params from GET, then you check for confirmation.
  • Obviously it's not the case at this time so you output the confirmation form, adding hidden inputs to renew only 3 of the 6 above params.
  • Then the script is called again: since it can't grab the 6 params, none of the variables is populated, so firing the Undefined variable: id message.

Ironically, note that you should have been notified on the first encountered undefined variable, which was $image. But you didn'get an error message about it because you imprudently used the @ suppress error, as already pointed by @tadman.

How to avoid it... but not only that

Obviously, from what is explained above, you must add hidden inputs for the 3 missing params in your confirm form, and it's enough to make the script work as expected.

But there is something more to notice: in it's current structure, your script looks for confirmation, then launches the real process if confirm == 'yes' even if it couldn't grab the params.
Otherwise the error wouldn't fire, despite the lack of params.

Last point: anyway you should have noticed that your own message "Sorry, no thumbnail was specified for removal." is printed just before the PHP error notification.

Comments