dounai7148 2013-10-02 03:58
浏览 41
已采纳

使用Simple Dom Parser将cURL数据插入mysql时遇到问题

Hi I am trying to work out how to save my data to mysql once I have it scraped. It won't insert anything into the database.I assume it's because the data I am passing is not in the right format? Do I need to post this via json?

    <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"
            );

?>
  • 写回答

1条回答 默认 最新

  • drne47241 2013-10-02 10:43
    关注

    You are scrapping data and generating your table correctly...

    The problem as you said, happens when you try to insert the html code into the database...

    This code has to be escaped using 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));
    

    OR

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

    Unfortunately, this function is deprecated and will be removed in the future... So depending on your need, use either mysqli_real_escape_string() or PDO::quote()

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值