0

こんにちは、データをスクレイピングしたら、データを 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"
            );

?>
4

1 に答える 1

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

また

$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()のいずれかを使用してください。

于 2013-10-02T10:43:02.753 に答える