I have records like "1,3,6,17" in a column and use the following to delete e.g. "6" from them:
$sql = "SELECT SeId, SeCatId FROM series WHERE FIND_IN_SET(:catid,SeCatId)";
$stmt = $dbh->prepare($sql);
$stmt -> bindParam('catid',$id);
$stmt->execute();
$result = $stmt->FetchAll();
foreach($result as $row){
$seid = $row['SeId'];
$ids = explode(",",$row['SeCatId']);
$i = 0;
foreach($ids as $catid) {
if($catid===$id){unset($ids[$i]);}
$i++;
}
$ids = implode(",",$ids);
$sql = "UPDATE series SET SeCatId = :catid WHERE SeId = :id";
$stmt = $dbh->prepare($sql);
$stmt -> bindParam(':catid',$ids);
$stmt -> bindParam(':id',$seid);
$stmt->execute();
}
Works fine, but seems to be complicated. Is there an easier way to do the same?