我如何让每部电影出现在各自的日期
Posted
技术标签:
【中文标题】我如何让每部电影出现在各自的日期【英文标题】:How do i make each movie appear on under their respective date 【发布时间】:2020-02-26 23:49:16 【问题描述】:我似乎无法让每部电影都出现在各自的日期下,并且为电影时间选择提供正确的时间下拉菜单
$sql_date = "SELECT DISTINCT sDate, sTitle FROM movieScreenings ";
$result_date = mysqli_query($db, $sql_date);
while($row = mysqli_fetch_array($result_date))
echo "<h2>" . $row['sDate'] . "</h2>";
$sql_movie = "SELECT * FROM movieList, movieScreenings WHERE title = '" . $row['sTitle'] . "'";
$result_movie = mysqli_query($db, $sql_movie);
while($row2 = mysqli_fetch_array($result_movie))
echo "<div class='box'>
<img class='poster' src='posters/" . $row2['poster'] . "'/>
<h2>" . $row2['title'] . "</h2>
<p>" . $row2['description'] . "</p>";
$sql_time = "SELECT DISTINCT sTime FROM movieScreenings WHERE sTitle = '" . $row2['title'] . "' AND sDate = '" . $row['sDate'] . "'";
$result_time = mysqli_query($db, $sql_time);
while($row3 = mysqli_fetch_array($result_time))
echo "<select name='sTime'>
<option value='" . $row3['sTime'] . "'>" . $row3['sTime'] . "</option>
</select>";
echo "</div>";
mysqli_free_result($result_date);
mysqli_free_result($result_movie);
mysqli_free_result($result_time);
// Close connection
mysqli_close($db);
【问题讨论】:
您的代码易受 SQL 注入攻击。您应该使用准备好的语句。 嗨,Kelvin,我们需要更多关于您正在使用的数据以及您想要实现的目标的信息...... 【参考方案1】:您可能希望使用ORDER BY sDate
按sDate 对您的第一个查询进行排序。
在您的第二个查询中,您再次从 movieScreenings 中进行选择,这不是必需的,因为我想您正在访问的所有数据(海报、标题、描述)都存储在电影列表。
第三,在您的最后一个 while 循环中,您将在每个筛选时间重复 <select>
标记。这将导致多个下拉输入。此外,您将在此循环中包含结束 </div>
标记。所以只循环<option>
标签。
通过这些更改,您的代码将如下所示:
$sql_date = "SELECT DISTINCT sDate, sTitle FROM movieScreenings ORDER BY sDate ASC";
$result_date = mysqli_query($db, $sql_date);
while($row = mysqli_fetch_array($result_date))
echo "<h2>" . $row['sDate'] . "</h2>";
$sql_movie = "SELECT * FROM movieList WHERE title = '" . $row['sTitle'] . "'";
$result_movie = mysqli_query($db, $sql_movie);
while($row2 = mysqli_fetch_array($result_movie))
echo "<div class='box'>
<img class='poster' src='posters/" . $row2['poster'] . "'/>
<h2>" . $row2['title'] . "</h2>
<p>" . $row2['description'] . "</p>";
$sql_time = "SELECT DISTINCT sTime FROM movieScreenings WHERE sTitle = '" . $row2['title'] . "' AND sDate = '" . $row['sDate'] . "'";
$result_time = mysqli_query($db, $sql_time);
echo "<select name='sTime'>";
while($row3 = mysqli_fetch_array($result_time))
echo "<option value='" . $row3['sTime'] . "'>" . $row3['sTime'] . "</option>";
echo "</select>";
echo "</div>";
...
最后,我建议您使用 ID 作为您的外键,以防止将来出现重复。可能有多部电影具有相同的标题。
【讨论】:
以上是关于我如何让每部电影出现在各自的日期的主要内容,如果未能解决你的问题,请参考以下文章