使用数据库

时间:2016-11-07 09:17:29

标签: php sql

我想通过名为' DELETE'的按钮删除图像。 我的图像保存在服务器上,我的图像链接保存在数据库表中。

要立即删除效果很好,但每个用户都可以通过在网址中输入图片ID来立即删除每个用户的图片。

uploads.php

<div id="myuploads">
<?php
//Configuration
$host = 'localhost';
$user = 'root';
$pass = '';
$db = 'myimg';

$salt = "u6d5u6mj65dehjum568nuu65umk57endjzu766imm57e8u5u56";

if(isset($_SESSION['username'])){
    $hash = hash('sha224', $_SESSION['username']).$salt;
}

$conn = mysqli_connect("$host", "$user", "$pass", "$db");

//Script
if(isset($_SESSION['username'])){
    $uid = $_SESSION['username'];
    $dir = $uid . "/";

    $alledateien = mysqli_query($conn, "SELECT * FROM imglinks WHERE uid='$uid'");

    foreach ($alledateien as $datei)
    {

        echo "<div class='pictures'> \
              <img class='pbild' src='" . $dir . $datei["link"] . "'><br/> \ 
              <form action='functions/deleteimg.php'></div> \
              <input class='deleteimg' type='submit' name='deleteimg' value='DELETE'> \
              <input class='post' type='submit' value='PUBLISH'></form>";
    }
}else{
    header("Location: ../index.php");
}
?>
</div>

deleteimg.php

<?php
include '../config.php';

$uid = $_SESSION['username'];

$username = mysqli_query($conn, "SELECT uid FROM imglinks WHERE uid='$uid'");

foreach($username AS $name) {

if(isset($_GET['deleteimg']) && $uid = $name['uid']){
    $sql = "SELECT * FROM imglinks WHERE id='".$_GET['deleteimg']."' LIMIT 1";

    $filepath = mysqli_query($conn, $sql);

    foreach($filepath AS $value) {
        unlink($value['link']);
    }

    $uid = $_SESSION['username'];
    mysqli_query($conn, "DELETE FROM imglinks WHERE id='".$_GET['deleteimg']."' AND uid='$uid'");
}}

从我的数据库的imglinks表中发出的链接。

DATABASE STRUCTURE

如果您需要更多细节,请随时提出。

5 个答案:

答案 0 :(得分:0)

你可以:

  1. 在名为“show”的 imglinks 表中添加一个布尔字段,并相应地修改您的查询。在这种情况下,您甚至可以“取消删除”意外删除的图像。
  2. 如果您的磁盘空间不足,请将<input class='deleteimg' type='submit' name='deleteimg' value='DELETE'>修改为<input class='deleteimg' type='submit' name='deleteimg' value='".$image_to_delete."'>,以便您在postdata中看到此链接并能够处理它

答案 1 :(得分:0)

您需要像这样更改按钮:

foreach ($alledateien as $datei)
{

    echo "<div class='pictures'> \
          <img class='pbild' src='" . $dir . $datei["link"] . "'><br/> \ 
          </div><form action='functions/deleteimg.php'> \
          <input type='hidden' name='img_id' value='".$datei["id"]."'> \
          <input class='deleteimg' type='submit' name='deleteimg' value='DELETE'> \
          <input class='post' type='submit' name='publish' value='PUBLISH'></form>";
}

然后在deleteimg.php上,获取图片ID并执行删除查询:

if(isset($_POST['deleteimg']) && $_POST['deleteimg'] == 'DELETE'){
    $img_id = $_POST['img_id'];
    mysqli_query($conn, "DELETE FROM imglinks WHERE id='$img_id'");
}

答案 2 :(得分:0)

它不是那么复杂,只需使用这样:

* Root是您的根目录!

$path = root/$uid/$imgname;

$query("DELETE FROM imagetable WHERE imgname = $imgname");

删除图片:

unlink($path);

只需使用表单向您的脚本提交imgname!

*为了更加安全,您还可以在查询中添加$ uid!

答案 3 :(得分:0)

我在删除时会建议/做的一件事我会使用锚标记而不是按钮删除然后在锚标记上我会有动作和id作为查询字符串。然后在deleteimg.php页面上,如果操作是删除,我将查询操作和ID,然后删除所选的id。

然后您的代码将如下所示:

<强> uploads.php

<div id="myuploads">
<?php
//Configuration
$host = 'localhost';
$user = 'root';
$pass = '';
$db = 'myimg';

$salt = "u6d5u6mj65dehjum568nuu65umk57endjzu766imm57e8u5u56";

if(isset($_SESSION['username'])){
    $hash = hash('sha224', $_SESSION['username']).$salt;
}

$conn = mysqli_connect("$host", "$user", "$pass", "$db");

//Script
if(isset($_SESSION['username'])){
    $uid = $_SESSION['username'];
    $dir = $uid . "/";

    $alledateien = mysqli_query($conn, "SELECT * FROM imglinks WHERE uid='$uid'");

    foreach ($alledateien as $datei)
    {

        echo "<div class='pictures'> \
              <img class='pbild' src='" . $dir . $datei["link"] . "'><br/> \ 
              <form action='functions/deleteimg.php'></div> \
              <a class='deleteimg' href='functions/deleteimg.php?action=delete&id='".$datei['id']."'>DELETE</a><br>
              <input class='post' type='submit' value='PUBLISH'></form>";
    }
}else{
    header("Location: ../index.php");
}
?>
</div>

然后 deleteimg.php

<?php

    // Your connection here...

    // check if session is set here...



      $action = $_GET['action'];
      $img_id = intval($_GET['id']);


      if($action === "delete"){

      $sql = "DELETE FROM imglinks WHERE id='$img_id'";

    if (mysqli_query($conn, $sql)) {
        echo "Record deleted successfully";
//image delete... do something else

    } else {
        echo "Error deleting record: " . mysqli_error($conn);
    }

      }

    mysqli_close($conn);

    ?>

这对我来说总是很有用,而且总是很容易,希望你会发现它很有用。

注意:阅读有关mysqli预处理语句,针对sql注入的内容..您可以阅读here

答案 4 :(得分:0)

感谢所有答案,现在我已经自己完成了。

<强> deleteimg.php

<?php
include '../config.php';

$uid = $_SESSION['username'];

if(isset($_GET['deleteimg'])){
    $sql = "SELECT * FROM imglinks WHERE id='".$_GET['deleteimg']."' LIMIT 1";
    $filepath = mysqli_query($conn, $sql);

    $test = mysqli_fetch_array($filepath);

    if($test['uid'] == $uid){
        unlink($test['link']);
        mysqli_query($conn, "DELETE FROM imglinks WHERE id='".$_GET['deleteimg']."' AND uid='$uid'");

        echo 'File successfully deleted.';
        header ("Refresh: 2; ../index.php");

    } else {
        echo 'You do not have the permission to delete this file.';
        header ("Refresh: 2; ../index.php");
    }


}