$provided_productid = striphtmlquotes($_GET["productid"]);
$provided_userid = $bbuserinfo['userid'];
$query_check_user = mysql_query("SELECT * FROM user_wishlist WHERE `user_id`='".$provided_userid."'", $DB->link);
$user_exists = mysql_num_rows($query_check_user);
if ($user_exists == 1) {
$fetch_user = mysql_fetch_array($query_check_user);
$row_id = $fetch_user['id'];
if ((substr($fetch_user['game_list'], -1)) == ",") {
$fetch_user['game_list'] = substr($fetch_user['game_list'], 0, -1);
}
if ((strrpos($fetch_user['game_list'], ",")) === false) {
$game_list_array = array($fetch_user['game_list']);
} else {
$game_list_array = explode(",", $fetch_user['game_list']);
}
$product_exists = array_search($provided_productid, $game_list_array);
if ((!$product_exists) or ($product_exists === false) or ($product_exists == "")) {
$new_array = $fetch_user['game_list'] . "," . $provided_productid;
$query_update_wishlist = mysql_query("UPDATE `user_wishlist` SET `game_list`='".$new_array."' WHERE `id`='".$row_id."'", $DB->link);
echo "Wishlist Added";
} else {
array_splice($game_list_array, $product_exists, 1);
if ((strrpos($fetch_user['game_list'], ",")) === false) {
$game_list_string = $game_list_array;
} else {
$game_list_string = implode(",", $game_list_array);
}
$query_update_wishlist = mysql_query("UPDATE `user_wishlist` SET `game_list`='".$game_list_string."' WHERE `id`='".$row_id."'", $DB->link);
echo "Wishlist Deleted";
}
} elseif ($user_exists == 0) {
$new_array = $provided_productid;
$query_add_update_wishlist = mysql_query("INSERT into `user_wishlist` (user_id,game_list) values ('".$provided_userid."','".$new_array."')", $DB->link);
echo "Wishlist added. Wishlist Updated";
} else {
die("error!");
}
It works, but the only problem is the first product ID that it recieves, (the first entry of the array), it can never delete it. But any other product id added later to the array can be deleted with no problem.
I am really stressed out here, I tried everything that I possibly could, and I am so stressed that I dont even feel like explaining what I tried to do 🙁
So if any of you are kind enough to look through the code and see what exacly is wrong, I'd really appreciate it.
Thanks.