2011-02-28 2 views
0

내 메인 페이지 - index.php에있는이 양식의 데이터를 사용하려고했습니다.PHP를 사용하여 <select> 태그에서 데이터를 수집

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의 테이블에서 데이터를 보여줍니다

<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 코드의 주요 부분이다. 왜? 솔직히

+0

* (관련 항목) * [$ _REQUEST를 $ _POST (으)로 변경해야합니까?] (http://stackoverflow.com/questions/2987447/should-i-change-request-to-post/2987468#2987468) – Gordon

답변

1

사용 == 대신 =.

4

변수를 설정하는 단일 등호를 사용하고 있습니다. double은 비교됩니다.

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

'articles'체크도 가능합니다. –

0

등호가 없으므로 등호 테스트 대신 지정을합니다.

if ($wheretosearch = 'Articles') 

해야 ...

if ($wheretosearch == 'Articles') 

또한 : 당신은 당신의 쿼리를 탈출하지 않는, 그래서 당신의 데이터베이스는 쉽게 해킹이 불가능한 것이다. 쿼리에 변수를 사용하기 전에, 지금처럼 탈출 :

$wheretosearch = mysql_real_escape_string($wheretosearch); 
1

=을 =

== 귀하의 조건을 지정하지 비교 연산을 포함하는 경우!.

일반적인 실수로 많은 블로그에 쉽게 실수를 피할 수 있습니다.

관련 문제