循环6次,但只添加了3条记录


looping 6 times, but only adding 3 records

我正在使用简单的html dom抓取数据。然而,似乎有一些奇怪的行为。即使写了6次测试,它也只添加了3条记录。为什么它循环了6次,却只增加了3行?

include('simple_html_dom.php');
$html = file_get_html("http://www.dailydot.com/tags/counter-strike/");
foreach($html->find("//li[@class='span4']") as $element) {
    echo "test";
    $title = strip_tags($element->find("//a[@class='article-title']/h3", 0));
    $img = $element->find("//div[@class='picfx']/a/img[@class='lzy-ld']", 0)->getAttribute('data-original');
    $link = $element->find("//a[@class='article-title']", 0)->href;
    $date = $element->find("//p[@class='byline']/time", 0)->datetime;
    mysqli_query($con, "INSERT INTO news (`title`, `url`, `image_url`, `news_text`, `referer_img`) VALUES ('$title', '$link', '$img', '$full_text_strip', 'test')");
}

可能是因为它失败了3次:插入的D不安全。你应该使用真正的转义字符串。如果你不这样做,如果你的任何变量包含一个简单的引号,你的代码就会失败。(它允许坏人注入sql命令)

include('simple_html_dom.php');
$html = file_get_html("http://www.dailydot.com/tags/counter-strike/");
foreach($html->find("//li[@class='span4']") as $element) {

$title = mysqli_real_escape_string($con, strip_tags($element->find("//a[@class='article-title']/h3", 0)));
$img = mysqli_real_escape_string($con, $element->find("//div[@class='picfx']/a/img[@class='lzy-ld']", 0)->getAttribute('data-original'));
$link = mysqli_real_escape_string($con, $element->find("//a[@class='article-title']", 0)->href);
$date = mysqli_real_escape_string($con, $element->find("//p[@class='byline']/time", 0)->datetime);
mysqli_query($con, "INSERT INTO news (`title`, `url`, `image_url`, `news_text`, `referer_img`) VALUES ('$title', '$link', '$img', '$full_text_strip', 'test')");
  echo "test ".mysqli_error($con);
}