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

报告相同问题?

悬赏问题

  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀