我现在有一个数据库,里面有很多学生选择的运动项目。我有一个名为studentchoices.php的页面,允许学生选择他们的运动选项。我该如何用他们的运动选项填充页面中的下拉列表,禁用它们并隐藏提交按钮数据库中是否有一个记录有他们的用户id?
<body>
<div class='container col-md-8 rounded p-5 mt-5 border'>
<h2 class='text-center'>Oundle School Sports Database</h2>
<h4 class='pt-4'><?php echo 'Welcome: '.$_SESSION['name'] ?></h4>
<h5 class='pt-2'>Please fill all forms</h5>
<form action="postChoice.php" method ="post">
<div class='py-2 row'>
<div class='col-md-4'>
<select name="term1sport" class='custom-select'>
<option value=" " selected disabled>Please select a first term sport...</option>
<?php
include_once('connection.php');
try{
$stmt = $conn->prepare(
"SELECT DISTINCT c.Choice_ID, s.Name
From Sports AS s INNER JOIN Choices As c
ON c.Sport_ID = s.Sport_ID INNER JOIN Year As y
ON y.Year_ID = c.Year_ID
Where y.Code Like CONCAT('%', :year, '%') AND
c.Current = 'Y' AND
c.Sex IN (:sex, 'B') AND
c.Term_ID = 1 ORDER BY Name ASC");
$stmt->bindParam(':year', $_SESSION['year']);
$stmt->bindParam(':sex', $_SESSION['sex']);
$stmt->execute();
while ($row = $stmt->fetch(PDO::FETCH_ASSOC))
{
echo("<option value='".$row['Choice_ID']."'>".$row['Name']."</option>");
}
}
catch(PDOException $e)
{
echo "error".$e->getMessage();
}
?>
</select>
</div>
<div class='col-md-4'>
<select name="term2sport" class='custom-select'>
<option value=" " selected disabled>Please select a second term sport...</option>
<?php
include_once('connection.php');
try{
$stmt = $conn->prepare(
"SELECT DISTINCT c.Choice_ID, s.Name
From Sports AS s INNER JOIN Choices AS c
ON c.Sport_ID = s.Sport_ID INNER JOIN Year AS y
ON y.Year_ID = c.Year_ID
Where y.Code Like CONCAT('%', :year, '%') AND
c.Current = 'Y' AND
c.Sex IN (:sex, 'B') AND
c.Term_ID = 2 ORDER BY Name ASC");
$stmt->bindParam(':year', $_SESSION['year']);
$stmt->bindParam(':sex', $_SESSION['sex']);
$stmt->execute();
while ($row = $stmt->fetch(PDO::FETCH_ASSOC))
{
echo("<option value='".$row['Choice_ID']."'>".$row['Name']."</option>");
}
}
catch(PDOException $e)
{
echo "error".$e->getMessage();
}
?>
</select>
</div>
<div class='col-md-4'>
<select name="term3sport" class='custom-select'>
<option value=" " selected disabled>Please select a third term sport...</option>
<?php
include_once('connection.php');
try{
$stmt = $conn->prepare(
"SELECT DISTINCT c.Choice_ID, s.Name
From Sports AS s INNER JOIN Choices As c
ON c.Sport_ID = s.Sport_ID INNER JOIN Year As y
ON y.Year_ID = c.Year_ID
Where y.Code Like CONCAT('%', :year, '%') AND
c.Current = 'Y' AND
c.Sex IN (:sex, 'B') AND
c.Term_ID = 3 ORDER BY Name ASC");
$stmt->bindParam(':year', $_SESSION['year']);
$stmt->bindParam(':sex', $_SESSION['sex']);
$stmt->execute();
while ($row = $stmt->fetch(PDO::FETCH_ASSOC))
{
echo("<option value='".$row['Choice_ID']."'>".$row['Name']."</option>");
}
}
catch(PDOException $e)
{
echo "error".$e->getMessage();
}
?>
</select>
</div>
</div>
<div class='py-2 row'>
<div class='col-md-12'>
<input class='btn btn-success float-right'type="submit" value="Submit Choices">
</div>
</div>
</form>
1条答案
按热度按时间laik7k3q1#
在评论的帮助下,我在数据库中查找用户并计算记录数。如果这个数字大于1,我就查运动的值作为我的下拉列表。然后我使用javascript更新并禁用了它们。可能不是最好的代码,但它工作。