douya1248 2014-02-06 11:00
浏览 165
已采纳

使用php从MySQL中删除一条记录

I made a database (php/mysql) with photos and descriptions. Everything works well. I can remove the entire record (4 pictures and descriptions) but do not know how to remove only one picture to remain eg 3. Thank you in advance for your help

enter image description here

delete.php

<?php

 // 2. Perform database query
    $id = $photo["id"];

    $safe_page_id = mysqli_real_escape_string($connection, $id);

    $query  = "DELETE FROM photographs ";
    $query .= "WHERE id = {$id} ";
    $query .= "LIMIT 1";

    $result = mysqli_query($connection, $query);

    if ($result && mysqli_affected_rows($connection) == 1) {
        // Success
        $_SESSION["message"] = "product removed.";
        redirect_to("list_photos_46.php"); 
        echo "Success!";
    } else {
        // Failure
        $_SESSION["message"] = "Failure.";
        redirect_to("list_photos_46.php?page={$id}");
        die("Database query failed. " . mysqli_error($connection));
    }

?>

list_photos.php - deletes all pictures and descriptions

<td><a href="delete_photo_2.php?id=<?php echo urlencode($row['id']); ?>"onclick="return confirm('are you sure? ');">{Delete}</a></td>
  • 写回答

3条回答 默认 最新

  • douwen9343 2014-02-06 11:25
    关注

    What I understand that you have 6 columns in tables

    ID
    Picture1
    Picture2
    Picture3
    Picture4
    description

    When you delete any record Picture1,Picture2,Picture3,Picture4,description all are deleted. If you want to delete any one picture you can set that column to NULL. e.g picture3

    $query  = "UPDATE photographs SET Picture3 = NULL WHERE id = {$id} ";
    

    Or when you are inserting pictures, insert four records for four pictures. In this way you can deleted them with the query in your code

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥50 树莓派安卓APK系统签名
  • ¥15 maple软件,用solve求反函数出现rootof,怎么办?
  • ¥65 汇编语言除法溢出问题
  • ¥15 Visual Studio问题
  • ¥15 state显示变量是字符串形式,但是仍然红色,无法引用,并显示类型不匹配
  • ¥20 求一个html代码,有偿
  • ¥100 关于使用MATLAB中copularnd函数的问题
  • ¥20 在虚拟机的pycharm上
  • ¥15 jupyterthemes 设置完毕后没有效果
  • ¥15 matlab图像高斯低通滤波