开发者

delete a page from my favorite list. (php)

My friend created a website with the capability to add pages to the user favourite list. but recently I got many complains about deleting one page from the favourite list... it looks like the code has an error and sometimes add pages instead of deleting pages.

more explanation : each page has a certain id so when adding something to the favourite it adds the id number and then it puts comma to separate other numbers. same thing with deleting a page from the favorite.. it searches for the number and deletes it ... but sometimes it does not ! I do not know why ... it tells me "the page was successfully deleted" but I donot see it deleted and sometimes it returns some old pages that I deleted before ! can you please check the code ..

here is the code

if($_REQUEST['do'] == 'delfav')
{
    $mn_Id = safe($_REQUEST['mn_Id'],true);
    $page  = 'favorite';
    if(isset($mn_Id))
    {
        $fav = explode(',' , $_SESSION['user']['Favorites']);
        if(in_array($mn_Id,$fav))
        {
            echo $ind = array_search($mn_Id , $fav);
            unset($fav[$ind]);
            $new = implode(',' , $fav);
            $ss = $db->query("UPDATE users SET Favorites = '".$new."' WHERE Id = '".$_SESSION['user']['Id']."' ");
            if($ss)
            {
                $msg = 'the page was successfully deleted';
            }
            else
            {
                $msg = 'error occurred please try again later';
            }
        }
        else
        {
            $msg = 'This page is not in your favorite list';
        }
    }开发者_C百科
}


If you can't change the code related to database, you can test with the follow code instead:

(...)

if(isset($mn_Id))
{
    $fav = explode(',' , $_SESSION['user']['Favorites']);
    if(in_array($mn_Id,$fav))
    {
        $fav = array_diff($fav, array($mn_Id));
        $new = implode(',' , $fav);
        $ss = $db->query("UPDATE users SET Favorites = '".$new."' WHERE Id = '".$_SESSION['user']['Id']."' ");

(...)

Where the change was replacing this:

echo $ind = array_search($mn_Id , $fav);
unset($fav[$ind]);

for this:

$fav = array_diff($fav, array($mn_Id));


From my previous experience - and as phresenel said - avoid using CSV inside ur tables. I don't know about the length of your lists but I've managed to break PHP strings several times. If you wish to keep your CSV (It is your funeral site), check that unset truly unsets the desired variable (echo the list after and before). But if you wish to improve it, I suggest using a separate table for the favourites; a table that has only two columns, one for the used id and the other for the favourite web site (A four entries list will translate to four records - each with the same user id). To delete just search for the user id and web site, if found delete that row. To add, just add a new row. Note that this a very simplified approach that may violate several DB best practices.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜