如何使用简单 HTML Dom 从 For 循环作为单个查询将数据插入 MYSQL 数据库

How to Insert Data In MYSQL Database From A For Loop As a Single Query Using Simple HTML Dom

我在下面有这段代码,如何将它们作为单个查询插入,这让我很困惑,因为一些数据应该从 For 循环中获取,知道如何实现吗?现在我真的很困惑,它只向数据库插入最后一个数据。

<?php
$hostname="localhost";
$username="root";
$password="";
$dbname="tz-arb";

  $connect=mysql_connect($hostname,$username,$password)or die("Error cannot connect to 

  database".mysql_error());
  mysql_select_db($dbname,$connect)or die("Failed To Select Database".mysql_error());



  include('advanced_html_dom.php'); 
  $html = file_get_html('yoururl');

   //Finding Numbers of 1x2 Game
  $numcounter = count($gameone = $html->find('div[data-gamename="1X2"]'));


   foreach($html->find(".event-date") as $evdate)
   echo "Event TIME ".$evdate."<br>";

   foreach($html->find(".event-name") as $evname)
   echo "Event NAME ".$evname."<br>";

   for ($i = 0; $i < $numcounter; $i += 3){
   // Set up variables to be inserted
   $home = $html->find('div[data-gamename="1X2"]', $i);
    $draw = $html->find('div[data-gamename="1X2"]', $i + 1);
   $away = $html->find('div[data-gamename="1X2"]', $i + 2);
   echo "1 HOME ".$home."<br>";
   echo "2 DRAW ".$draw."<br>";
   echo "3 AWAY ".$away."<br>";

   // Create unique queries for each INSERT
     $query = "INSERT INTO premier_bet_football 
        (TIME, TEAMS, HOME, DRAW, AWAY) 
 VALUES ('$evdate->plaintext', '$evname->plaintext', '$home->plaintext', '$draw->plaintext','$away->plaintext')";

  // Actually execute each unique INSERT (and log output)
   if (mysql_query($query)) {
    echo "Time And Match Added To Database";
     }
    else {
     echo "RECORD NOT ENTERED SOMETHING WENT WRONG";
     echo mysql_error();
     }

     }


    $html->clear();
     ?>

这些是代码。

您似乎是直接从 HTML DOM 本身插入信息。考虑到您的插入在循环之外,因此只会插入 $home$draw$away 的最后一个值。要解决此问题,您需要在数据库 内插入一个循环。

在你的三个循环中,你基本上只是在抵消索引。这可以通过在每个变量赋值的末尾使用 addition 而不是在单个循环中完成:

for ($i = 0; $i < $numcounter; $i += 3){

  // Event name and date
  $evdate = $html->find(".event-date")[$i]
  echo "Event TIME ".$evdate."<br>";
  $evname = $html->find(".event-name")[$i]
  echo "Event NAME ".$evname."<br>";

  // Set up variables to be inserted
  $home = $html->find('div[data-gamename="1X2"]', $i);
  $draw = $html->find('div[data-gamename="1X2"]', $i + 1);
  $away = $html->find('div[data-gamename="1X2"]', $i + 2);

  // Create unique queries for each INSERT
  $query = "INSERT INTO premier_bet_football 
            (time, teams, home, draw, away) 
     VALUES ('$time->plaintext', '$eventname->plaintext', '$home->plaintext', '$draw->plaintext', '$away->plaintext')";

  // Actually execute each unique INSERT (and log output)
  if (mysql_query($query)) {
    echo "Time And Match Added To Database";
  }
  else {
    echo "RECORD NOT ENTERED SOMETHING WENT WRONG";
    echo mysql_error();
  }

}

此外,我强烈建议切换到MySQLi or PDO, as the mysql_ constructor is deprecated as of PHP 5.5, and outright removed as of PHP 7. You should also look into prepared statements以防止MySQL注入。

希望对您有所帮助! :)