发布于 2024-10-19 12:08:40 字数 2690 浏览 1 评论 0原文

我尝试使用位于我的主页 - 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 表的数据。为什么?

I tried to use the data from this form which is located in my main page - 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>

This is the main part of search.php code:

    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>";
    } 

        }

No matter I do it always shows data from both mysql tables. Why?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

猫烠⑼条掵仅有一顆心 2024-10-26 12:08:40

您使用一个等号来设置一个变量。双等于进行比较。

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
}

You're using a single equals sign, which sets a variable. A double equals compares.

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
}
蓝眸 2024-10-26 12:08:40

if 语句中使用 == 而不是 =

Use == instead of = in your if statements.

冷心人i 2024-10-26 12:08:40

= != ==

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

这是常见的错误,很多博客都包含避免这种错误的简单方法。

= != ==

Your if conditions contain assignments, not comparison operations.

Common mistake, plenty of blogs contain easy ways to avoid it.

卷耳 2024-10-26 12:08:40

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

(然后检查您的if-子句,您使用的是赋值 (=) 而不是比较 (==))

Honestly: http://bobby-tables.com/

(and then check your if-clauses, you're using assignments (=) instead of comparisons (==))

演多会厌 2024-10-26 12:08:40

您缺少一个等号,因此它正在进行分配而不是测试相等性。

if ($wheretosearch = 'Articles')

应该是......

if ($wheretosearch == 'Articles')

另外:你没有逃避你的查询,所以你的数据库很容易被黑客攻击。在查询中使用变量之前,请像这样转义它们:

$wheretosearch = mysql_real_escape_string($wheretosearch);

You're missing an equal sign, so it's making an assignment instead of testing for equality.

if ($wheretosearch = 'Articles')

should be...

if ($wheretosearch == 'Articles')

Also: you're not escaping your queries, so your database would be easily hackable. Before using your variables in a query, escape them like so:

$wheretosearch = mysql_real_escape_string($wheretosearch);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文