使用PHP </select>从<select>标签收集数据

时间:2011-02-28 19:16:35

标签: php search-engine

我尝试使用此表单中的数据,该表单位于我的主页 - index.php。

    <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表的数据。为什么呢?

5 个答案:

答案 0 :(得分: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
}

答案 1 :(得分:1)

老实说:http://bobby-tables.com/

(然后检查你的if - 条款,你使用的是赋值(=)而不是比较(==))

答案 2 :(得分:1)

==声明中使用=代替if

答案 3 :(得分:1)

=!= ==

您的if条件是否包含赋值,而非比较操作。

常见的错误,很多博客都有简单的方法来避免它。

答案 4 :(得分:0)

你错过了一个等号,所以它正在进行分配,而不是测试是否相等。

if ($wheretosearch = 'Articles')

应该是......

if ($wheretosearch == 'Articles')

另外:您没有逃避查询,因此您的数据库很容易被黑客攻击。在查询中使用变量之前,请将它们转义为:

$wheretosearch = mysql_real_escape_string($wheretosearch);