Guest User

Untitled

a guest
Aug 14th, 2018
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.21 KB | None | 0 0
  1. Delete specific image from the database [simple php/mySql]
  2. <form method="post">
  3. <ul>
  4. <?php
  5.  
  6. $host = "127.0.0.1"; //database location
  7. $user = ""; //database username
  8. $pass = ""; //database password
  9. $db_name = ""; //database name
  10.  
  11. if(!$link = mysql_connect($host, $user, $pass)) {
  12. echo "<p>error: ".mysql_error()."</p>";
  13. } else {
  14. mysql_select_db($db_name);
  15. }
  16.  
  17. $selectAll = "SELECT image_name FROM home_images";
  18. $doIt = mysql_query($selectAll);
  19.  
  20. // if(isset($_POST['delete'])) {
  21. // mysql_query("DELETE FROM home_images WHERE image_name = ");
  22. // }
  23.  
  24. ?>
  25. <?php while($result = mysql_fetch_array($doIt)) : ?>
  26.  
  27. <li style="display:inline; margin-right:10px">
  28. <img src="<?php bloginfo('url') ?>/wp-content/uploads/<?php echo $result[0]; ?>" height=50 width=60 />
  29. <input type="hidden" value="<?php echo $result[0]; ?>" name="imagename" />
  30. <input type="submit" value="X" name="delete" />
  31. </li>
  32.  
  33. <?php endwhile; ?>
  34.  
  35. <?php
  36.  
  37. if(isset($_POST['delete'])) {
  38. $imagename = $_POST['imagename'];
  39. $deleter = "DELETE FROM home_images WHERE image_name = '$imagename'";
  40. if(mysql_query($deleter)) {
  41. echo "Successful!";
  42. echo $imagename;
  43. } else {
  44. echo mysql_error();
  45. }
  46. }
  47. ?>
  48.  
  49.  
  50.  
  51. </ul>
  52. </form>
  53.  
  54. <?php while($result = mysql_fetch_array($doIt)) : ?>
  55. <li style="display:inline; margin-right:10px">
  56. <form method="post">
  57. <img src="<?php bloginfo('url') ?>/wp-content/uploads/<?php echo $result[0]; ?>" height=50 width=60 />
  58. <input type="hidden" value="<?php echo $result[0]; ?>" name="imagename" />
  59. <input type="submit" value="X" name="delete" />
  60. </form>
  61. </li>
  62. <?php endwhile; ?>
  63.  
  64. <?php while($result = mysql_fetch_array($doIt)) : ?>
  65.  
  66. <li style="display:inline; margin-right:10px">
  67. <img src="<?php bloginfo('url') ?>/wp-content/uploads/<?php echo $result[0]; ?>" height=50 width=60 />
  68. <div <!--some kind of formatting here i suppose-->>
  69. <input type="radio" value="<?php echo $result[0]; ?>" name="imagename" />
  70. </div>
  71. </li>
  72.  
  73. <?php endwhile; ?>
  74.  
  75. <?php
  76.  
  77. if(isset($_POST['delete'])) {
  78. $imagename = $_POST['imagename'];
  79. $deleter = "DELETE FROM home_images WHERE image_name = '$imagename'";
  80. if(mysql_query($deleter)) {
  81. echo "Successful!";
  82. echo $imagename;
  83. } else {
  84. echo mysql_error();
  85. }
  86. }
  87. ?>
Add Comment
Please, Sign In to add comment