2011-02-28 32 views
0

我試圖使用位於我的主頁 - index.php中的此表單中的數據。使用PHP從<select>標記中收集數據

<center> 
<form action="search.php" method="post"> 
<input type="text" name="search" size="30" /> 

<select name='wheretosearch'> 
    <option value='Articles'>Articles</option> 
    <option value='Users'>Members</option> 
    </select> 

<input type="submit" value="Search" /> 
</form> 
</center> 

這是search.php中代碼的主要部分:

include "all/config.php"; 

    $search = $_REQUEST['search']; //Getting the words 


    $split = split(" ",$search); //If there is more than one I spit them. 

    foreach ($split as $array => $value) 
{  $NewResult .= $value; 
     } 

    $wheretosearch = $_POST['wheretosearch']; 

     if ($wheretosearch = 'Articles') 
     {$Results = mysql_query("SELECT * FROM bgarticles WHERE title LIKE '%$value%' OR description LIKE '%$value%' OR text LIKE '%$value%' OR tags LIKE '%$value%' OR date LIKE '%$value%' OR author LIKE '%$value%' OR ip LIKE '%$value%' "); 

     while($row = mysql_fetch_array($Results)) 
    { 
     echo "<div class='top'>"; 
     echo "<span class='title'>"; 
     echo "<a href=details.php?id=$row[id]>"; 
     echo $row['title']; 
     echo "</a>"; 
     echo "</span> <br><br>"; 
     echo "<span class='author'>"; 
     echo $row['author']; 
     echo "</span>"; 
     echo "<span class='date'> Date: "; 
     echo $row['date']; 
     echo "</span> <br><br><br>"; 
     echo "<br><br></div>" ; 
     echo "<div class='bottom'><br><br></div>"; 
    } 
    } 


     if ($wheretosearch = 'members') 
     {$Results2 = mysql_query("SELECT * FROM members WHERE username LIKE '%$value%' OR firstname LIKE '%$value%' OR lastname LIKE '%$value%' "); 

      while($row2 = mysql_fetch_array($Results2)) 
    { 
     echo "<div class='top'>"; 
     echo "<span class='title'>"; 
     echo "<a href=details.php?id=$row2[id]>"; 
     echo $row2['username']; 
     echo "</a>"; 
     echo "</span> <br><br>"; 
     echo "<span class='author'>"; 
     echo $row2['firstname']; 
     echo "</span>"; 
     echo "<span class='date'> Date: "; 
     echo $row2['date']; 
     echo "</span> <br><br><br>"; 
     echo "<br><br></div>" ; 
     echo "<div class='bottom'><br><br></div>"; 
    } 

     } 

無論我做它總是顯示來自MySQL的表中的數據。爲什麼?

+0

*(相關)* [我應該更改$ _REQUEST到$ _POST](http://stackoverflow.com/questions/2987447/should-i-change-request-to-post/2987468#2987468) – Gordon 2011-02-28 19:33:59

回答

1

使用==,而不是=if語句。

4

您正在使用一個等號,它設置一個變量。雙等於比較。

if ($wheretosearch = 'members') { 
// $wheretosearch is now (always) set to 'members' 
// this will always trigger 
} 

if ($wheretosearch == 'members') { 
// this will only trigger when the above is true 
} 
+0

相同'物品'檢查也是如此。 – 2011-02-28 19:20:39

0

你錯過了一個等號,所以它做了一個任務而不是測試相等。

if ($wheretosearch = 'Articles') 

應該是...

if ($wheretosearch == 'Articles') 

另外:你不逃避你的查詢,讓你的數據庫是很容易被破解。在查詢中使用的變量之前,逃避他們就像這樣:

$wheretosearch = mysql_real_escape_string($wheretosearch); 
1

= = ==

你如果條件包含的任務,而不是比較操作!

常見的錯誤,大量的博客包含簡單的方法來避免它。