我的数据库中有一个表,其中包含两个字段,例如 UserID 和 MovieID。我在 php 中编写了一个用于插入和更新表的函数,其中 MovieIDs 是可能包含许多由逗号分隔的值的字段,同样需要一个从表中删除值的函数,我用于插入和更新的函数表中的值如下所示:
<?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";
}
}
?>
现在,如果已经在该行中插入了一个值,我需要编写一个函数来从表中删除值以及逗号...任何人都可以帮助我编写代码...我尝试编写以下代码但是它不起作用..请帮助我摆脱这个..
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";
}
}