I've a table in my data-base which contains two-fields like UserID & MovieIDs. I written a function in php for inserting and updating the table where the MovieIDs is the field that may contain many values which are separated by comma's, similarly a need a function for deleting a value from the table, the function i used for inserting and updating the value in the table looks as follows:
<?php
addmovietowatchlist(2,36546436);
function addmovietowatchlist($userid, $movieid)
{
$con=mysql_connect('localhost','username','password');
if(!$con)
{
die('could nt connect 2 the server');
}
mysql_select_db("database", $con);
$useriddb = mysql_query("select MovieIDs from tbl_user_watchlist WHERE UserID = ".$userid);
$count = mysql_num_rows($useriddb);
if($count>0)
{
$mids=mysql_fetch_assoc($useriddb);
$listarr=explode(",",$mids['MovieIDs']);
array_push($listarr, $movieid);
$finallist=implode(",",$listarr);
//update
$result = mysql_query("UPDATE tbl_user_watchlist SET MovieIDs = '".$finallist."' WHERE UserID = ".$userid);
if($result)
echo "updated sucessfully";
}
else{
//insert
$result = mysql_query("INSERT INTO tbl_user_watchlist (UserID,MovieIDs) values (".$userid.",'".$movieid."')");
echo "inserted successfully";
}
}
?>
Now, i need to write a function for deleting the value from the table along with the comma if already a value is inserted in that row...can anyone help me in writing the code... I tried by writing the below code but it doesn't worked....please help me in getting out of this..
function deletemovietowatchlist($userid, $movieid)
{
$con=mysql_connect('localhost','username','password');
if(!$con)
{
die('could nt connect 2 the server');
}
mysql_select_db("database", $con);
$useriddb = mysql_query("select MovieIDs from tbl_user_watchlist WHERE UserID = ".$userid);
$count = mysql_num_rows($useriddb);
if($count>0){
$mids=mysql_fetch_assoc($useriddb);
$listarr=implode(",",$mids);
//echo $listarr;
//array_pop($listarr, $movieid);
$finallist=explode(",",$listarr);
echo $finallist;
$result = mysql_query("DELETE MovieIDs FROM tbl_user_watchlist WHERE UserID=".$userid);
if($result)
echo "deleted successfully";
}
}