简体   繁体   English

关于从 MySQL 获取的 PHP 数据表的建议

[英]Advice on PHP Data Table fetched from MySQL

I've written some PHP and CSS to query a MySQL database table that I have running on a WAMP server.我编写了一些 PHP 和 CSS 来查询我在 WAMP 服务器上运行的 MySQL 数据库表。 I have two files that run, index.php and test.php.我有两个运行的文件,index.php 和 test.php。 Index contains a selector box which allows the user to choose a filter option from 'family.'索引包含一个选择器框,允许用户从“家庭”中选择一个过滤器选项。 Ultimately I would like for the selected option to be implemented into the SELECT query that determines the data output in the PHP portion of test.php.最终,我希望将所选选项实施到 SELECT 查询中,该查询确定 test.php 的 PHP 部分中的数据输出。

For instance, if the user selects "capacitor" from index.php, I need the table that is created to display only the family entries that match "capacitor" (as well as all of the corresponding details like "capacitance", "voltage", and "price").例如,如果用户从 index.php 中选择“电容器”,我需要创建的表仅显示与“电容器”匹配的系列条目(以及所有相应的详细信息,如“电容”、“电压” ,和“价格”)。 Currently, my PHP just fetches the entire table (Ex. picture below).目前,我的 PHP 只是获取整个表(例如下图)。

在此处输入图片说明

I know that I am needing to use the $_POST super global to fetch the user input and place it into the SELECT query, but I am not sure how.我知道我需要使用 $_POST 超级全局来获取用户输入并将其放入 SELECT 查询中,但我不确定如何。

EDIT: When I attempt to narrow the returned data by using a WHERE modifier, I receive the following error编辑:当我尝试使用 WHERE 修饰符缩小返回的数据时,我收到以下错误

ERROR: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'capacitor' in 'where clause'错误:SQLSTATE[42S22]:未找到列:“where 子句”中的 1054 列“电容器”未知

Below is index.php下面是 index.php

<form action="test.php" method="post">
   <select name="family">
      <option value="" selected="selected">Any family</option>
      <option value="capacitor">capacitor</option>
      <option value="resistor">resistor</option>
      <option value="ferrite bead">ferrite bead</option>
   </select>
   <input name="search" type="submit" value="Search"/>
</form>

</html>

Below is test.php下面是 test.php

<!DOCTYPE html>
<html lang = "en-US">
   <head>
      <meta charset = "UTF-8">
      <title>test.php</title>
      <style>
         table {
         border-collapse: collapse;

         width: 50%;
         }
         th, td {
         text-align: left;
         padding: 8px;
         }
         th {
         background-color: SkyBlue;
         }
      tr:nth-child(odd) {background-color: #f2f2f2;}
      tr:hover {background-color: AliceBlue;} 
      </style>
   </head>

   <body>    
      <p>
      <?php

      try {
         $con= new PDO('mysql:host=localhost;dbname=mysql', "root", "kelly188");
         $con->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
         $query = "SELECT * FROM testv2";

         //first query just gets the column names
         print "<table>";
         $result = $con->query($query);

         //return only the first row (we only need field names)
         $row = $result->fetch(PDO::FETCH_ASSOC);
         print " <tr>";
         foreach ($row as $field => $value){
            print " <th>$field</th>";
            }
         // end loop
         print " </tr>";

         //second query gets the data
         $data = $con->query(**PERTAINS TO EDIT**);
         $data->setFetchMode(PDO::FETCH_ASSOC);
         foreach($data as $row){
            print " <tr>";
               foreach ($row as $name=>$value){
              print " <td>$value</td>";
               } //end row loop
            print " </tr>";
            } //end record loop
         print "</table>";
      } catch(PDOException $e) {
         echo 'ERROR: ' . $e->getMessage();
      } // end try
      ?>
      </p>
   </body>
</html>

You have to capture the family requested:您必须捕获请求的家庭:

$filter_key = "";
if(isset($_POST['family'])){
    $filter_key = $_POST['family'];
}

them yu have to conform your query using the filter setted:他们必须使用设置的过滤器来符合您的查询:

if(!empty($filter_key))
    $query = 'SELECT * FROM testv2 WHERE family = "'.$filter_key.'"';
else
    $query = 'SELECT * FROM testv2';

您可以创建第二个查询作为选择记录,其中系列是您循环的值,如下所示:

$data= $con->query('SELECT * from testv2 where family = $field');

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

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