从mysql数据库创建了一个选择下拉选项,但我还想从同一个表单下拉PHP中添加一个全选选项

2ic8powd  于 2022-11-28  发布在  PHP
关注(0)|答案(1)|浏览(101)

我有一个简单的表单,它使用下拉列表从phpmyadmin数据库中按位置选择团队成员,并在index.php文件中使用php。
这将完美地返回行,效果很好,但是,我希望在同一表单中也有一个选项,可以选择该表中的所有记录
下面是html表单

<form id="main_select" action="view_members.php" method="POST">
    <select name='main_select' required>
        <option value="" disabled selected>Select staff position</option>
        <option value="all">View All Members</option>
        <option value="Professor">Professor</option>
        <option value="Senior Lecturer">Senior Lecturer</option>
        <option value="Reader">Reader</option>
        <option value="Lecturer">Lecturer</option>
    </select>
    <input type="submit" value="View Selected Staff Members">
</form>

这里是view_members.php,当选择教授选项时,它可以很好地工作

<?php
if (isset($_POST['main_select'])) {
  $position = $_POST['main_select'];
  $statement = "SELECT * FROM staff_members WHERE position = '$position'";
  $result = mysqli_query($conn, $statement);
}

?>

<?php
echo '<table align="center" border="0" cellspacing="35" cellpadding="2" width="100%">';
echo "<thead><tr><th>ID</th><th>Name</th><th>Email</th><th>Position</th><th>Update</th> 
<th>Action</th></tr></thead>";
while ($row = mysqli_fetch_assoc($result)) {
  echo "<tr>";
  echo "<td>{$row['id']}</td>";
  echo "<td>{$row['name']}</td>";
  echo "<td>{$row['email']}</td>";
  echo "<td>{$row['position']}</td>";
  echo "<td><a href='edit_member.php?id={$row['id']}'>Edit</a></td>";
  echo "<td><a href='delete_member.php?id={$row['id']}'>Delete</a></td>";
  echo "</tr>";
}
echo "</table>";
echo '<p><a href="index.php">Back</a></p>';
?>

然后,我尝试添加一个else语句,在表单中查找“all”,并简单地选择所有记录,但如果我再次选择professor,它将不会返回任何结果,是否正常?有什么方法可以做到这一点吗?
下面是我尝试使用的if else代码

<?php
if (isset($_POST['main_select'])) {
  $position = $_POST['main_select'];
  $statement = "SELECT * FROM staff_members WHERE position = '$position'";
  $result = mysqli_query($conn, $statement);
} else {
if (isset($_POST['main_select' == 'all'])) {
  $statement = "SELECT * FROM staff_members";
  $result = mysqli_query($conn, $statement);
}

}
如能提供任何帮助,我们将不胜感激。
谢谢
大卫大卫大卫

yvgpqqbh

yvgpqqbh1#

所以,谢谢你们的意见,伙计们,这是我放在一起假设这仍然是开放的SQL注入?

<?php
if (isset($_POST['main_select'])) {
    $position = $_POST['main_select'];
    if ($position == "all") {
        $statement = "SELECT * FROM staff_members";
    } else {
        $statement = "SELECT * FROM staff_members WHERE position = '$position'";
    }
    $result = mysqli_query($conn, $statement);
}

echo '<table align="center" border="0" cellspacing="35" cellpadding="2" width="100%">';
echo "<thead><tr><th>ID</th><th>Name</th><th>Email</th><th>Position</th><th>Update</th><th>Action</th></tr></thead>";
while ($row = mysqli_fetch_assoc($result)) {
  echo "<tr>";
  echo "<td>{$row['id']}</td>";
  echo "<td>{$row['name']}</td>";
  echo "<td>{$row['email']}</td>";
  echo "<td>{$row['position']}</td>";
  echo "<td><a href='edit_member.php?id={$row['id']}'>Edit</a></td>";
  echo "<td><a href='delete_member.php?id={$row['id']}'>Delete</a></td>";
  echo "</tr>";
}
echo "</table>";
echo '<p><a href="index.php">Back</a></p>';
?>

但是,当选择“全部”时,它会工作并拉入所有记录

相关问题