php - 如何使用 php 和 mysql 获取下拉菜单中项目的详细信息

标签 php mysql

我有一个数据库,在该数据库中我有一个名为courses_details的表。我希望当用户从下拉菜单中选择一门类(class)时,该类(class)的信息即(类(class)代码、类(class)标题、类(class)学分)可以使用 php 以表格形式显示。我无法得到它。如果有人能帮助我,我将感到荣幸。

<form action="courses.php" method="POST" class="FormStyle">
    <select name="courses">
    <option value="ITC">Intro. To Computing</option>
    <option value="OOP">Object Oriented Programming</option>
    <option value="DS">Data Structures</option>
    </select>
</form>

最佳答案

这就是创建 $_POST[submit] 和 $_POST['courses'] 变量的表单。 members将是您希望显示带有查询信息的表单的页面。

<form action="members.php" method="POST" class="FormStyle">
    <select name="courses">
      <option value="ITC">Intro. To Computing</option>
      <option value="OOP">Object Oriented Programming</option>
      <option value="DS">Data Structures</option>
    </select>
    <input type="submit" name="submit" value="Submit">
</form>

PHP 手册中定义的 $_POST 变量 => An associative array of variables passed to the current script via the HTTP POST method when using application/x-www-form-urlencoded or multipart/form-data as the HTTP Content-Type in the request. .

记住, <input type="submit" name="submit" value="Submit">是输入“按钮”,用于在用户按下按钮后提交表单。此输入中的名称定义为 name="submit" 。因此,一旦我们阅读了有关 POST 变量的手册,我们就会发现它是通过 http 请求传递的表单中的变量的关联数组。因此,我们构建逻辑来检查按钮 submit 是否有效。被压了。 if($_POST['submit']) ,运行与成功按下按钮相关的代码。在输入字段中 name属性是用三元运算符填充的结果。可加selected和/或checked使用相同的方法,或任意数量的属性。

在这里,您将构建逻辑来查询保存该值的数据库和表。成功查询后,定义您想要打印/回显的变量。

更新:您的 course.php 页面看起来像这样。

"your_external_page_source".php

define("HOST", 'yourhostvalue');
define("USER", 'yourusername');
define("PASSWORD", 'yourpassword');
define("DATABASE", 'yourdbname');
define("DB_MEMBERS", 'yourdbtable');

然后在“form_page”.php

include = 'constants.php';
if($_POST['submit']){//here we check to see if the input 'submit' was posted through the form

  //success define your variable(s)

  //$courses will now hold the $_POST variable that passed through the http POST method (the form names as defined in your various forms tags)
  $courses = $_POST['courses'];//assign the value of your select field to the variable $courses

  //connect to DB if not already
  $db = new mysqli(HOST, USER ,PASSWORD, DATABASE);//CONSTANTS from a linked file

// Check for errors
if(mysqli_connect_errno()){
    echo mysqli_connect_error();
}
//using an example DB with a table defined as a constant DB_MEMBERS
$result = $db->query("SELECT * FROM `DB_MEMBERS` WHERE `courses`=".$courses);
if($result->num_rows){// if rows are present from the query
     // Cycle through results and define your variables
    while ($row = $result->fetch_assoc()){
        $courseCode = htmlspecialchars($row['CourseCode']); //*avoid possible exploits with htmlspecialchars()
        $courseTitle = htmlspecialchars($row['CourseTitle']); //*
        $courseCredits = htmlspecialchars($row['CourseCredits']);//*
    }
}

创建一个表单来显示您的结果。

<form action="somelink.php" method="POST">
   <input class="form-group-item" value="<?php $cCode = ($courseCode > 0 ? echo $courseCode : echo ''); echo  $cCode; ?>" name="courseCode">
   <input class="form-group-item" value="<?php $cTitle = ($courseTitle > 0 ? echo $courseTitle  : echo ''); echo  $cTitle; ?>" name="courseTitle">
   <input class="form-group-item" value="<?php $cCredits  = ($courseCredits > 0 ? echo $courseCredits : echo ''); echo  $cCredits;  ?>" name="cCredits ">
   <input type="submit" name="submitMembers" value="Submit">
</form> 

Check out the official PHP $_POST Manual还有$_GET ->通过 URL site.com?get=".$somevalue; 发送和$_REQUEST->(Both $_POST and $_GET)

代码尚未检查

关于php - 如何使用 php 和 mysql 获取下拉菜单中项目的详细信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43818737/

相关文章:

php - create_function() 参数中 undefined variable ?

php编译器比较代码?

php - MySQL 和 PHP : Joins & Counts

mysql - sql 查询在 Windows 上运行正常,但在 Ubuntu 上出现错误

php - 如何从 JavaScript 读取 PHP 变量?

php - 如何在视觉 Composer 中使用短代码

Mysql在子查询中返回匹配的用户

php - 取消选中先前选中的复选框时删除行

php - 有人成功安装了 php-perl 扩展吗?

PHP MYSQL - 根据特定类别添加项目