繁体   English   中英

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

[英]How to get details of items in drop down menu using php and mysql

我有一个数据库,并且在该数据库中有一个名为courses_details的表。 当用户从下拉菜单中选择一门课程时,可以使用php以该表格的形式显示该课程的信息(CourseCode,CourseTitle,CourseCredits)。 我无法得到它。 如果有人能帮助我,我将感到荣幸。

<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的内容。

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

更新 :您的courses.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> 

查看官方PHP $ _POST手册 ,还有$_GET >通过URL site.com?get=".$somevalue;$_REQUEST->(Both $_POST and $_GET)

代码尚未检查

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM