我有一个表,将图像名称存储在表中,并将文件存储在服务器上的文件夹中。我想要做的是能够从数据库中删除记录,同时从服务器上的文件夹中删除表记录中具有其名称的相应图像。任何帮助将不胜感激以下是代码:

if ((isset($_POST['hiddenFieldID'])) && ($_POST['hiddenFieldID'] != "")) {
  $deleteSQL = sprintf("DELETE FROM photodiary WHERE pdiaryID=%s",
                       GetSQLValueString($_POST['hiddenFieldID'], "int"));

  mysql_select_db($database_connMain, $connMain);
  $Result1 = mysql_query($deleteSQL, $connMain) or die(mysql_error());

  $deleteGoTo = "adphotodiary.php?user=" . $row_rsadmin['adID'] . "";
  if (isset($_SERVER['QUERY_STRING'])) {
    $deleteGoTo .= (strpos($deleteGoTo, '?')) ? "&" : "?";
    $deleteGoTo .= $_SERVER['QUERY_STRING'];
  }
  header(sprintf("Location: %s", $deleteGoTo));
}

最佳答案

您将不得不执行 SELECT 查询以获取文件名并检索其在服务器上的路径。然后您可以发送 DELETE 查询,如果成功,您将删除该文件。

if ((isset($_POST['hiddenFieldID'])) && ($_POST['hiddenFieldID'] != "")) {
  $getPathSql = sprintf("SELECT * FROM photodiary WHERE pdiaryID=%s",
                       GetSQLValueString($_POST['hiddenFieldID'], "int"));
  mysql_select_db($database_connMain, $connMain);
  $pathResult = mysql_query($getPathSql , $connMain) or die(mysql_error());
  $path = ...
  $deleteSQL = sprintf("DELETE FROM photodiary WHERE pdiaryID=%s",
                       GetSQLValueString($_POST['hiddenFieldID'], "int"));

  $Result1 = mysql_query($deleteSQL, $connMain) or die(mysql_error());
  if($Result1 && is_file($path)) unlink($path);

  $deleteGoTo = "adphotodiary.php?user=" . $row_rsadmin['adID'] . "";
  if (isset($_SERVER['QUERY_STRING'])) {
    $deleteGoTo .= (strpos($deleteGoTo, '?')) ? "&" : "?";
    $deleteGoTo .= $_SERVER['QUERY_STRING'];
  }
  header(sprintf("Location: %s", $deleteGoTo));
}

关于php - 从数据库中删除记录时,我需要帮助从文件夹中删除图像,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/12479415/

10-12 05:55
查看更多