0

Hi guys,

My project is chatting system in PHP and I need some help in delete friend from friend list I have already add friend function but remove friend does not work I don't know can any one help me quick

this is add friend code:

this code in utile page.

function addFriend($friendUsername){
  $id = $_SESSION['id'];
  $friendId = getUserId($friendUsername);
  if (friendExists($friendId))
    return;
  $query = "INSERT INTO friends (userid, friendid) VALUES ('$id', '$friendId')";
  mysql_query($query);
}

and this code in post page.

if (isset($_POST['addfriend'])){
  if ($_POST['friend']!=""){
    dbconnect();
    addFriend($_POST['friend']);
  } else {
    $_SESSION['msg'] = $FRIEND_NOTSELECTED;
  }
  header('location: index.php?page=allusers');
}

tell me if u need more information...

3
Contributors
16
Replies
17
Views
5 Years
Discussion Span
Last Post by Riogan
0

Add a delete function with this:

$query = "DELETE FROM friends WHERE userid = '$id' AND friendid = '$friendId'";
0

sorry man but I don't need just the SQL query but I need the whole delete function, as I mention before I write the delete code but it's not working but I don't know the reason.

This is the delete function but its not working:
In utile page.

function removeFriends($username){
  $userId=$_SESSION['id'];
  $query = "DELETE FROM friends where userid='$userId' and friendid='$username'";
  $result = mysql_query($query);
}

In post page.

if (isset($_POST['removeFriends'])){
  if ($_POST['friend']!=""){
    dbconnect();
    removeFriends($_POST['friend']);
  } else {
    $_SESSION['msg'] = $FRIEND_NOTSELECTED;
  }
  header('location: index.php?page=allusers');
}
0

Have you tried:

$result = mysql_query($query) or die(mysql_error());

Are you passing a friend name or a friend id, and what's it in the database ? Try your query in phpmyadmin first, see what happens. "It's not working" is a bit too vague.

Edited by pritaeas: n/a

0

This is MYSQL code "friend table":

CREATE TABLE IF NOT EXISTS `friends` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `userid` int(11) NOT NULL,
  `friendid` int(11) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=13 ;
0

Are you passing a friend name or a friend id, and what's it in the database ? Try your query in phpmyadmin first, see what happens. "It's not working" is a bit too vague.

Passing friend id, in database I show you the code before, and I try it in phpmyadmin but its not working. "If you want I can attach the whole code its not big"

Edited by Riogan: n/a

0

What was the error in phpmyadmin ?

Error: 1064 Message: %s near '%s' at line %d

0

And what was the query you put in ?

$query = "DELETE FROM friends where userid='$userId' and friendid='$username'";
@pritaeas
Hey man can u give your e-mail address, I wanna send the code to you.

Edited by Riogan: n/a

0

You need to replace the variables in phpmyadmin with actual values.

I replace it but still incorrect.

0

You need to post your new query with the error, I can't just keep guessing what's wrong.

Listen give me your E-mail to send to you the code I've finished the project but still I need only the delete function my way in explain is not good I know very sorry for troubling you.

0
function removeFriends($username){
  $userId=$_SESSION['id'];
  $friendId = getUserId($username); //you have to get the friendid as i can see you have //used the friendid in the addFriends function but not the username
  $query = "DELETE FROM friends where userid='$userId' and friendid='$friendId'";
  $result = mysql_query($query);
}

this will work

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.