duanchifo2866 2015-01-24 03:56 采纳率: 100%
浏览 49
已采纳

尝试POST“选择”HTML表单输入时出现PHP错误[重复]

This question already has an answer here:

So I have an incredibly simple HTML survey form that I created that only has one select drop down input and one text input field. The text input only shows up if the select field is on the "other" option. So that is all well and good, I have JavaScript handling all of it and it works great.

Now the problem appears when I try POST the form values to the PHP file and then insert the values into my database table. I have been receiving this error every time I try to submit the form:

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

Because of the odd single quote that seems to cut off the first part of sql string, I am lead to believe I am somehow SQL injecting myself on my form POST or my PHP validation. I have gone through the code many times searching for weird quotes, have re-written the form completely from scratch, triple checked the table and row names from my database, and even grabbed a SQL string that I know works other places changed it to work here. Unfortunately I always get the same error, so I would greatly appreciate any insight or help you all can lend me.

My HTML Form:

  <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>

My 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;
}
</div>
  • 写回答

1条回答 默认 最新

  • dop20345 2015-01-24 04:01
    关注

    The problem with the query is that 'select' is a reserved keyword in the SQL standard, and must be properly escaped if it is to be used as a column name.

    The best options would be to either rename the column, or escape it in the query. See the following link for more detail.

    Escaping reserved keywords

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大