2013-10-02 1 views
0

안녕하세요. 한 번 긁어 내면 MySQL에 데이터를 저장하는 방법을 알아 보려고합니다. 전달하는 데이터가 올바른 형식이 아니기 때문에 데이터베이스에 아무 것도 삽입하지 않습니다. json을 통해 게시해야합니까? 당신은 데이터를 폐기하고 올바르게 테이블을 생성하는Simple Dom Parser로 mysql에 cURL 데이터를 삽입하는 데 문제가 있습니다.

<head> 
<link rel="stylesheet" href="styles.css" type="text/css"> 
</head> 
<?php 

    //echo phpinfo(); 

    //include the php class to get the div content 
    include_once('simple_html_dom.php');//http: 
    include_once('dbconn.php'); 

    //the name of the curl function 
    function curl_get_contents($url){ 

    //Initiate the curl 
    $ch=curl_init(); 

    curl_setopt($ch, CURLOPT_URL, $url); 
    //removes the header of the webpage 
    curl_setopt($ch, CURLOPT_HEADER, 0); 
    //do not display the whole page 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
    //execute the curl 
    $output = curl_exec($ch); 
    //close the curl so that resources are not wasted 
    //curl_close($ch); 
    return $output; 

    ini_set('max_execution_time', 3000); //300 seconds = 5 minutes 
     } 




    $output = curl_get_contents('http://www.lockweb.com.au/en/site/lockweb/Products/? groupId=469&productId=135'); 

    // Get the complete html from the address 

    $html = str_get_html($output); 

    //get the title 
     $title = $html->find('div.content-wide[id=content]', 0); 
    $description = $html->find('div[class="tab-content content-wide selected"]',0); 
    $spec = $html->find('div[class="tab-content content-wide"]',0); 
    $moreinfo = $html->find('div[id=downloadsitem]',0); 
    $image = $html->find('div.product-presentation',0); 



    $echoHTML = "<table border=1 align=center>"; 
     $echoHTML .="<tr>"; 
     /* adding rows header to table */ 
       $echoHTML .="<th>Title</th>"; 
       $echoHTML .="<th>Description</th>"; 
       $echoHTML .="<th>Specifications</th>"; 
       $echoHTML .="<th>MoreInfo</th>"; 
       $echoHTML .="<th>Image/s</th>"; 
     $echoHTML .="</tr>"; 
     $echoHTML .="<tr>"; 
       $echoHTML .="<td>".$title->plaintext."</td>"; 
       $echoHTML .="<td>".$description->plaintext."</td>"; 
       $echoHTML .="<td>".$spec."</td>"; 
       $echoHTML .="<td>".$moreinfo."</td>"; 
       $echoHTML .="<td>".$image."</td>"; 

     /* end of table */ 
     $echoHTML .= "</table>"; 

     echo $echoHTML; 
     //trying to insert the data into the database 
      $q= ("INSERT INTO `lockwood`.`products` (
      `Title` , 
      `Descr` , 
      `Spec` , 
      `more info` , 
      `image` , 
      `id` 
      ) 
      VALUES (
      '$title', 'teat', 'test', 'test', 'test', NULL" 
      ); 

?> 
+0

PDO::quote() cURL을의 FUNC의 작품을 사용,하지만 난 나머지를 테스트하기 위해 간단한 DOM을 가지고 있겠지. 스크랩 프로세스에서 데이터를 가져 왔는지 확인하십시오 ('var_dump '로 화면에 덤프하십시오). – gwillie

답변

0

...

데이터베이스에 HTML 코드를 삽입 할 때 당신이 말한대로 문제가 발생

...

이 코드 mysql_real_escape_string()

$q= sprintf(("INSERT INTO `lockwood`.`products` (
     `Title` , 
     `Descr` , 
     `Spec` , 
     `more info` , 
     `image` , 
     `id` 
     ) 
     VALUES (
     '%s', 'teat', 'test', 'test', 'test', NULL" 
     ), mysql_real_escape_string($title)); 

또는

사용하여 탈출해야합니다

불행하게도,이 기능이 그래서 필요에 따라 ... 사용되지 않으며 향후에 제거됩니다, 중 mysqli_real_escape_string() 또는

+0

감사합니다. 매력처럼 작동했습니다. 궁금한 사람에게는 내 해결책이 있습니다. $ q를 = \t는 mysql_query ("lockwood'.'products' 'INTO INSERT ( \t \t \t'Title', \t \t \t 'Descr', \t \t \t 'Spec', \t \t \t'더 info', \t \t \t'image' \t \t \t \t(' "($ titlex를 .mysql_real_escape_string.)"'\t) \t \t \t 값 ' ".는 mysql_real_escape_string ($의 descriptionx).' ',' '. ($ specx을)는 mysql_real_escape_string."', '테스트', ' 테스트')"); \t \t \t \t 경우 ($ q를!) \t \t \t \t \t { \t \t \t 다이 (mysql_error를()); \t \t \t \t} \t \t \t \t \t \t \t \t mysql_close ($ 콘); \t \t \t \t \t \t \t \t \t \t \t \t 에코 strip_tags ($ 타이틀); – LWNZ