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

标签 php html mysql json simple-html-dom

嗨,我正在尝试弄清楚如何在抓取数据后将其保存到 mysql 中。 它不会将任何内容插入数据库。我认为这是因为我传递的数据格式不正确?我需要通过 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"
            );

?>

最佳答案

您正在废弃数据并正确生成表格...

正如您所说,当您尝试将 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));

或者

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

不幸的是,此功能已被弃用,并将在将来被删除...因此,根据您的需要,使用 mysqli_real_escape_string()PDO::quote()

关于php - 使用 Simple Dom Parser 将 cURL 数据插入 mysql 时遇到问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19129769/

相关文章:

php - ffmpeg : Concat multiple audio files Lost sound Issue

html - 在 div 中居中图像元素

jquery - 旧浏览器的真正占位符文本修复?

mysql - 如何更改mysql中的存储过程

mysql - 为什么mysqld_multi无法启动mysql?

mysql - 比较sql表中的哈希值

php - 当我定义它时类中 undefined variable

来自另一台服务器的 php 数据库连接异常情况

javascript - 具有多个字段的 jquery ui 自动完成

javascript - 如何创建一个 HTML 模块