Jump to content

Archived

This topic is now archived and is closed to further replies.

techiefreak05

...php/button code being wierd .. ???

Recommended Posts

i have a site where u can have fruebds.. a profile,etc. and for every users. it displays some info and a REMOVE FRIEND button ....and when i click the REMOVE FRIEND BUTTON.. it removes a random friend .... sometimes it removes the right friend....... most times not .. whats wrong .. heres the code:

[code]<center>
<?php
$user = $_SESSION[username];
$result = mysql_query("SELECT count(*) FROM `friends` WHERE `username` = '$user' AND `accepted` = 'yes'");
if (!$result) {
  echo 'Could not run query: ' . mysql_error();
  exit;
}
$row = mysql_fetch_row($result);
$new = $row[0];
if($new == ""){
$new = "0";
}
if($new != "0" ){
?>
You have <b><? echo $new; ?></b> Friends
<?
}
?>
</center>
<table align="center">
<tr><td bgcolor="#3399CC">
<?php
$query = mysql_query("SELECT * FROM friends WHERE `username` = '$user' AND `accepted` = 'yes'");
while ($array = mysql_fetch_array($query)){
$to=$array['friend'];
echo "<br><center><b>" .$to. "</b></center>";
echo "<a href='sendMessage.php?to=" .$to. "'>-Message " .$to. "-</a> |<a href='getInfo.php?user=" .$to."'> -Get Info-</a> | <a href='http://zycoworld.com/" .$to. "'>-View zPage-</a><br><form action=\"\" method=\"post\">
      <table align=\"center\" border=\"0\" cellspacing=\"0\" cellpadding=\"3\">
<tr><td colspan=\"2\" align=\"right\"><input type=\"submit\" name=\"delete\" value=\"-Remove-\"></td></tr>
</table>
</form>";
}
$user = $_SESSION[username];
$query = mysql_query("SELECT * FROM friends WHERE `username` = '$user' AND `accepted` = 'yes'");
$array = mysql_fetch_array($query);
$friend=$array['friend'];

if($_POST['delete']){
mysql_query("DELETE FROM friends WHERE username = '$user' AND friend =
'$friend' LIMIT 1");
mysql_query("DELETE FROM friends WHERE username = '$friend' AND friend =
'$user' LIMIT 1");
echo "<font color=red> The User has been removed from your friends list, please refresh to view changes.</font>";
}
?>
</td></tr>
</table>[/code]

what is wrong !?!?!?!? ??? ??? ???

Share this post


Link to post
Share on other sites
The problem here is that you are setting the $friend variable from the result of your "SELECT * ..." query above.  Then you are deleting THAT friend.  You need to check the $_POST data to see which friend should be deleted.

This means that you need to submit the friend to be deleted along with the form.  For example

[code]<input type=hidden name=friend value="$to"> (this must go inside each form, inside the while loop.  It will ensure that $_POST['friend'] is set when the form is submitted)
...
if($_POST['delete']){
  $friend_to_delete = $_POST['friend']
  mysql_query("DELETE FROM friends WHERE username = '$user' AND friend = '$friend_to_delete' LIMIT 1");
  mysql_query("DELETE FROM friends WHERE username = '$friend_to_delete' AND friend = '$user' LIMIT 1");
  echo "<font color=red> The User has been removed from your friends list, please refresh to view changes.</font>";
}
[/code]

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.