我正在尝试根据复选框从表中删除多行,但是我不太确定如何最好地去做。目前,我有一个电影列表,我希望能够根据它们是否被选中来删除这些电影中的一部或多部。
// Update Watchlist
if ($submit == 'Update Watchlist') {
require_once("db_connect.php");
$watchlist_name = clean_string($_POST['watchlist-name']);
$watchlist_description = clean_string($_POST['watchlist-description']);
$watchlist_category = $_POST['watchlist-category'];
$updateWatchlist_bad_message = '';
if (!empty($watchlist_name)) {
if ($watchlist_name = clean_string($watchlist_name)) {
$update_watchlist_name_query = "UPDATE watchlists SET name = '$watchlist_name' WHERE watchlist_id = " . $watchlist_id;
mysql_query($update_watchlist_name_query) or die("Insert failed. " . mysql_error() . "<br />" . $$update_watchlist_name_query);
}
}
if (!empty($watchlist_description)) {
if ($watchlist_description = clean_string($watchlist_description)) {
$update_watchlist_description_query = "UPDATE watchlists SET description = '$watchlist_description' WHERE watchlist_id = " . $watchlist_id;
mysql_query($update_watchlist_description_query) or die("Insert failed. " . mysql_error() . "<br />" . $$update_watchlist_description_query);
}
}
if ($watchlist_category != "") {
$update_watchlist_category_query = "UPDATE watchlists SET category = '$watchlist_category' WHERE watchlist_id = " . $watchlist_id;
mysql_query($update_watchlist_category_query) or die("Insert failed. " . mysql_error() . "<br />" . $$update_watchlist_category_query);
}
if(isset($_POST['film-name'])) {
$checkbox = $_POST['film-name'];
$count = count($checkbox);
for($i = 0; $i < $count; $i++) {
$id = (int) $checkbox[$i]; // Parse your value to integer
if ($id > 0) { // and check if it's bigger then 0
mysql_query("DELETE FROM watchlist_films WHERE film_id = $rt_id");
}
}
} else {
$updateWatchlist_bad_message = '<div class="alert alert-error">Sorry, but we can\'t do that at the minute. Please try again later.</div>';
}
require_once("db_close.php");?>
<script type="text/javascript">
window.location = "watchlist.php?id=<?php echo $watchlist_id; ?>"
</script><?php
}
适当的字符串是电影名称,我尝试使用此解决方案 - PHP 删除带有多个复选框的 SQL 行- 但是它不起作用,因为电影没有从包含的监视列表中删除。
基本上,我的查询背后的逻辑如下:
- 检查是否勾选了一个复选框
- 如果勾选了一个复选框,请检查是否还勾选了其他复选框
- 从关注列表中删除所有已勾选的电影
我不确定上述是否是最简单的方法,例如,只需检查是否有任何复选框被一次性选中,而不是在检查之前先检查是否有任何复选框被选中,可能会更简单和更清晰如果其他人也是。
更新
只是想我会用更多信息来澄清-我在监视列表中显示所有电影的实际 foreach 如下(对格式表示歉意):
foreach ($films as $key => $film_item) {
include ("watchlist-film-controller.php");?>
<label class="checkbox input-block-level">
<p class="pull-right"><?php echo $title; ?></p>
<input type="checkbox" class="input-block-level" name="film-name[]"
value="<?php echo $title; ?>">
</label><?php
}
更新 2
为了回答对这篇文章的两个(非常感谢!)评论,这里有一些关于现在正在发生的事情的更多信息。我已经尝试了这两种解决方案,但都没有奏效。就目前而言,我已经实现了 didierc 给出的解决方案,我的代码目前如下所示:
<?php
/*
ini_set('display_errors', 1);
error_reporting(E_ALL);
*/
$rt_id = $film_item['film_id'];
$watchlist_id = $_GET['id'];
include_once('/api/RottenTomatoes.php');
$rottenTomatoes = new RottenTomatoes('2b2cqfxyazbbmj55bq4uhebs', 10, 'us');
/* echo "<pre>"; */
try {
$result = $rottenTomatoes->getMovieInfo($rt_id);
//print_r($result);
} catch (Exception $e) {
//print_r($e);
}
/* echo "</pre>"; */
$title = $result['title'];
$year = $result['year'];
$critics_consensus = $result['critics_consensus'];
$poster_thumb = $result['posters']['thumbnail'];
// Update Watchlist
if ($submit == 'Update Watchlist') {
require_once("db_connect.php");
$watchlist_name = clean_string($_POST['watchlist-name']);
$watchlist_description = clean_string($_POST['watchlist-description']);
$watchlist_category = $_POST['watchlist-category'];
$updateWatchlist_bad_message = '';
if (!empty($watchlist_name)) {
if ($watchlist_name = clean_string($watchlist_name)) {
$update_watchlist_name_query = "UPDATE watchlists SET name = '$watchlist_name' WHERE watchlist_id = " . $watchlist_id;
mysql_query($update_watchlist_name_query) or die("Insert failed. " . mysql_error() . "<br />" . $$update_watchlist_name_query);
}
}
if (!empty($watchlist_description)) {
if ($watchlist_description = clean_string($watchlist_description)) {
$update_watchlist_description_query = "UPDATE watchlists SET description = '$watchlist_description' WHERE watchlist_id = " . $watchlist_id;
mysql_query($update_watchlist_description_query) or die("Insert failed. " . mysql_error() . "<br />" . $$update_watchlist_description_query);
}
}
if ($watchlist_category != "") {
$update_watchlist_category_query = "UPDATE watchlists SET category = '$watchlist_category' WHERE watchlist_id = " . $watchlist_id;
mysql_query($update_watchlist_category_query) or die("Insert failed. " . mysql_error() . "<br />" . $$update_watchlist_category_query);
}
if(isset($_POST['film-name'])) {
$films = array_map('intval', $_POST['film-name']); // make sure that every film id is an integer
mysql_query("DELETE FROM watchlist_films WHERE film_id IN (" . implode(',', $films) . ") AND watchlist_id = " . $watchlist_id);
} else {
$updateWatchlist_bad_message = '<div class="alert alert-error">Sorry, but we can\'t do that at the minute. Please try again later.</div>';
}
require_once("db_close.php");?>
<script type="text/javascript">
window.location = "watchlist.php?id=<?php echo $watchlist_id; ?>"
</script><?php
}
$rt_id
是每部电影的唯一 ID,并被传递给表单,因此查询知道在这种情况下应该删除哪些电影或哪些电影。电影的名称仅用于使实际的删除表单更易于阅读,而不是打印出 ID 号列表,因为用户无法知道哪个 ID 与哪个电影匹配。在尝试了给出的两种解决方案后,似乎都没有工作,但是没有返回错误 - 表单提交,但选定的电影不会从监视列表中删除。
更新 3
作为对 didierc 评论的回应,以下是正在发生的事情的完整细分:
- 监视列表分为两个表格 -
watchlists
和watchlist_films
.watchlists
保存简单信息,例如监视列表 ID、名称和描述,以及创建它的用户的用户 ID。watchlist_films
仅包含 Watchlist ID 和 Watchlist 包含的电影 ID ($rt_id
)。监视列表占用表格中的单行watchlists
和表格中的多行watchlist_films
(因为一个监视列表可以包含多部电影)。 - 电影信息是从烂番茄和 TMDb API 中获取的,这就是电影 ID (
$rt_id
) 的来源——每部电影都有一个完全独特的$rt_id
.
Watchlists 的完整“处理代码”在Update 2中,但是 HTML 呈现如下:
<?php
include("checklog.php");
require_once("watchlist-controller.php");
?>
<!DOCTYPE html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<link rel="shortcut icon" href="img/fav.ico">
<link rel="apple-touch-icon" href="img/apple-touch-icon.png">
<title>Screening - Your ticket to your movies - <?php echo $watchlist_name; ?></title>
<meta name="description" content="Screening is a brand new take on the traditional movie database, fusing social networking and multimedia to provide a clear, concise experience allowing you to share your favourite movies, and discover new classics.">
<meta name="keywords" content="Movies, Films, Screening, Discover, Watch, Share, experience, database, movie database, film database, share film, share films, discover film, discover films, share movie, share movies, discover movie, discover movies">
<!-- Bootstrap -->
<link href="css/bootstrap.css" rel="stylesheet" media="screen">
<link href="css/bootstrap-responsive.css" rel="stylesheet">
<link href="css/custom-bootstrap.css" rel="stylesheet">
<link rel="stylesheet" href="fonts.css" type="text/css" />
<link rel="stylesheet/less" type="text/css" href="css/stylesheet.less" />
<script src="js/less-1.3.3.min.js" type="text/javascript"></script>
<script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.8/jquery.min.js"></script>
<script src="js/bootstrap.min.js"></script>
<!-- Start Google Analytics -->
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-36943512-1']);
_gaq.push(['_trackPageview']);
(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
</script>
<!-- End Google Analytics -->
<!-- Start Google Analytics -->
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-36943512-1']);
_gaq.push(['_trackPageview']);
(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
</script>
<!-- End Google Analytics -->
</head>
<body>
<div class="container"><?php
require_once ("header.php");?>
<div class="well main-content">
<p class="page-title"><?php echo $watchlist_name; ?></p>
<div class="row-fluid">
<section class="span3 sidebar pull-left">
<p class="sidebar-text"><span class="bold">NAME: </span><?php echo $watchlist_name; ?></p>
<p class="sidebar-text"><span class="bold">CATEGORY: </span><?php echo $watchlist_category; ?></p>
<div class="alert alert-info"><?php echo $watchlist_description; ?></div>
<a href="#watchlistUpdate" class="btn btn-primary btn-block" data-toggle="modal" title="Update Watchlist">Update Watchlist</a>
<a href="#watchlistDelete" class="btn btn-danger btn-block" data-toggle="modal" title="Delete Watchlist">Delete Watchlist</a>
<a href="profile.php" class="btn btn-block" title="Logout">Your Profile</a>
</section>
<section class="span9 watchlist-holder">
<!-- Loading bar -->
<!--
<div class="progress progress-striped active">
<div class="bar" style="width: 100%;"></div>
</div>
-->
<ul class="unstyled"><?php
foreach($films as $key => $film_item) {
include ("watchlist-film-controller.php");?>
<li class="well list-item clearfix">
<div class="row-fluid">
<a href="movie.php?id=<?php echo $rt_id; ?>" class="span1 pull-left" title="<?php echo $title; ?>"><img src="<?php echo $poster_thumb; ?>" alt="<?php echo $title; ?> poster" title="<?php echo $title; ?> poster" /></a>
<div class="span11 movie-info">
<p class="search-title"><a href="movie.php?id=<?php echo $film_item['film_id']; ?>" title="<?php echo $title; ?> (<?php echo $year; ?>)"><?php echo $title; ?></a> <small>(<?php echo $year; ?>)</small></p><?php
if ($critics_consensus == "") {?>
<p class="watchlist-synopsis">No overview available</p><?php
} else {?>
<p class="watchlist-synopsis"><?php echo $critics_consensus; ?></p><?php
}?>
</div>
</div>
</li><?php
}?>
</ul>
</section>
<div id="watchlistUpdate" class="modal hide fade" tabindex="-1" role="dialog" aria-labelledby="watchlistUpdateLabel" aria-hidden="true">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<h3 id="watchlistUpdateLabel" class="modal-title">Update Watchlist</h3>
</div>
<form name="watchlist-updater" class="watchlist-updater" action="watchlist.php?id=<?php echo $watchlist_id; ?>" method='POST'>
<div class="modal-body">
<?php echo $updateWatchlist_bad_message; ?>
<div class="alert alert-info">Use the boxes below to change the Watchlist name and description</div>
<input type="text" class="input-block-level" name="watchlist-name" alt="watchlist-name" placeholder="<?php echo $watchlist_name; ?>">
<textarea rows="3" class="input-block-level" name="watchlist-description" title="Watchlist Description" placeholder="<?php echo $watchlist_description; ?>"></textarea>
<label for="Watchlist Category" class="pull-left inline-label" title="Watchlist Category">Watchlist Category</label>
<select class="input-block-level" name="watchlist-category" title="Watchlist Category">
<option value="" title=""></option>
<option value="General" title="General">General</option>
<option value="To watch" title="To watch">To watch</option>
<option value="To share" title="To share">To share</option>
<option value="Favourites" title="Favourites">Favourites</option>
</select>
<div class="alert alert-info">Use the checkbox to the left of each film to remove it from the Watchlist</div><?php
foreach ($films as $key => $film_item) {
include ("watchlist-film-controller.php");?>
<label class="checkbox input-block-level">
<p class="pull-right"><?php echo $title; ?></p>
<input type="checkbox" class="input-block-level" name="film-name" value="<?php echo $title; ?>">
</label><?php
}?>
</div>
<div class="modal-footer">
<button type="button" class="btn" data-dismiss="modal" aria-hidden="true">Close</button>
<button type="submit" class="btn btn-success" name="submit" value="Update Watchlist">Update Watchlist</button>
</div>
</form>
</div>
<div id="watchlistDelete" class="modal hide fade" tabindex="-1" role="dialog" aria-labelledby="watchlistDeleteLabel" aria-hidden="true">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<h3 id="watchlistDeleteLabel" class="modal-title">Delete Watchlist</h3>
</div>
<div class="modal-body">
<?php echo $deleteWatchlist_bad_message; ?>
<div class="alert alert-error alert-block">
<p>Deleting this Watchlist will delete all its containing films from your profile. This information will not be recoverable.</p>
<p>Please only delete this Watchlist if you are absolutely sure you want to purge all the information it contains.</p>
</div>
<p>Are you sure you want to delete this Watchlist?</p>
</div>
<div class="modal-footer">
<form name="watchlist-delete" class="watchlist-delete" action="watchlist.php?id=<?php echo $watchlist_id; ?>" method="POST"><?php
include ("watchlist-film-controller.php");?>
<button type="button" class="btn" data-dismiss="modal" aria-hidden="true">Do not delete this Watchlist</button>
<button type="submit" class="btn btn-danger" name="submit" value="Delete Watchlist">Delete Watchlist</button>
</form>
</div>
</div>
</div>
</div>
<?php include 'footer.html'; ?>
</div>
</body>
</html>
Watchlist 的实际更新在#updateWatchlist 模式中。需要更多信息,我很乐意提供!