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

时间:2016-09-26 20:31:15

标签: php mysql mysqli

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

注意:未定义的变量:第53行的C:\ xampp \ htdocs \ display_image \ removescore.php中的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>

1 个答案:

答案 0 :(得分:1)

错误发生的原因

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

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

具有讽刺意味的是,请注意您应该收到第一个遇到的未定义变量的通知,即$image。但是你并没有得到关于它的错误消息,因为你非常谨慎地使用了@抑制错误,正如@tadman已经指出的那样。

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

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

但还有一些需要注意的事项:在其当前结构中,您的脚本会查找确认,然后启动真实流程confirm == 'yes' 即使它无法抓住PARAMS 即可。
否则,即使缺少参数,错误也不会发生。

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