double0201 2014-02-15 04:07
浏览 23
已采纳

撇号和反斜杠悖论

I've been puzzling with this for a while now, and have finally succumb to asking for help here.

I have a preg_match script for scraping, that matches different variables in a loop and stores them in an array, to finally be posted into a SQL table. The problem occurs whenever a variable contains an apostrophe.

Here's how I grab and organize the data

for($i = 0; $i < count($bokse[0]); $i++){
    preg_match_all("/title=\"Mere information om (.+?)\"/sim", $bokse[0][$i], $name, PREG_SET_ORDER);
    $laeger[$i]['navn'] = stripslashes(mysql_real_escape_string($name[0][1]));
}

Now, the value of something that I grabbed, mysql_escaped and stripped can be:

**Michael D'Angelo** [NOTICE THE APOSTROPHE]

If I want to echo this to the page, I can succesfully echo D'angelo correctly (with apostrophe)

echo $laeger[$i]['navn'];

Will give me Michael D'Angelo.

But when it comes to storing this in a database, I run into a paradox; I can chose to either store it as Michael D\'Angelo

Or store the D, and receive an SQL error.

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 'angelo'
(..........blablabv...............)

Here's how I store the data

mysql_query("INSERT INTO table (name) VALUES ('".$laeger[$i]['navn']."'")");  or die(mysql_error());

Depending on if I stripslashes or not, I'll either mess up the query or mess up the result (with a bachflash). FML.

  • 写回答

2条回答 默认 最新

  • doutou7549 2014-02-20 21:04
    关注

    As you noted in your comment, the accepted answer does nothing for you. It will only work if you know in advance what data you are going to be scraping so you can put the backslashes in the correct place, but of course you do not know in advance so you need a solution that will work at run time. MySQL provides such a solution for you.

    The answer that solves your issue is to change your insert to:

    mysql_query("INSERT INTO table (name) VALUES ('".mysql_real_escape_string($laeger[$i]['navn'])."'")");
    

    Or as I prefer to do it, even though it is non-standard:

     mysql_query("INSERT INTO table set name='".mysql_real_escape_string($laeger[$i]['navn'])."'");
    

    I think that it is much easier to code and much easier to read.

    For my own work, I created a "helper function" named "dbstring" that wraps the apostrophes around the escape function which makes everything even more compact, more readable, transportable between databases and SQL injection attack resistant.

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

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?