使用 PHP 读取和排序用户的下拉菜单

我正在做一个项目,我需要在用户中阅读(我使用 MySQL)并且能够排序 1. 男性/女性 2. 薪水(例如 30k+、50k+、100k+...)


我试过设置一个选择下拉菜单,但由于某种原因,它只显示男性,即使我选择了女性。


<form action="#" method="post">

      <select name="Gender">

         <option value=''>Select Gender</option>

         <option value="Men">Men</option>

         <option value="Women">Women</option>

      </select>

   <input type="submit" name="submit" value="Get Selected Values" />

</form>

if(isset($_POST['submit']) && $_POST['submit'] = "Men"){

        $selected_val = $_POST['Gender'];  

        echo "You have selected :" .$selected_val;

        $conn = create_Conn();

        $sql = "SELECT * FROM users WHERE kon='Man'";

        $result = $conn->query($sql);

        if (isset($_SESSION['anvnamn'])) {

            while($row = $result->fetch_assoc()) {

              //Prints user data

            }

        }

        else {

            while($row = $result->fetch_assoc()) {

             //Prints user data but emails

            }

        }


}

elseif (isset($_POST['submit']) && $_POST['submit'] = "Women"){

        $selected_val = $_POST['Gender'];  

        echo "You have selected :" .$selected_val;

        $conn = create_Conn();

        $sql = "SELECT * FROM users WHERE kon='Woman'";

        $result = $conn->query($sql);

        if (isset($_SESSION['anvnamn'])) {

            while($row = $result->fetch_assoc()) {

               //Prints user data

            }

        }

        else {

            while($row = $result->fetch_assoc()) {

               //Prints user data but emails

            }

        }


}

else {

        print("-");

}


潇潇雨雨
浏览 138回答 2
2回答

蛊毒传说

您已经在ifs 中分配了值,而不是与它们进行比较。此外,您使用了错误的输入进行比较。$_POST['submit']将始终包含值Get Selected Values。if (isset($_POST['submit']) && $_POST['Gender'] === "Men") {&nbsp; &nbsp; $selected_val = $_POST['Gender'];&nbsp; &nbsp; echo "You have selected :" . $selected_val;&nbsp; &nbsp; $conn&nbsp; &nbsp;= create_Conn();&nbsp; &nbsp; $sql&nbsp; &nbsp; = "SELECT * FROM users WHERE kon='Man'";&nbsp; &nbsp; $result = $conn->query($sql);&nbsp; &nbsp; if (isset($_SESSION['anvnamn'])) {&nbsp; &nbsp; &nbsp; &nbsp; while ($row = $result->fetch_assoc()) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //Prints user data&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; while ($row = $result->fetch_assoc()) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //Prints user data but emails&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }} elseif (isset($_POST['submit']) && $_POST['Gender'] === "Women") {&nbsp; &nbsp; $selected_val = $_POST['Gender'];&nbsp; &nbsp; echo "You have selected :" . $selected_val;&nbsp; &nbsp; $conn&nbsp; &nbsp;= create_Conn();&nbsp; &nbsp; $sql&nbsp; &nbsp; = "SELECT * FROM users WHERE kon='Woman'";&nbsp; &nbsp; $result = $conn->query($sql);&nbsp; &nbsp; if (isset($_SESSION['anvnamn'])) {&nbsp; &nbsp; &nbsp; &nbsp; while ($row = $result->fetch_assoc()) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //Prints user data&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; while ($row = $result->fetch_assoc()) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //Prints user data but emails&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }} else {&nbsp; &nbsp; print("-");}这里的代码更简单,冗余更少。并假设您使用的是 PHP PDO。if (strtolower($_SERVER['REQUEST_METHOD']) === 'post') {&nbsp; &nbsp; $gender = $_POST['Gender'] ?? null; // your old $selected_val variable&nbsp; &nbsp; if (!$gender) {&nbsp; &nbsp; &nbsp; &nbsp; // do something to abort the execution and display an error message.&nbsp; &nbsp; &nbsp; &nbsp; // for now, we're killing it.&nbsp; &nbsp; &nbsp; &nbsp; print '-';&nbsp; &nbsp; &nbsp; &nbsp; exit;&nbsp; &nbsp; }&nbsp; &nbsp; /** @var PDO $dbConnection */&nbsp; &nbsp; $dbConnection = create_Conn();&nbsp; &nbsp; $sql = 'SELECT * FROM users WHERE kon = :gender';&nbsp; &nbsp; $stmt = $dbConnection->prepare($sql);&nbsp; &nbsp; $stmt->bindParam('gender', $gender);&nbsp; &nbsp; $stmt->execute();&nbsp; &nbsp; foreach ($stmt->fetchAll() as $user) {&nbsp; &nbsp; &nbsp; &nbsp; if (isset($_SESSION['anvnamn'])) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Prints user data&nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Prints user data but emails&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}

蝴蝶不菲

由于丹在我之前提供了一个宏伟的答案,现在这只是要审查的内容。如果您查看表单,则有两个元素。在提交时,您的脚本将看到..性别 - $_POST['Gender'] 将是 ''、'Men' 或 'Women'提交 - $_POST['submit'] 将为空或值“获取选定值”。如果 php 文件被其他东西调用,它只能为 null。您可以通过在第一个 if() 之前在代码中使用命令 print_r($_POST) 来查看这一点。这允许您测试和检查调试期间实际发布的内容。因此,要查看表单是否已发布,您可以使用外部检查提交代码,然后检查 Gender 的状态。以下是对 IF() 的更正,以及一些对代码进行整理的建议。<?php// Process the form data using Ternary operators// Test ? True Condition : False Condition$form_submitted = isset($_POST['submit'])? $_POST['submit']:FALSE;$gender = isset($_POST['Gender'])? $_POST['Gender']:FALSE;if($form_submitted){&nbsp; if($gender == 'Men') {&nbsp; &nbsp; // Stuff here&nbsp; }&nbsp; else if($gender == 'Women') {&nbsp; &nbsp; // Stuff here&nbsp; }&nbsp; else {&nbsp; &nbsp; print("-");&nbsp; }} else {&nbsp; // Optional: Case where the form wasn't submitted if other code is present.}您也可以考虑使用 switch / case 结构。我会把它留给你查查。
打开App,查看更多内容
随时随地看视频慕课网APP