2015-01-24 2 views
0

그래서 믿을 수 없을만큼 간단한 HTML 설문 양식을 만들었습니다. 입력 양식 하나와 텍스트 입력 필드 하나만 선택했습니다. 선택 입력란이 "기타"옵션에있는 경우에만 텍스트 입력이 표시됩니다. 모든 것이 훌륭하고 훌륭합니다. JavaScript를 모두 처리 했으므로 훌륭하게 작동합니다.POST "선택"HTML 양식 입력시 PHP 오류가 발생했습니다.

이제 폼 값을 POST로 시도한 다음 데이터베이스 테이블에 값을 삽입하면 문제가 발생합니다. 내가 양식 제출하려고 할 때마다이 오류가 발생되었습니다 때문에 SQL 문자열의 첫 번째 부분을 잘라 것 홀수 따옴표의

Error: INSERT INTO survey (select, other) VALUES ('flyer','') 
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ' other) VALUES ('flyer','')' at line 1 

을, 나는 어떻게 든 SQL을 주입하고 믿을 리드입니다 내 양식 POST 또는 내 PHP 유효성 검사. 코드를 여러 번 검색하여 이상한 따옴표를 찾고, 처음부터 완전히 다시 쓰고, 데이터베이스에서 테이블과 행 이름을 확인하고, 다른 곳에서 작동하도록 변경 한 SQL 문자열을 가져 와서 작동하도록 변경했습니다. 이리. 불행하게도 나는 항상 같은 오류를 낳습니다. 그래서 나는 통찰력을 높이 평가할 것입니다. 여러분 모두가 저를 빌려줄 수 있도록 도와 드리겠습니다.

내 HTML 양식 :

<form action="includes/survey.php" method="POST" id="hear_form"> 
    <label for="hear_select">How did you here about us?</label> 
    <br> 

    <select id="hear_select" name="hear_select"> 
     <option value='flyer'> 
      Flyer left on door 
     </option> 
     <option value='email'> 
      Email from Troop 
     </option> 
     <option value='sodo'> 
      SODO News 
     </option> 
     <option value='conway'> 
      Conway News 
     </option> 
     <option value='southwest'> 
      Southwest Orlando Bulletin 
     </option> 
     <option value='winter'> 
      Winter Park Observer 
     </option> 
     <option value='baldwin'> 
      Baldwin Park Living 
     </option> 
     <option value='facebook'> 
      Facebook 
     </option> 
     <option value='neighborhood'> 
      Neighborhood posting 
     </option> 
     <option value='other'> 
      Other 
     </option> 
    </select> 

    <label id="otherlabel" for="other_type">Where else did you hear about us?</label> 
    <input id="other_type" type="text" name="other_type" maxlength="200" value=""> 

    <input type="submit" value="Submit" id="hear_submit"> 
    </form> 

내 PHP :

require_once 'db_con.php'; 
require_once 'functions.php'; 

$selectErr = ""; 
$otherErr = ""; 

//validating inputs 
if ($_SERVER["REQUEST_METHOD"] == "POST"){ 
    if (empty($_POST["hear_select"])){ 
     $selectErr = "* An answer is required"; 
     $valid = false; 
    }else{ 
     $select = test_input($_POST["hear_select"]); 
     $valid = true; 
    }  

    if (empty($_POST["other_type"])) { 
     $other = test_input($_POST["other_type"]); 
     $valid = true; 
    }else{ 
     if((strlen($_POST["other_type"]) < 200)){ 
      $other = test_input($_POST["other_type"]); 
      $valid = true; 
     }else{ 
      $otherErr = "* An answer must have less than 200 characters"; 
      $valid = false; 
     } 
    } 


if($selectErr != '' || $otherErr != ''){ 
    $valid = false; 
} 

    if($valid){ 
    var_dump($_POST); 
    //inserting variables into the database 
    $sql = "INSERT INTO survey (select, other) VALUES ('$select','$other')"; 
    //checking if all worked, if it did redirect page top next step 
    if ($mysqli->query($sql) === TRUE) { 
     header('Location: index.php') ; 
    } else { 
     echo "Error: " . $sql . "<br>" . $mysqli->error; 
    } 

    $mysqli->close();      

    exit; 
    } 
} 

function test_input($data) { 
    $data = trim($data); 
    $data = str_replace('"', "", $data); 
    $data = str_replace("'", "", $data); 
    $data = stripslashes($data); 
    $data = htmlspecialchars($data); 
    return $data; 
} 
+1

select is reserved 다른 이름을 사용할 수 있습니다. – user254153

답변

0

쿼리에 대한 문제는 SQL 표준에서 예약 된 키워드가 '선택', 제대로 할 경우 탈출해야한다는 것입니다 열 이름으로 사용됩니다.

가장 좋은 옵션은 열의 이름을 바꾸거나 쿼리에서 이스케이프하는 것입니다. 자세한 내용은 다음 링크를 참조하십시오.

Escaping reserved keywords

+1

열의 이름을 변경하면 반복적으로 물지 않습니다. –

+0

임원진! 정말 고마워요. 그런 실수를 할 때 나는 싫어. –

+0

답변을 수락하는 것을 잊지 마십시오. –

관련 문제