2015-05-16 85 views
0

我想根據HTML選擇框的後期值從表中進行選擇。我沒有得到任何結果,我回應的職位價值沒有問題。該聲明適用於它自己,但不會在我使用選擇表單填充它時使用。這只是我的測試,我將在下拉框中添加其他選項。PHP mySQL select via html select

<?php 
if(isset($_POST['value'])) { 
if($_POST['value'] == 'Militaria') { 
    $query = "SELECT * FROM listings WHERE category1=Militaria"; 
} 
else { 
    // query to get all records 
    $query = "SELECT * FROM listings"; 
} 
} 
$sql = mysql_query($query); 
while ($row = mysql_fetch_array($query)){ 
    echo 'Description:' . $row['description']; 
} 
mysql_close($con);  
?> 

這裏是我使用的,誰能告訴我,我要去哪裏錯了HTML表單,我應該做它用不同的方式等等,我是新來的PHP?謝謝!!

<form action='<?php echo $_SERVER['PHP_SELF']; ?>' method='post' name='form_filter' > 
<select name="value"> 
    <option value="all">All</option> 
    <option value="Militaria">Militaria</option> 
</select> 
<br /> 
<input type='submit' value = 'Filter'> 
</form> 
+0

嘗試'的var_dump($ _ POST)',看看是否有價值觀裏面$ _ POST – Lal

+0

附近添加單引號...類別1 = '軍備' –

回答

2

mysql_fetch_array()應接收resorce作爲參數。嘗試mysql_fetch_array($sql)。圍繞 '軍備' 和mysql_fetch_array($ SQL)

0

報價

<?php 

     if(isset($_POST['value'])) { 
      if($_POST['value'] == 'Militaria') { 
       $query = "SELECT * FROM listings WHERE category1='Militaria'"; 
      } 
      else { 
       // query to get all records 
       $query = "SELECT * FROM listings"; 
      } 

      $sql = mysql_query($sql); 
      while ($row = mysql_fetch_array($sql)){ 
       echo 'Description:' . $row['description']; 
      } 
      mysql_close($con); 
     } 
    ?> 

    <form action='<?php echo $_SERVER['PHP_SELF']; ?>' method='post' name='form_filter' > 
    <select name="value"> 
     <option value="all">All</option> 
     <option value="Militaria">Militaria</option> 
    </select> 
    <br /> 
    <input type='submit' value = 'Filter'> 
    </form> 
0

你在你的PHP代碼兩個錯誤。

第1名:引用Militaria。查詢應該是:$query = "SELECT * FROM listings WHERE category1='Militaria'";

2nd:mysql_fetch_array接受執行查詢的結果作爲參數。它應該是,$row = mysql_fetch_array($sql)

最終代碼:

<?php 
if(isset($_POST['value'])) { 
if($_POST['value'] == 'Militaria') { 
    $query = "SELECT * FROM listings WHERE category1 = 'Militaria'"; 
} 
else { 
    // query to get all records 
    $query = "SELECT * FROM listings"; 
} 
} 
$sql = mysql_query($query); 
while ($row = mysql_fetch_array($sql)){ 
    echo 'Description:' . $row['description']; 
} 
mysql_close($con);  
?>