-1

我有一个如下所示的文本文件:

<http://dbpedia.org/resource/Autism> <http://www.w3.org/2000/01/rdf-schema#comment> "Autism is a disorder of neural development characterized by impaired social interaction and communication, and by restricted and repetitive behavior. The diagnostic criteria require that symptoms become apparent before a child is three years old. Autism affects information processing in the brain by altering how nerve cells and their synapses connect and organize; how this occurs is not well understood."@en .
<http://dbpedia.org/resource/Anarchism> <http://www.w3.org/2000/01/rdf-schema#comment> "Anarchism is generally defined as the political philosophy which holds the state to be undesirable, unnecessary, and harmful, or alternatively as opposing authority and hierarchical organization in the conduct of human relations. Proponents of anarchism, known as \"anarchists\", advocate stateless societies based on non-hierarchical voluntary associations. There are many types and traditions of anarchism, not all of which are mutually exclusive."@en .
<http://dbpedia.org/resource/Achilles> <http://www.w3.org/2000/01/rdf-schema#comment> "In Greek mythology, Achilles was a Greek hero of the Trojan War, the central character and the greatest warrior of Homer's Iliad. Plato named Achilles the most handsome of the heroes assembled against Troy. Later legends (beginning with a poem by Statius in the 1st century AD) state that Achilles was invulnerable in all of his body except for his heel. As he died because of a small wound on his heel, the term Achilles' heel has come to mean a person's principal weakness."@en .

我正在使用代码(此处不相关)来提取每行第一个 url 中的文章名称。然后我提取引号之间描述的第一句。问题是当我尝试将第一个句子字符串插入到我的表中时,插入失败(回显工作正常)。只需插入没有描述的标题就可以了。有谁知道为什么描述会导致插入失败?

这是我用来获取第一句话的代码:

 $data = fgets($handle); //get line

 $data = str_replace("> ", "-!-", $data);

 dataArr = explode("-!-", $data);

 //Get last part of uri from 1st element in array
  $title = getLastPartOfUrl($dataArr[0]);   
  $desc=preg_replace('/(.*?[?!.](?=\s|$)).*/', '\\1', escape(substr($dataArr[2],1)));

  $db->query("insert into mytable SET title = '".$title."', desc ='".$desc."'");

  function escape($str)
    {
            $search=array("\\","\0","\n","\r","\x1a","'",'"');
            $replace=array("\\\\","\\0","\\n","\\r","\Z","\'",'\"');
            return str_replace($search,$replace,$str);
    }

编辑:我尝试了 urlencode 和 addlashes 都无济于事,在这两种情况下,包括 $desc 字符串都会使插入失败。

4

1 回答 1

4

你逃不掉的title

我也不相信你的escape功能。我不确定是什么$db,但你应该使用正确的参数化查询和 PDO/mysqli

编辑:DESC是 MySQL 中的保留字。您需要在查询中用反引号将其括起来(用作列名时)。

于 2013-02-26T00:27:45.647 回答