0

I have this code where I get all the rows out of my mySQL database:

<?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 }; ?>

I would like to delete the image where my delete button is on. I have no idea how I should start or how I can achieve this. Any help is welcome!enter image description here

Mae
  • 443
  • 2
  • 5
  • 22
  • 3
    Please, [don't use `mysql_*` functions](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php), They are no longer maintained and are [officially deprecated](https://wiki.php.net/rfc/mysql_deprecation). Learn about [prepared statements](http://en.wikipedia.org/wiki/Prepared_statement) instead, and use [PDO](http://us1.php.net/pdo) or [MySQLi](http://us1.php.net/mysqli). [This article](http://php.net/manual/en/mysqlinfo.api.choosing.php) will help you decide. – Jay Blanchard Oct 21 '14 at 16:00
  • 2
    You want something like a HTML button linking to `deleteRow.php?rowID=#`. – user3973427 Oct 21 '14 at 16:01
  • @JayBlanchard I got the error code from PHP indeed. I just started with PHP lessons at school and we are using this code to learn the basics. Thanks for pointing out though, I will learn the other one for sure after I'm done with this project! ^^ – Mae Oct 21 '14 at 16:03
  • @user3973427 and in the deleteRow.php I make a mySQL query with GET? – Mae Oct 21 '14 at 16:11
  • You should use a form instead of a link and use POST to make your delete request. Never use GET for something that modifies your database. – jeroen Oct 21 '14 at 16:15

1 Answers1

0

Like others have stated, check out PDO or mysqli for your interface between your database. To your question. You need to issue a MySQL DELETE command. In order for this to work, your mysql user needs to have delete privileges on the table you are deleting the row from. Using your example above, the sql would look like this:

<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. 
  }
}
?>

In order for this to work, you're going to need to add a primary key to your photos table.

Tanner
  • 221
  • 2
  • 8
  • Everything you suggested has already been done. The problem is that I don't know how I should create this function. I want to click on the delete button and the image where the button is on to be deleted. :( – Mae Oct 21 '14 at 16:19
  • you where faster than me! Your previous answer made me think how I could use the get function. Thank you very much! :) You made my day! – Mae Oct 21 '14 at 16:28