简体   繁体   English

从其他页面提取后无法将数据插入数据库mysql

[英]not able to insert data into database mysql after extracting from other page

trying to insert data from content news in website. 试图从网站内容新闻中插入数据。
Below are content of page source, m trying to insert data after extracting from this page. 以下是页面源的内容,试图从该页面提取后插入数据。

Example of page source of website that i want to extract 我要提取的网站页面来源示例

<html>
    ...
    <div class=story-box>
    <img src="http://www.example.com/assets/images/CM.gif">
       <h2>Heading</h2>
       <p>afsdfdfha adhfaksdhf adfhakhf adfhaskfdha fsahfkasdhfaasfdjhasdf ahdfkahsd</p>
       <p>afsdfdfha adhfaksdhf adfhakhf adfhaskfdha fsahfkasdhfaasfdjhasdf ahdfkahsd</p>
       <p>afsdfdfha adhfaksdhf adfhakhf adfhaskfdha fsahfkasdhfaasfdjhasdf ahdfkahsd</p>
       <p>yuoyuouoyuoyuyu oyuiouioyuioyuyiouyoiy youyoiyuioyuioyuyoiuyiuyiyuioyu yuyiu</p>
    </div>
    ...
    </html>

i want to extract and insert into database of contents of heading (inside tag), image(inside tag), all content in p tags. 我想提取标题(inside标签),image(inside标签),p标签中的所有内容并将其插入数据库。

<?php
    include('simple_html_dom.php');
    $url = 'http://www.example.com';
    $html1=file_get_html($url);
    $heading=$html1->find("div.story-box h2",0);
    $heading1=strip_tags($heading);
    echo $heading;

    $image=$html1->find("div.story-box img",0);
    $image1=strip_tags($image);
    echo $image;
    $content=array();
    foreach($html1->find('div.story-box p') as $e)
    {
    $content=$e;
    $content1=strip_tags($content);
    echo "$e <br>";
    }
 ?>

This are process of inserting into database after above php codes 这是在上述php代码之后插入数据库的过程

if(isset($_GET['submit']))
    {
        $connect = mysql_connect("localhost","root","");
        if(! $connect )
            {
            die('Could not connect: ' . mysql_error());
        }

        mysql_select_db("test1");

        $sql = 'INSERT INTO test1 '.
       '(heading,image, article) '.
       'VALUES ( "$heading1", "$image1", "$content1")';
        $retval = mysql_query( $sql, $connect );
        if(! $retval )
        {
        die('Could not enter data: ' . mysql_error());
        }
        echo "Entered data successfully\n";
        mysql_close($connect);
    }
    ?>

this are my sql statement of making table of mysql 这是我制作mysql表的sql语句

CREATE TABLE IF NOT EXISTS `test1` (
  `heading` varchar(400) NOT NULL,
  `image` blob NOT NULL,
  `article` text NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

But inside database i get only $heading1 $image1 $content1 not actual data 但是在数据库内部我只得到$ he​​ading1 $ image1 $ content1而不是实际数据

Your VALUES fields needed quotes around them as they're a string. 您的VALUES字段是字符串,因此需要在它们周围加上引号。 As the data your are entering into the database is unknown I have also added mysql_real_escape_string function around the data to protect you and to sanitise it. 由于您要输入数据库的数据是未知的,因此我还在数据周围添加了mysql_real_escape_string函数,以保护您并对其进行清理。

$sql = 'INSERT INTO test1 '.
   '(heading,image, article) '.
   'VALUES ( "'. mysql_real_escape_string($heading1) . '", "'. mysql_real_escape_string($image1) . '", "'. mysql_real_escape_string($content1). '")';

尝试从$ sql中的变量名中删除引号,即,代替“ $ heading”,请尝试$ heading,因为这是变量名。

Try your code by using something like that: 通过使用以下代码尝试代码:

    $sql = 'INSERT INTO test1 '.
   '(heading,image, article) '.
   'VALUES ( '. $heading1. ', '. $image1. ', '. $content1. ')';

Just like you did before with "." 就像您之前使用“。”一样。 string connector. 字符串连接器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM