0

I have a table that becomes populated with data from a MySQL database, and each row receives its own delete button. I would like to have the option to delete each row separately with a delete button that deletes the corresponding row in the database. How would I go about doing so? Here's the part of the code that I have to far, which does not seem to work whatsoever.

if(isset($_POST['id'])) {
$id = $_POST['id'];
$delete = mysql_query($conn,"DELETE FROM mods WHERE id = '$id'");
}

Unimportant code omitted.

Table/Form Creation:

   echo "<td><form action=\"\" method=\"post\"></td>";
   echo "<tr class='modlist'>";
   echo "<td>".$row['id']."</td>";
   echo "<td><div class=\"edit\" id=\"div_1\">".$row['title']."</div></td>";
   echo "<td><div class=\"edit\" id=\"div_2\"><a href=".$row['mod_url'].">".$row['mod_url']."</a></div></td>";
   echo "<td><div class=\"edit\" id=\"div_3\">".$row['developer']."</div></td>";
   echo "<td><div class=\"edit\" id=\"div_4\">".$row['type']."</div></td>";
   echo "<td><div class=\"edit\" id=\"div_5\">".$v162."$nbsp".$v164."$nbsp".$v172."</div></td>";
   echo "<td><div class=\"edit\" id=\"div_6\">".$row['title'].",$nbsp".$row['developer']."</div></td>";
   echo "<td><div id=\"save\"><input type=\"submit\" name=\"save\" value=\"Save\"></div></td>";
   echo "<td><div id=\"delete\"><input type=\"submit\" name=\"delete\" value=\"Delete\"></div></td>";
   echo "</tr>";
   echo "</form>";
   }
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
swiftsly
  • 811
  • 4
  • 16
  • 29

3 Answers3

3

You have the parameters for mysql_query() backwards. First pass the query, then pass the $conn.

$delete = mysql_query("DELETE FROM mods WHERE id = '$id'", $conn);

But beware you now have an SQL injection problem! Sanitize $id before you use it!

$id = mysql_real_escape_string($id);

And consider using mysqli or PDO (and use parameterized queries), as mysql_* are deprecated!

nl-x
  • 11,762
  • 7
  • 33
  • 61
  • 1
    +1 for suggesting use of mysqli or PDO. You're better off learning (and using) this sooner rather than later. – ryryan Dec 16 '13 at 22:44
  • 1
    @swiftsly Also look at the answer of Jerry. That is also important! – nl-x Dec 16 '13 at 22:46
  • @nl-x Alright, I fixed the query, added the sanitization for `$id`, and change things over to mysqli, but it still doesn't delete the row. Any ideas? – swiftsly Dec 17 '13 at 22:15
2

Form action needs to have a control with the name 'id', otherwise $_POST will not get any id.

You can do it like <input type="hidden" name="id" value=".$row['id']." />

Also, I suggest you have a look at How can I prevent SQL injection in PHP? as your form, by the code you displayed, looks completely vulnerable.

UPDATE

You also have to fix your $delete query like nl-x mentioned:

$delete = mysql_query("DELETE FROM mods WHERE id = '$id'", $conn);
Community
  • 1
  • 1
zurfyx
  • 31,043
  • 20
  • 111
  • 145
  • I added the line to the form, as well as everything said above, but still no luck deleting the row, any ideas? – swiftsly Dec 17 '13 at 22:15
  • I also used/added his changes but still no luck. Is the form action empty with the code you provided or should I have something in it? I currently have `action='".$_SERVER['PHP_SELF']."'` which seems to do the same thing as a blank action in this case. – swiftsly Dec 17 '13 at 23:29
0

This WORKED I created the button with PHP code in my table

table.php

<?php 
include "connect.php";
$rezult = mysqli_query($con,"SELECT id,p1,p2,p3 FROM table");
if($rezult === FALSE) {die(mysql_error());}
$count=mysqli_num_rows($rezult);
while($row=mysqli_fetch_assoc($rezult)){

echo "<tr><td>".$row['p1']."</td>";
echo "<td>".$row['p2']."</td>";
echo "<td>".$row['p3']."</td>";
echo "<td><form action='delete.php' method='post'><input type='hidden' 
value='".$row['id']."'><input type='submit' name='delete' value='DELETE'>
</form></td></tr>";
?>

delete.php

<?php

include "connect.php";
$str_del="DELETE FROM table WHERE (id=".$_POST['id'].")";
$result = mysqli_query($con,$str_del) or die (mysql_error());
?>

connect.php

 <?php
$dbhost = ""; // ip
$dbname = ""; // DBname
$dbuser = ""; // login
$dbpass = ""; // pass
$con=mysqli_connect($dbhost, $dbuser, $dbpass, $dbname);
mysqli_query($con, 'CREATE TEMPORARY TABLE `table');
mysqli_select_db($con,$dbname);
mysqli_query($con,"set names utf8");
?>
Nandhini
  • 645
  • 7
  • 21
Vladimir
  • 11
  • 1