Forum Moderators: coopster

Message Too Old, No Replies

Delete From Database With Form

         

ShadyB

7:01 pm on Feb 25, 2008 (gmt 0)

10+ Year Member



Hi, Ive ran into a problem with this script ive been making.
It takes an array of email users from a database, and prints out the details. I cannot seem to get the delete button to work..

<?php
// First we establish a MySQL connection...
include 'config/db.inc.php';
// Then we fetch the table data from the database...
$result = mysql_query("select * from users");
// A few variables need to be declared...
while($r=mysql_fetch_array($result))
{
$id=$r["id"];
$email=$r["email"];
$quota=$r["quota"];

// Now we can display the table...
echo "<tr>";
echo "<td>";
echo "$id";
echo "</td>";
echo "<td>";
echo "$email";
echo "</td>";
echo "<td>";
echo "$quota kb";
echo "</td>";
echo "<td>";
echo "<form method=\"post\" action=\"includes/scripts/user_delete.php\"><input name=\"userdel\" type=\"hidden\" value=\"$id\"><input type=\"image\" src=\"images/delete.png\"></form>";
echo "</td>";
echo "</tr>";
}

I have used a form, and the script it points to is this..

<?php
include '../../config/db.inc.php';
$result = mysql_query("select * from users");
// A few variables need to be declared...
while($r=mysql_fetch_array($result))
{
$id=$r["id"];
$email=$r["email"];
$password=$r["password"];
$quota=$r["quota"];
}
if (isset($_POST['userdel'])){
$userdel = mysql_query("DELETE FROM users WHERE id='$id'");
$_SERVER['PHP_SELF'];

Any idea's how I can get it working? It seems to always delete the bottom record.

The page looks like this..
<snip>
Thanks in advance.

[edited by: ShadyB at 7:22 pm (utc) on Feb. 25, 2008]

[edited by: dreamcatcher at 8:31 pm (utc) on Feb. 25, 2008]
[edit reason] no urls as per T.O.S [webmasterworld.com].Thanks [/edit]

dreamcatcher

8:33 pm on Feb 25, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi ShadyB,

I`m afraid we don`t allow urls to personal sites.

Your problem is this line:

$userdel = mysql_query("DELETE FROM users WHERE id='$id'");

Should read:

$userdel = mysql_query("DELETE FROM users WHERE id='".$_POST['userdel']."'");

dc

ShadyB

8:42 pm on Feb 25, 2008 (gmt 0)

10+ Year Member



Sorry about posting the url, I'm new here.

And thankyou very much :)