duanji4870 2016-09-29 15:59
浏览 75
已采纳

带双引号的PHP / SQL预处理语句

I'm using an HTML input element to accept data from a user and save it in a mySQL database. To do this, I'm using PHP and mysqli to create a prepared statement.

This works very well until I try to accept an input string containing a double quote("). If the input string contains a double quote, the data saved will be the string up to the double quote character.

For example:

I saw a 7'9" person.

Will be saved as:

I saw a 7'9

I've searched high and low, but can't find a solution. I've tried mysqli_real_escape_string, but that just adds backslashes to escape meaningful characters. When coupled with the prepared statement, the backslashes are interpreted literally and the double quote still interrupts the string. So my example would be:

I saw a 7\'9

I even thought maybe the PHP wasn't getting the whole string from the input element, but when I echo the element's value, I do see the whole string.

Here is some example code. I've removed some business details, but this is generally what I'm trying to do.

$sqlSave = $objConnection->prepare('update tbl set Answer=? where ID=?;');
$sqlSave->bind_param("si", $sqlAnswer, $sqlID);

$sqlSave->execute();

I feel like I'm missing something obvious, so please forgive my ignorance. Any help or additional information is appreciated.

  • 写回答

2条回答 默认 最新

  • duanqi5114 2016-09-29 16:11
    关注

    Yes, indeed it's something very obvious. Just look at the generated HTML code and you will immediately get a clue.

    And then learn by heart that HTML attributes have to be always encoded using htmlspecialchars()

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥35 平滑拟合曲线该如何生成
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 自己瞎改改,结果现在又运行不了了
  • ¥15 链式存储应该如何解决
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站