簡體   English   中英

帶有提交按鈕的復選框表單不會回顯數據庫php html sql中的值

[英]Checkbox Form with Submit Button doesn't echo values from database php html sql

我是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>

哎喲。 :)

  1. IsChecked()函數有什么作用? 它是定義的,還是你想要的isset()?
  2. 不要使用$ dollar符號命名您的字段。
  3. 不要讓它們成為陣列,如果不是,那么你正在使用該組一次,因此它不應該有[]
  4. 要訪問已發布的名稱字段,您需要$ _POST superglobal,您無法通過其名稱直接訪問它:

-

 <input name="asd">

如果在PHP中發布,則會被訪問:

$_POST['asd']

並檢查它是否發布將是:

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

通過使用$ row []數組的對流,我假設您要打印查詢結果。 要打印結果,您需要獲取它們並迭代它們。 在你的方式它將是:

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

但是不推薦使用mysql_ *函數,在熟悉它們之前,最好切換到mysqli_ *或PDO庫。

這只是一個簡單的代碼供您遵循,您可以通過將選中的值放入數組並通過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的使用,請注意這一點

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM