gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 21:57:41 24 4
gpt4 key购买 nike

我在下面有这段代码,如何将它们作为单个查询插入,这让我很困惑,因为一些数据应该从 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 的最后一个值会被插入。要解决此问题,您需要在循环中内部插入数据库。

在你的三个循环中,你基本上只是简单地偏移索引。这可以在单个循环内完成,方法是在每个变量赋值的末尾使用加法:

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 PDO ,因为 mysql_ 构造函数是 deprecated as of PHP 5.5 , 并且彻底 removed as of PHP 7 .您还应该查看 prepared statements 防止 MySQL 注入(inject)。

希望对您有所帮助! :)

关于php - 如何使用简单的 HTML Dom 从 For 循环将数据作为单个查询插入 MYSQL 数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44532348/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com