I have a server script with which i have allowed the user to mark any item as favorite, but i also want another script through which the user can unfavorite the same item if they want and that item should be deleted from their favorite list. I have kept the same table for both favorite and unfavorite code, therefore i have used update query to update the details. For this purpose i have a code, but its not working, as i am new in the programming field would appreciate some guidance

<?php
    require_once('config.php');
    $favorite = $_REQUEST['favorite'];
    $unfavorite = $_REQUEST['unfavorite']; 
    $id=$_REQUEST['id'];

$unfavoritedeal=mysql_query("SELECT * FROM favoritedeals where id='".$id."'"); //favoritedeals is the name of the table

if($row=mysql_fetch_array($unfavoritedeal))
       {
          $favorite=$row['favorite'];
          $unfavorite=$row['unfavorite'];
       }

          $myfavorite=(isset($_REQUEST['favorite'])?$_REQUEST['favorite']:$favorite);
          $myunfavorite=(isset($_REQUEST['unfavorite'])?$_REQUEST['unfavorite']:$unfavorite);

$update = mysql_query("update favoritedeals set favorite = '".$myfavorite."', unfavorite = 1 where id = '".$id."'");    

if(unfavorite="1" where id='".$id."')
  {
    "delete from favoritedeals WHERE id= '".$id."'";
  } 


    $posts[0]['message'] = 'favorite list updated';
    $selectt = mysql_query("select * from favoritedeals where id = '".$id."'");
    $results =  mysql_fetch_assoc($selectt);
    $posts[0]['detail'] = $results;
    header('Content-type: application/json');
    echo json_encode($posts);
?>

Hi, the query at line 18 is not correct because you're going to set 1 to the unfavorite column, no matter what is the value of $_REQUEST['favorite'].

Try to avoid $_REQUEST and use $_POST or $_GET, otherwise the script can receive input from different sources: cookies, post & get requests.

The IF statement at line 20 is not correct:

if(unfavorite="1" where id='".$id."')
{
    "delete from favoritedeals WHERE id= '".$id."'";
}

It seems SQL. Your intention is to delete the row?

Anyway, I would change your script a bit, you can use a single update query to set up the favorite and to remove it:

UPDATE favoritedeals SET favorite = CASE WHEN favorite = 0 THEN 1 ELSE 0 END WHERE id = $id

To do this, remove the unfavorite column, and change the favorite column to tinyint type, with default value 0: when the value is 0 the item is not between the favorites, when is 1 is a favorite. So, the example of the table is this:

create table favoritedeals(
id int unsigned not null auto_increment primary key,
favorite tinyint unsigned not null default 0
) engine = myisam;

Then change your script to this:

<?php

require_once('config.php');

# default message
$posts[0]['message'] = 'Error: specify favorite and/or a valid id.';

if(array_key_exists('id', $_REQUEST))
{
    $id     = (int)$_REQUEST['id'];
    $update = mysql_query("UPDATE favoritedeals SET favorite = CASE WHEN favorite = 0 THEN 1 ELSE 0 END WHERE id = $id");

    # overwrite the default message
    $posts[0]['message'] = 'favorite list updated';

}

$query = mysql_query("SELECT * FROM favoritedeals WHERE favorite = 1");

# suggested here: http://www.php.net/manual/en/function.mysql-fetch-assoc.php#90030
while(($posts[0]['detail'][] = mysql_fetch_assoc($query)) || array_pop($posts[0]['detail']));

header('Content-type: application/json');
echo json_encode($posts);

Now the select query will return all the favorites, if you want to return only the last one, change the query at line 18 of this example to:

$query = mysql_query("SELECT * FROM favoritedeals WHERE favorite = 1 AND id = $id");

The update query can also be used to create a new item, for example:

$update = mysql_query("INSERT INTO favoritedeals (id, favorite) values($id, 1) ON DUPLICATE KEY UPDATE favorite = CASE WHEN favorite = 0 THEN 1 ELSE 0 END");

So, if you use a link like:

http:/localhost/fave.php?id=5

And item 5 doesn't exists, it will be inserted and favorited at the same time, by refreshing the same link, instead the favorite will be removed.

By the way, since you're still learning, avoid using the MySQL API, this is going to be deprecated in PHP 5.5.0 and will be removed, read this for more information:

Hope it helps you, bye!

Thank You so much.. it has helped me alot

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.