2011-01-16 4 views
-3

나는 mysql에서 검색을 위해이 코드를 가지고 있지만 doesent 실행하고 나는 이유를 모른다.PHP로 mysql에서 간단한 검색

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 
    <fieldset> 
    <legend>search Employees</legend> 
    <form name="search" method="post" action= <?=$PHP_SELF?>> 
    Αναζήτηση για: <input type="text" name="find" /> στο 
    <Select NAME="field"> 
    <Option VALUE="fname">First Name</option> 
    <Option VALUE="lname">Surname</option> 
    <Option VALUE="phone">Phone</option> 
    <Option VALUE="address">Address</option> 
    </Select> 
    <input type="hidden" name="searching" value="yes" /> 
    <input type="submit" name="search" value="Search" /> 
    </form> 
    </fieldset> 
    <? 
    //This is only displayed if they have submitted the form 
    if ($searching =="yes") 
    { 
    echo "<h2>Results</h2><p>"; 

    //If they did not enter a search term we give them an error 
    if ($find == "") 
    { 
    echo "<p>You forgot to enter a search term"; 
    exit; 
    } 

    // Otherwise we connect to our Database 
    mysql_connect("localhost", "root", "123") or die(mysql_error()); 
    mysql_select_db("ergasia2") or die(mysql_error()); 

    // We preform a bit of filtering 
    $find = strtoupper($find); 
    $find = strip_tags($find); 
    $find = trim ($find); 

    //Now we search for our search term, in the field the user specified 
    $data = mysql_query("SELECT * FROM EMPLOYEES WHERE upper($field) LIKE'%$find%'"); 

    //And we display the results 
    while($result = mysql_fetch_array($data)) 
    { 
    echo $result['fname']; 
    echo " "; 
    echo $result['lname']; 
    echo "<br>"; 
    echo $result['info']; 
    echo "<br>"; 
    echo "<br>"; 
    } 

    //This counts the number or results - and if there wasn't any it gives them a little message explaining that 
    $anymatches=mysql_num_rows($data); 
    if ($anymatches == 0) 
    { 
    echo "Sorry, but we can not find an entry to match your query<br><br>"; 
    } 

    //And we remind them what they searched for 
    echo "<b>Searched For:</b> " .$find; 
    } 

?> 
+2

을 정확히하지 않는 무엇 작업? 약간의 오류가 발생했습니다. mysql_error()는 무엇을 반환합니까? 다른 오류가 있습니까? 또한, $ find가 데이터 외부에 있다면, mysql_real_escape_string()을 수행 할 필요가 있습니다. –

+0

$ field는 $ _POST [ 'field'] 여야합니다. $ find –

+0

과 $ searching = $ _POST ['수색']. $ searching이 "yes"와 같지 않으면 모든 코드가 전혀 실행되지 않습니다. – Ass3mbler

답변

0

아마도 쿼리가 잘못 되었기 때문일 수 있습니다. 이와 같이 열 이름에 UPPER을 사용할 수 없습니다. 또한 필드 이름이 FIELD (전체 대문자)이거나 Field (첫 번째 문자 만 대문자) 인 경우 확실하지 않습니다. 그리고 귀하의 의견을 피하는 것을 잊지 마십시오!

이 시도 : 테이블 이름/열 이름이 정말로 경우

$escaped_field = mysql_real_escape_string($field); 
$field_name = strtoupper($escaped_field); // if you want FIELD 
$field_name = ucfirst($escaped_field); // if you want Field 
$data = mysql_query("SELECT * FROM EMPLOYEES WHERE `$field_name` LIKE '%$find%'"); 

내가, 전체 대문자를 추가 할 수 있습니다 (예를 들어 : FIELD),이를 변경할 수 있습니다. 대문자 키워드는 일반적으로 예약어이므로 혼란 스러울 수 있습니다.

+0

대문자에 대해서는 들어 본 적이 없습니다. 나는 그것이 어떤 방식 으로든 중요하지 않다고 생각합니다. 당신의 선호는 키워드를 대문자로 유지하는 것이지만, 원하는 경우 키워드와 컬럼 이름이 될 수 있습니다. –

+0

@ 브래드 : 나는 그들이 그들이 원하는 모든 경우가 될 수 있다는 것을 알고 있지만, 여전히 이것을 볼 때 여전히 혼란 스러울 수 있다고 생각합니다. 당신이 알고있는 한가지 제안. – netcoder

+0

문제 없습니다, 방금 사실처럼 발표했습니다. :) 그냥 공기를 조금 비우십시오. (그것도 나의 선호도이다.) –

2

코드는 register_globals는 (PHP의 새로 설치할이가 기본적으로 사용 안함으로 설정됩니다) 켜져 보안 위험이기 때문에이 오프 해야한다고 가정합니다. 나는 등 보안

을 표시하는 SQL 검색을 만드는 대신에 어떤 문제가 있는지에가는에 대한 자습서를 읽어 제안, 나는이 튜토리얼 위로를 썼다, 아마 그것은 당신을 도울 것입니다 : Simple SQL Search

+0

이것은 OP의 문제를 수정하지는 않겠지 만 ** 어쨌든 ** ** register_globals *를 사용하지 않는 것에 대해 언급 한 +1이다. – netcoder