【问题标题】:How do I get a <select> <option> to sort and return a query in mysql database如何让 <select> <option> 在 mysql 数据库中排序并返回查询
【发布时间】:2018-06-29 23:57:57
【问题描述】:

我一直试图让我从 mysql 数据库中选择、排序和返回查询,但它一直返回整个信息表,而不是我试图获取的选定信息。我有 HTML 和 PHP 代码。有人可以帮帮我吗?每当我在 WHERE 字段中的项目上包含“:”时,我都会收到“未定义”错误,例如未定义类别等... 这是代码:

  <?php
  if(isset($_GET['search'])) {
  try {
     $dsn = 'mysql:host=localhost;dbname=bajan_glasses';
     $db = new PDO($dsn, 'glasses_cms', '8019');

    $sql = 'SELECT id, category, style, color, material, size, price, image,  
  position, caption, visible

            FROM eyeglasses
            WHERE id LIKE id  AND category = category AND style = style AND 
  color = color
             AND material = material AND size = size AND price = price AND 
  image = image AND  position = position
            AND caption = caption AND visible = visible
            ORDER BY id'; 
    // <!--------------------- when ":" is added to the above , the table 
 does not display------------->   

    $stmt = $db->prepare($sql);
  $stmt->bindValue(':id', '%' . $_GET['id'] . '%');
    // $stmt->bindParam(':id', $_GET['id'], PDO::PARAM_INT);
    $stmt->bindParam(':category', $_GET['category'], PDO::PARAM_STR);
    $stmt->bindParam(':style', $_GET['style'], PDO::PARAM_STR);
    $stmt->bindParam(':color', $_GET['color'], PDO::PARAM_STR);
    $stmt->bindParam(':material', $_GET['material'], PDO::PARAM_STR);
    $stmt->bindParam(':size', $_GET['size'], PDO::PARAM_INT);
    $stmt->bindParam(':price', $_GET['price'], PDO::PARAM_INT);
    $stmt->bindParam(':image', $_GET['image'], PDO::PARAM_INT);
    $stmt->bindParam(':position', $_GET['position'], PDO::PARAM_INT);
    $stmt->bindParam(':caption', $_GET['caption'], PDO::PARAM_STR);
    $stmt->bindParam(':visible', $_GET['visible'], PDO::PARAM_INT);

   //  $stmt->execute('bindParam'); // use://if still getting token error.
     $stmt->execute(); 

     if (isset($errorInfo[2])) {
        $error = $errorInfo[2];
     }
  } catch (Exception $e) {

    $error = $e->getMessage();
   }
  }

 ?>



<!DOCTYPE html>
<html>
<head>
 <meta charset="UTF-8">
 <title>PDO: SELECT Loop</title>
 <link href="../../styles/styles.css" rel="stylesheet" type="text/css">
 </head>
 <body>



<form method="get" action="<?php echo $_SERVER['PHP_SELF']; ?>">




<fieldset>

<p>

<label for="id">id </label>
 <select name="id" id="id">
 <?php

//$material = array('woo' => 'wood');
//$material = array('ace' => 'acetate');
///$material = array('pla' => 'plastic');
//$material = array('ste' => 'steel');



  for ($id =0; $id <=1 ; $id++) { 
  echo "<option>$id</option>";
    };

            ?>         



    </select>




 <label for="category ">category </label>
 <select name="category " id="category ">


     <option> </option>
       <option>men</option>         
        <option>women</option>
        <option>children</option>

   </select>




<label for="style ">style </label>
<select name="style " id="style ">


   <option  selected value="style">style</option>
       <option>aviator</option>         
        <option>rectangular</option>
        <option>round</option>
        <option>square</option>
        <option>vintage</option>
        <option>black</option>


   </select>




 <label for="color ">color </label>
 <select name="color " id="color ">


        <option></option>
        <option>white</option>
        <option>blue</option>
        <option>green</option>
        <option>yellow</option>
        <option>brown</option>
        <option>black</option>
        <option>red</option>
        <option>pink</option>
        <option>pink</option>

    </select>


<label for="material">Material </label>
<select name="material" id="material">
 <?php

//$material = array('woo' => 'wood');
//$material = array('ace' => 'acetate');
///$material = array('pla' => 'plastic');
//$material = array('ste' => 'steel');



  // for ($material = 'wood'+'steel';   $material <= 'plastic'; $material++) 
  { 
  // echo "<option>$material</option>";
   // };

        ?>         

        <option> </option>
        <option>wood</option>
        <option>acetate</option>
        <option>plastic </option>
        <option>steel </option>
     </select>




  <label for="size">size </label>
<select name="size" id="size">
   <?php

  $size = array('sma' => 'small');
  $size = array('med' => 'medium');
  $size = array('lar' => 'large');


        for ($size= 'small'; $size<= 'medium'; $size++) { 
            echo "<option>$size</option>";

       };

        ?>  

        <option> </option>
        <option>small</option>
        <option>medium</option>
        <option>large</option>

    </select>


 <label for="price ">price</label>
 <select name="price " id="price ">


   <option  selected value="price"></option>
       <option>$199</option>            
        <option>rectangular</option>
        <option>$259</option>
        <option>$129</option>
        <option>$111</option>
        <option>$111</option>


 </select>




 <label for="style ">image </label>
 <select name="image " id="image ">


   <option ></option>



   </select>




 <label for="style ">position</label>
 <select name="style " id="style ">


   <option  selected value="position"></option>
       <option>1</option>           
        <option>2</option>
        <option>3</option>



   </select>



 <label for="style ">caption</label>
 <select name="style " id="style ">


   <option  selected value="style"></option>
       <option>Choose from Mens styles...</option>          
        <option>men glasses</option>
        <option>women glasses</option>
        <option> children glasses...</option>



   </select>       




 <label for="visible  ">visible </label>
 <select name="visible  " id="visible  ">



       <option></option>            
        <option>1</option>
        <option>2</option>
        <option>3</option>



   </select>       



<input type="submit" name="search" value="Search">
</p>
 </fieldset>
 </form>

 <?php if (isset($_GET['search'])) {
 $row = $stmt->fetch();
 if ($row) {

    ?>
  <table>
  <tr>
   <th>id</th>
    <th>category</th>
    <th>style</th>
    <th>color</th>
    <th>material</th>
    <th>size</th>
    <th>price</th>
    <th>image</th>
    <th>position</th>
    <th>caption</th>
     <th>visible</th>

  </tr>
  <?php /******** foreach ($db->query($sql) as $row) {*********/?>
   <?php do { ?>
  <tr>
    <td><?php echo $row['id'];?></td>
    <td><?php echo $row['category'];?></td>
    <td><?php echo $row['style'];?></td>
    <td><?php echo $row['color'];?></td>
    <td><?php echo $row['material'];?></td>
    <td><?php echo $row['size'];?></td>
    <td><?php echo $row['price'];?></td>
    <td><?php echo $row['image'];?></td>
    <td><?php echo $row['position'];?></td>
    <td><?php echo $row['caption'];?></td>      
      <td><?php echo $row['visible'];?></td>
    </tr>

  <?php } while ($row = $stmt->fetch()); ?> 
</table>
  <?php } else {
    echo '<p>No results found.</p>';
 }}
 }   ?>
</body>
</html>

【问题讨论】:

标签: php mysql


【解决方案1】:

这样的比较总是是真的:

category = category

毕竟,什么时候不等于本身。所以确实,表中的每条记录都将始终符合这些条件。

对于所有条件,您都希望将列值与 参数 值进行比较。像这样:

category = :category

对所有参数重复此操作。

看起来您可能绑定:id 参数有点不同,我想知道这是否会导致您在之前尝试更正此代码时提到的错误。 This other question 可能有助于在 PDO 参数中绑定通配符。

【讨论】:

  • 感谢大卫的回复,这确实解决了问题,但现在它根本没有输出任何结果。
  • @MarkSmart:您的WHERE 子句可能不匹配任何记录似乎是合理的。
  • 我只是最后的结果,它回显“未找到结果”。
猜你喜欢
  • 1970-01-01
  • 2018-09-14
  • 2016-09-26
  • 2012-10-21
  • 2012-01-12
  • 1970-01-01
  • 1970-01-01
  • 2016-09-03
  • 1970-01-01
相关资源
最近更新 更多