删除通过mysql_query获取的特定行结果?

时间:2014-10-21 15:59:14

标签: php mysql

我有这段代码,我从mySQL数据库中获取所有行:

<?php
$result = mysql_query("SELECT * FROM `photos` WHERE `photo_category` = 1 AND `photo_active` = 1");
while($row = mysql_fetch_array($result)) {;
?>
    <img src="<?php echo $row['thumb_path'] ?>"><br>
    Creatie datum: <?php echo $row['photo_date'] ?><br>
    <a href="">Delete picture</a>
<?php }; ?>

我想删除删除按钮所在的图像。我不知道我应该如何开始或如何实现这一目标。欢迎任何帮助!enter image description here

1 个答案:

答案 0 :(得分:0)

与其他人一样,请查看PDOmysqli,了解数据库之间的界面。对你的问题。您需要发出MySQL DELETE命令。为了使其正常工作,您的mysql用户需要对要删除行的表具有删除权限。使用上面的示例,sql将如下所示:

<html>
  <img src="<?php echo $row['thumb_path'] ?>"><br>
  Creatie datum: <?php echo $row['photo_date']; ?><br>

  <form name="photo_<?php echo $row['photo_id']; ?>_delete" action="doDelete.php" method="post">
    <input type="hidden" name="photo_id" value="<?php echo $row['photo_id']; ?>">
    <input class="delete_button" type="submit" name="submit" value="delete">
 </form>

</html>


<?php //doDelete.php

if ($_POST['submit'] == 'delete') {
  $photo_id = $_POST['photo_id']; 
  $sql = "DELETE FROM photos WHERE photo_id = " . $photo_id . " LIMIT 1";
  if (mysql_query($sql)) {
    //photo deleted.
  } else {
    //handle errors. 
  }
}
?>

为了实现这一点,您需要在照片表中添加primary key