Got a problem that has completely stumped me... am hoping the combined wisdom of the forum could assist...

I have a php script that enables a user to follow and unfollow items on a site...

Am doing this with a table that captures basically the user's id and the item id...

The below code is what does all the dirty work...

Writing a new record to the db when a user elects to follow an item works fine... However for some reason the delete request does not want to work when a user elects to "unfollow" an item...

Would appreciate any suggestions/tips... A couple of notes, does not appear to be any issues with db connection or privilleges (have set it to full access for the user). And in any case the fact that I can write a new record for new "follows" demonstrates that db connectivity is not the issue...

THE CODE --->
  //for a user to follow a thread
    if(!empty($_POST['folVal'])){
        $fv = $_POST['folVal'];
        if($fv==1){
            //user has selected the item to be followed, so need to update UserFavourites table
            $q = "INSERT INTO tUserFavourites (ThreadID, Username ) VALUES ('{$_POST['ThreadID']}','{$_POST['Username']}')";
            $r = mysql_query($q);
            if(mysql_affected_rows() == 1) {
                //echo $fv;
                echo "true";
            } else {
            echo "false";
            }
            $r.mysql_close();
        } else 
            {
            //user has selected the item to be UNfollowed, so need to update UserFavourites table
            $q = "DELETE FROM tUserFavourites WHERE ThreadID='{$_POST['ThreadID']}' AND Username='{$_POST['Username']}'";
            $TID=$_POST['ThreadID'];
            $UID=$_POST['Username'];
            $q = "DELETE FROM tUserFavourites WHERE ThreadID='$TID' AND Username='$UID'";
           mysql_query($q);
            if(mysql_affected_rows() >0) {
                echo "true";
            } else {
            echo "false";
            }
            $r.mysql_close();
        }
    }

In your code you have $q declared in line 19 and 22, the first query statement will be overwrited by the second and only this last will be performed by mysql_query.

At line 23 add or die(mysql_error()); to see if there is an error.

Also change $r.mysql_close(); with mysql_close(); or add as first argument the link identifier, not the query variable: http://php.net/manual/en/function.mysql-close.php

try by writing $q = "DELETE FROM tUserFavourites WHERE ThreadID='".$TID."' AND Username='".$UID."';";

Thanks all...

found the prob..

for some reason php is not recognising the else test for $fv... with your updated SQL manishanibhwani... thanks

just got more explcit - ie $fv must equal a specific value in the else and bingo it worked

Member Avatar for iamthwee

Mark as solved please.

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.