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...

Add a delete function with this:

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

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');
}

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.

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 ;

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"

What was the error in phpmyadmin ?

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

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.

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

I replace it but still incorrect.

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

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.

Member Avatar for ampere
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

Thank you man its work well.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.