0

我是 php、html 和 mysql 的新手。我有一个数据库,其中包含他们想要出售产品的用户名、产品和价格。该代码用于带有提交按钮的复选框表单,该按钮的作用类似于价格过滤器,提交时应该回显与相应复选框匹配的用户名、产品和价格。. 如果用户选择标记为“0-25”或“100 及以上”的复选框,并单击提交按钮,它应该会回显拥有该特定价格范围内商品的所有用户。我在网上看过并试图操纵一些接近我所拥有的东西。有人可以帮助我或让我了解如何做到这一点吗?任何帮助表示赞赏

这是我的复选框表单。

<form action="pricefilter.php" method="post">
    <br><b>Filter By Price:</b><br><br> 
    <input type="checkbox" name="$0-$25[]" id="Price" value="0-25"/>&nbsp;$0-$25<br><br>
    <input type="checkbox" name="$25-$50[]" id="Price" value="25-50"/>&nbsp;$25-$50<br><br>
    <input type="checkbox" name="$50-$100[]" id="Price" value="50-100"/>&nbsp;$50-$100<br><br>
    <input type="submit" name="submit" value="Submit" />
</form>

这是我的 php 文件的代码。

<?php
mysql_connect ("localhost", "root","root")  or die (mysql_error());
mysql_select_db ("xuswapuser");

$priceFilter = $_GET['priceFilter'];

 $filteredResponse = array ();
foreach($priceFilter as $range)
{
if($range == 025)
    {
        $query = "select * from Books where Price <= 25";
        $sql = mysql_query($query);

        array_push($filteredResponse, $sql);



    }

    if($range == 2550)
    {
        $query = "select * from Books where Price >= 25 AND Price <=50";
        $sql = mysql_query($query);

        array_push($filteredResponse, $sql);
    }
     if($range == 5075)
    {
        $query = "select * from Books where Price >= 50 AND Price <=75";
        $sql = mysql_query($query);

        array_push($filteredResponse, $sql);
    }

     if($range == 75100)
    {
        $query = "select * from Books where Price >= 75 AND Price <=100";
        $sql = mysql_query($query);

        array_push($filteredResponse, $sql);
    }
     if($range == 100)
    {
        $query = "select * from Books where Price >= 100";
        $sql = mysql_query($query);

        array_push($filteredResponse, $sql);
    }

}

?>


 <html>
   <head>
   <title> 
XUSWAP 
</title>
<script type="text/javascript" src="sorttable.js"></script>
</head>
<body style="margin: 0; padding: 0;">
       <div id="header" style="background-color:#339900;height:157px;width:100%;position:relative;">                                      <!--header area-->
            <form id="headerForm" name="headerForm" method="Post" action="" align="right">
                  <input type="image" src="https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcSN3XIAe50cjq5Cf91GbAywPkChmI5HOAqYxgmRN8F5OhW7I_RT5Q" alt="Placeholder" align="left" width="150" height="150">
                  <br><br><br>
                  <input type="text" name="searchInput"id="search" style="width:500px;"/>
                  <input type="submit" name="searchButton" id="searchButton" value="Search" />
                  <input type="submit" name="logoutButton" id="logoutButton" value="Logout"/>
                  <br><br><br>

            </form>
       </div> 
       <div id="background" style="background-color:#FFD700;height:100%; width:100%;"> 
 <div id = "pageContent"></div>
 <div id="background" style="background-color:#FFD700;height:100%; width:100%;"> 
<blockquote>
    <blockquote>
      <blockquote>
        <blockquote>
          <blockquote>
            <blockquote>
              <blockquote>
                <p>Books </p>
              </blockquote>
            </blockquote>
          </blockquote>
        </blockquote>
      </blockquote>
    </blockquote>
  </blockquote>
        <blockquote>

                                <table class="sortable" width="940" height="52" border="0">
                                <tr>
                                  <td width="200" id="sortable">Username</td>
                                  <td width="253"> Product</td>
                                  <td width="220">Condition</td>
                                  <td width="249">Price</td>
                                </tr>
                                <?php do { ?>
                                  <tr>
                                    <td><?php echo $range['id']; ?></td>
                                    <td><?php echo $range['Product']; ?></td>
                                    <td><?php echo $range['Condition']; ?></td>
                                    <td><?php echo $range['Price']; ?></td>
                                  </tr>
                                  <?php } while ($range = mysql_fetch_assoc($sql)); ?>
                              </table>
4

2 回答 2

2

哎哟。:)

  1. IsChecked() 函数有什么作用?它是否已定义,或者您想要 isset() ?
  2. 不要使用 $ 美元符号命名您的字段。
  3. 不要将它们设为数组,如果不是,则您正在使用该组一次,因此它后面不应有 []
  4. 要访问发布的名称字段,您需要 $_POST 超全局,您不能直接通过其名称访问它:

-

 <input name="asd">

如果在 PHP 中发布,将被访问,例如:

$_POST['asd']

并检查它是否已发布:

if(isset($_POST['asd']) { ...

通过使用 $row[] 数组的约定,我假设您要打印查询结果。为了打印结果,您需要获取它们并遍历它们。以您的方式,它将是:

 while ($row = mysql_fetch_assoc($sql)) { ...

但是 mysql_* 函数已被弃用,在您习惯它们之前,最好切换到 mysqli_* 或 PDO 库。

于 2013-04-18T19:52:35.147 回答
0

这只是供您遵循的简单代码,您可以通过将检查的值放入数组并通过 foreach() 执行查询来循环所有可能性

<input type="checkbox" name="priceFilter[]" value="025">
<input type="checkbox" name="priceFilter[]" value="2550">
// etc.

<?php
  $priceFilter = $_POST['pricFilter']; // this is now an array

  if(isset($priceFilter[025]))
  {
      $query25 = "select * from Books where Price <= 25";
  }
  if(isset($priceFilter[2550]))
  {
      $query2550 = "select * from Books where Price >= 25 AND Price <= 50";
  }
  //etc.

  // then run your query if it is set

  if(isset($query25))
  {
      $sql = mysql_query($query25);
  }

  // then do w/e you need to do then run subsequent possibily
?>

这很耗时,但我只是想向您说明如何处理 php 中的复选框

如果您想使用 foreach 进行更新,请更新:

<?php
$priceFilter = $_GET['priceFilter'];
$filteredResponse = array ();
foreach($priceFilter as $range)
{
if($range == 025)
    {
        $query = "select * from Books where Price <= 25";
        $sql = mysql_query($query);

        array_push($filteredResponse, $sql);
    }

    if($range == 2550)
    {
        $query = "select * from Books where Price >= 25 AND Price <=50";
        $sql = mysql_query($query);

        array_push($filteredResponse, $sql);
    }

    // do this for all the conditions and you can save the results in an array of arrays
}

您现在有了一个基于复选框表单选择的 MYSQL 响应数组。然后只需解析您认为合适的信息。

我还建议 - 在继续使用 PDO 而不是 mysql_ 之前,因为它是遗留代码

看看这个 PDO 使用

于 2013-04-18T20:11:49.187 回答