提问人:d212digital 提问时间:11/24/2022 最后编辑:ADysond212digital 更新时间:11/24/2022 访问量:28
从mysql数据库创建了一个选择下拉选项,但我还想从相同的表单下拉PHP中添加一个全选选项
Created a Select dropdown option from mysql db but I also want to add a select all option from the same form dropdown PHP
问:
我有一个简单的表格,它使用下拉列表从 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 语句以在表单中查找“所有”并简单地选择所有记录,但如果我再次选择教授,它什么也没返回,可以吗?有没有办法做到这一点?
这是我尝试过的 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);
}
}
任何帮助将不胜感激。
谢谢
大卫。
答:
-1赞
d212digital
11/24/2022
#1
所以谢谢你们的输入,这是我放在一起的,假设它仍然对 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>';
?>
但是,当选择“全部”时,它会起作用并拉入所有记录
评论
1赞
ADyson
11/24/2022
assuming this is still open to SQL injection?
...正确。请参阅我在另一个评论线程中提供的链接。
0赞
ADyson
11/24/2022
从逻辑上讲,所有循环浏览结果并显示它们的东西都需要在 不过中,否则你会发现即使没有选择任何选项,它也可以尝试运行该代码 - 因此没有执行任何查询。显然,当它找不到要循环的时,这将导致错误。if (isset($_POST['main_select'])) {
$result
0赞
d212digital
11/24/2022
再次感谢您的反馈,我从头开始使用 php,我多年前在 magento 1 上工作过,从那时起发生了很多变化。:-)我已经检查了该链接,并将从现在开始使用准备好的语句进行编码。:-)
评论
isset($_POST['main_select' == 'all'])
- 首先,这不是你使用 isSet 的方式(它检查变量是否存在,而不是它包含的内容),其次,它当然永远不会进入那个 else 分支,而这已经是真的。(只要您提交该表单并选择任何选项,就可以了。if (isset($_POST['main_select']))
if($_POST['main_select'] == 'all')
else
isset($_POST['main_select' == 'all'])
if (isset($_POST['main_select') { if ($_POST['main_select'] == "all") { /* code to select all without restriction */ } else { /* code to select by position */ } /* code to display the results */ }