dqkelut8423 2012-05-14 16:22
浏览 57
已采纳

使用php从数据库中的列中删除逗号分隔值的函数

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";
}


}
  • 写回答

2条回答 默认 最新

  • dongyijing2353 2012-05-14 17:45
    关注

    The right way to do this is to separate things out into two tables. One would hold all of the information in the current table, except it would not hold the comma-separated list. Instead, the other table would have two columns, one designating which list it belonged to (e.g. the ID of the row in the first table...or you could just use userid if there's never more than one list for a user), and then a column for the movie.

    That said, if it is too much work to change your current system and/or you are bound and determined to stick with an inferior and inefficient database structure, it is possible to make this function in your current setup. Basically, you need to find all references to the movieid that you are concerned with, and then manually adjust the lists corresponding to those matches, and then update all of those rows with new lists. It's a lot of extra code and wastes a lot of processing time (to the point that it could become a major bottleneck in your app as it grows), but it is possible.

    The simplest way to make the adjustment will be to do a string replacement on the existing list. So once you have the current list selected, do something like this:

    $newList = str_replace($movie_id_to_delete.",", "", $oldList);
    
    $result = mysql_query("UPDATE tbl_user_watchlist SET MovieIDs = '".$newList."' WHERE MovieIDs ='".$oldList."' AND UserID = ".$userid);
    

    Depending on your implementation, it might make sense to check to see if the $newList is empty and delete the entire row (instead of updating the MovieIDs as I do above) if it is empty.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能