2012-01-05 62 views
-1

第一功能是本PHP刮刀写入到MySQL数据库错误

   $output_csv = fopen("Poems.csv", "w"); 
..code here 
       $poem_text = trim($pre->innertext); 

       $poem_text = str_replace('"','',$poem_text); 

       $poem_text = html_entity_decode($poem_text); 

       $poem_text = str_replace(array('\\', "\0", "\n", "\r", "\t", "\x1a")," ",$poem_text); 
...code here 
       $p 

    oem_data = array($author_names[$i], $poem_names[$i], $poem_text, $poem_urls[$i]); 
       fputcsv($output_csv, $poem_data); 
...code here 

最后一个功能是

function writeQuotestoDb() { 
    $input_csv = fopen("Poems.csv", "r"); 
    $author_names = array(); 
    $poem_names = array(); 
    $poem_text = array(); 
    $poem_urls = array(); 

    while (($columns = fgetcsv($input_csv, 1000, ",")) !== FALSE) { 
    array_push($author_names, $columns[0]); 
    array_push($poem_names, $columns[1]); 
    array_push($poem_text, $columns[2]); 
    array_push($poem_urls, $columns[3]); 
    } 
    fclose($input_csv); 

    //$dbh = mysql_connect('localhost', 'poetiv', 'alegado'); 
    $dbh = mysql_connect('localhost', 'root', ''); 
    mysql_select_db('test', $dbh); 
    mysql_query("CREATE TABLE IF NOT EXISTS `poem2` (
      `id` INT(20) NOT NULL AUTO_INCREMENT PRIMARY KEY , 
      `author_name` varchar(128) DEFAULT NULL, 
       `poem_name` varchar(128) DEFAULT NULL, 
       `poem_text` text, 
       `poem_url` varchar(1024) DEFAULT NULL 
      ) ENGINE = MYISAM"); 
    mysql_query("TRUNCATE `poem2`");  
    for ($i=0; $i<count($author_names); $i++) { 
     $poems[$i] = substr($poems[$i],7,(strlen($poems[$i])-14)); 
     $poems[$i] = str_replace('"','',$poems[$i]); 
     $query = "insert into poem2 (author_name,poem_name,poem_text,poem_url) values 
     ('" . $author_names[$i] . "', '" . $poem_names[$i]."', '" . $poem_text[$i]."', '" .$poem_urls[$i] ."')"; 
     if(!mysql_query($query)) { echo $query; 
      } 
     } 
    echo count($author_names)." rows....successfully db updated"; 
    //SELECT count(*) FROM `quotes` WHERE criteria = 'Baby Quotes' 
} 

我得到这样一个例子错误

“你好大父母谷物土星,Virmo大!您是 仿古赞誉和贸易Aggredior,敢于开封来源。 玛丽。齿轮。 1 2.当你,我的上帝,农村色调 佩服,2和来自英国的公共职位退休,11月3日 更长的时间,请她忘恩负义的孩子,4个自己的优势 牺牲你的情况; 5我进入外资的领域我的命运 传达,6通过国家卓有成效的不朽的规定,7个其中 软海域

的“英国没有逃脱

林如果它需要是不知道” “或\”

,但我想知道如何解决这个错误,并能够完成编写所有的SQL数据库

回答

3
上把它们插入查询之前你的价值观

使用mysql_real_escape_string
更重要的是,获得与时并进,与prepared statements使用PDO扩展。

+0

11已经阅读了连接执行mysql_real_escape_string之前的SQL。 1在我的处境刮数据,并将它们保存在Poems.csv和写入到SQL数据库,我读了Poem.csv的信息。如何能够把mysql_real_escape_string 1帖子中提到的第一个函数? – merrill 2012-01-06 05:25:38

+0

数据写入文件时,您不使用它,你构建你的'$ query'变量时使用它!请阅读PHP手册中有关使用这些函数的例子。 – deceze 2012-01-06 05:34:04

+0

在这种情况下,你将如何把它:) – merrill 2012-01-06 09:33:47

3

你应该使用PHP的mysql_real_escape_string功能逃脱你的S特林要插入。

+0

11已经看过,之前连接到SQL执行mysql_real_escape_string。 1在我的处境刮数据,并将它们保存在Poems.csv和写入到SQL数据库,我读了Poem.csv的信息。如何能够把mysql_real_escape_string 1帖子中提到的第一个函数? – merrill 2012-01-06 05:25:53

+0

您只需调用这个函数在INSERT语句中已建立的连接后,但你运行的语句之前。出。 $查询= mysql_real_escape_string($查询);的mysql_query($查询); – landoncz 2012-01-06 16:19:09

0

这里是一个函数1遍我所有的数据通过,将其插入我的数据库之前...

private function makeSqlSafe($data){ 
    $str = @trim($data); 
    if(get_magic_quotes_gpc()){ 
    $data = stripslashes($data); 
    } 
    if(!is_numeric($data)){ 
    $data = "'".mysql_real_escape_string($data)."'"; 
    } 
    return $data; 
} 

希望帮助...

+0

1 1在我的处境已经阅读了连接执行mysql_real_escape_string之前,SQL 1凑数据,并将它们保存在Poems.csv和写入SQL数据库,我读了Poem.csv怎样的信息1将能够把mysql_real_escape_string在帖子中提到的第一个函数? – merrill 2012-01-06 05:25:06

+0

将Magic Quotes处理捆绑到'makeSqlSafe'函数中似乎是一个糟糕的主意。无论第一次触摸“$ _POST”阵列时数据会发生什么变化,您都应该去除魔术引用造成的斜杠。如果你想用数据做其他事情*和*把它放在数据库中怎么办?你最终会剥离斜杠两次(这可能会大大改变数据),或者你必须解决自己造成的问题。另外,不要使用'@'。另外,你从不使用'$ str'。总的来说,这不是一个好的功能。 – deceze 2012-01-06 06:16:44