4 Replies - 2912 Views - Last Post: 08 October 2010 - 10:35 PM Rate Topic: -----

#1 fuzzy29  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 86
  • Joined: 10-January 10

PHP MySQL delete

Posted 08 October 2010 - 01:55 PM

Hi there,
I know this has been done before, but I can't work out how to apply the current answers to my codee.
I have a list, which is being automatically generated, to display current users.
<?php
$username="user1";
$password="password1";
$database="database1";
$table="members";
$host="host1";

mysql_connect($host,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");
$query="SELECT * FROM $table";
$result=mysql_query($query);

$num=mysql_numrows($result);

mysql_close();
?>

<table class="bordered" width="98%" align="center">
<tr width="100%">
<th width="10%" align="center"><font face="verdana">ID</font></th>
<th width="30%" align="center"><font face="verdana">Username</font></th>
<th width="30%" align="center"><font face="verdana">Password</font></th>
<th width="15%" align="center"><font face="verdana">Delete?</font></th>
<th width="15%" align="center"><font face="verdana">Active?</font></th>
</tr>

<?php
$i=0;
while ($i < $num) {

$f1=mysql_result($result,$i,"id");
$f2=mysql_result($result,$i,"username");
$f3=mysql_result($result,$i,"password");
$f4=mysql_result($result,$i,"allow");
?>

<tr width="100%">
<td width="10%" align="center"><font face="verdana"><?php echo $f1; ?></font></td>
<td width="30%"><font face="verdana"><?php echo $f2; ?></font></td>
<td width="30%"><font face="verdana"><?php echo $f3; ?></font></td>
<td width="15%"><font face="verdana"><img src="../icons/delete.png" align="center">
<?php
$query = "DELETE members where id=$f1 && username=$f2 && password=$f3";
mysql_query($query);
?>
</font></td>
<td width="15%"><font face="verdana"><?php if($f4==1){
print '<html>';
print '<img src="../icons/allow_user.png" width="32" height="32" align="center">';
print '</html>';
}
else {
print '<html>';
print '<img src="../icons/deny_user.png" width="32" height="32" align="center">';
print '</html>';
} ?></font></td>
</tr>

<?php
$i++;
}
?>



The below is the piece of code I'd like to use to delete a user.

<td width="15%"><font face="verdana"><img src="../icons/delete.png" align="center">
<?php
$query = "DELETE members where id=$f1 && username=$f2 && password=$f3";
mysql_query($query);
?>
</font></td>



I understand that the best way of doing this is to use the image as a link to a php file, using the address to instruct the php delete file, I just can't seem to do it.

I have seen the following tutorial for this, but don't understand it...
http://www.phpeasyst...om/mysql/7.html

Any help would be greatly appreciated.
Thanks in advance, Daniel.

This post has been edited by fuzzy29: 08 October 2010 - 01:56 PM


Is This A Good Question/Topic? 0
  • +

Replies To: PHP MySQL delete

#2 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6058
  • View blog
  • Posts: 23,496
  • Joined: 23-August 08

Re: PHP MySQL delete

Posted 08 October 2010 - 02:45 PM

Are you getting errors? You should be doing this to make sure your query is working, which is PHP/MySQL Debugging 101:

<?php
$query = "DELETE members where id=$f1 AND username=$f2 AND password=$f3";
if (!mysql_query($query))
{
    die("Unable to delete using query $query : " .mysql_error());
}
?>
</font></td>



With that die() removed and replaced with proper error logging when exposed to a non-development environment.

I question the need for anything more than the ID in your delete query as well. If you have the ID, the ID should be unique and therefore all that's needed to perform the deletion.

Now, as far as hooking it to the image, you're probably looking at Javascript's onclick event, which means using AJAX to send the request to the server, as Javascript is executed on the client (i.e., the browser) while PHP is executed on the web server.
Was This Post Helpful? 0
  • +
  • -

#3 fuzzy29  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 86
  • Joined: 10-January 10

Re: PHP MySQL delete

Posted 08 October 2010 - 03:15 PM

View PostJackOfAllTrades, on 08 October 2010 - 01:45 PM, said:

Are you getting errors? You should be doing this to make sure your query is working, which is PHP/MySQL Debugging 101:

<?php
$query = "DELETE members where id=$f1 AND username=$f2 AND password=$f3";
if (!mysql_query($query))
{
    die("Unable to delete using query $query : " .mysql_error());
}
?>
</font></td>



With that die() removed and replaced with proper error logging when exposed to a non-development environment.

I question the need for anything more than the ID in your delete query as well. If you have the ID, the ID should be unique and therefore all that's needed to perform the deletion.

Now, as far as hooking it to the image, you're probably looking at Javascript's onclick event, which means using AJAX to send the request to the server, as Javascript is executed on the client (i.e., the browser) while PHP is executed on the web server.


Thanks for the reply!
The problem with the code is that I don't know how to implement it. I understand that I have to make a link to another php file. It's just getting the user data into that php file so that it is useable.
I'd like to do a similar thing to the $_POST option, but using a link - changing the web address to something like:
http://www.mysite.com/user_manager/delete_user.php?ID=1


Or something similar.
Thanks again, Daniel.
Was This Post Helpful? 0
  • +
  • -

#4 fuzzy29  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 86
  • Joined: 10-January 10

Re: PHP MySQL delete

Posted 08 October 2010 - 04:33 PM

Thanks for you help!
Got it working now with a javascript button to an external php file.
If anyone wants the code, i'll post it.

Once again, many thanks, Daniel.
Was This Post Helpful? 0
  • +
  • -

#5 no2pencil  Icon User is online

  • Toubabo Koomi
  • member icon

Reputation: 5248
  • View blog
  • Posts: 27,072
  • Joined: 10-May 07

Re: PHP MySQL delete

Posted 08 October 2010 - 10:35 PM

Delete is a really permanent way of dealing with your user base. I would suggest using an active column that defaults to 1. When the user gets 'deleted', the active column is set to zero. The delete statement in a mysql/php string is a dangerous thing if you don't know what you are doing.

Also it's questionable if you want your users, though even if just their own account, wielding administrative powers over your database. The more functionality you make available on the website the greater percentage something can go wrong.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1