dongzhong1929 2011-11-29 06:45
浏览 148
已采纳

在mysql中使用mysql_real_escape_string或addslashes时,字符串不会被转义

i use mysql_real_escape_string php function for escape data recieved from a form. my code for recieve form data is :

$std_id     =   mysql_real_escape_string($_POST['std_id']);
$name       =   mysql_real_escape_string($_POST['name']);
$family     =   mysql_real_escape_string($_POST['family']);

for example if enter O'reilly string in name form field , this function work fine and my query done too.but when i go to mysql and my table , see that this string is inserted like O'reilly and not O\'reilly. my query is :

$sql    =   "insert into student set 
            std_id  =   $std_id,
            name    =   '$name',
            family  =   '$family',
            ";

this happens when use addslashes() function too.

  • 写回答

2条回答 默认 最新

  • douniwan_0025 2011-11-29 06:48
    关注

    This is exactly what is supposed to happen. You want to insert the string O'reilly into the database, not O\'reilly, right?

    The slashes merely tell MySQL that the next ' is supposed to be a literal apostrophe, and not the apostrophe/single quote denoting the end of the string.


    PS: You might want to consider using PDO and prepared statements, which offer a much cleaner syntax.

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

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度