dry69034 2016-10-09 14:10
浏览 274
已采纳

PHP - 尝试通过for循环将数据插入数据库,但只填充一行

I am trying to insert data into database using for loop. Here is my code:

<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "objednavky";

// ******************************************************

include 'simple_html_dom.php';

$html = file_get_html('http://www.quickbistro.cz/cs/rozvoz');
$count = substr_count($html, '<li class="item">');
$jidloA = array();
$cenaA = array();

// ********************************************************

$conn = mysqli_connect($servername, $username, $password, $dbname);

if ($conn->connect_error) {
    $message = "Connection failed: " . $conn->connect_error;
} else {
    for ($x = 0; $x <= $count; $x++) {
        $cenaA[$x] = $html->find("span[class=price]", $x);
        $jidloA[$x] = $html->find("div[class=article]", $x);
        $jidloSQL = strip_tags($jidloA[$x]);
        $cenaSQL = strip_tags($cenaA[$x]);
        $id = $x + 1;
        $array = array(
            "id" => $id,
            "popis" => "$jidloSQL",
            "cena" => "$cenaSQL"
        );
        $sql = "INSERT INTO jidla";
        $sql.= " (`" . implode("`, `", array_keys($array)) . "`)";
        $sql.= " VALUES ('" . implode("', '", $array) . "') ";
    }
}

if ($conn->query($sql) === TRUE) {
    echo "Data inserted succesfully";
}
else {
    echo "error creating table" . $conn->error;
}

$conn->close();
?>

When I run the script it fills table my only with one row where only ID is filled with number 81 (number of items I am trying to extract). When I tried to insert just one row with without loop it was alright and row inserted item correctly so I am guessing the problem is somewhere in my for loop.

  • 写回答

1条回答 默认 最新

  • doumei8126 2016-10-09 14:25
    关注

    Your problem is that your $conn->query($sql) which runs the insert is not inside your for loop, so it is not being run on every loop, it's only being run after your for loop is finished and therefore it is only inserting the results of the final loop.

    try doing it this way:

        for($x = 0; $x <= $count; $x++) {
                $cenaA[$x] = $html->find("span[class=price]", $x);
                $jidloA[$x] = $html->find("div[class=article]", $x);
                $jidloSQL = strip_tags($jidloA[$x]);
                $cenaSQL = strip_tags($cenaA[$x]);
                $id = $x +1;    
                $array = array(
                   "id" => $id,
                   "popis" => "$jidloSQL",
                   "cena" => "$cenaSQL"
                );
    
                $sql  = "INSERT INTO jidla";             
                $sql .= " (`".implode("`, `", array_keys($array))."`)";
                $sql .= " VALUES ('".implode("', '", $array)."') ";
    
                if($conn->query($sql) === TRUE) {
                    echo "Data inserted succesfully";
                } else {
                    echo "error creating record" . $conn->error;
                }  
    
            }  
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 请问为什么我配置IPsec后PC1 ping不通 PC2,抓包出来数据包也并没有被加密
  • ¥200 求博主教我搞定neo4j简易问答系统,有偿
  • ¥15 nginx的使用与作用
  • ¥100 关于#VijeoCitect#的问题,如何解决?(标签-ar|关键词-数据类型)
  • ¥15 一个矿井排水监控系统的plc梯形图,求各程序段都是什么意思
  • ¥50 安卓10如何在没有root权限的情况下设置开机自动启动指定app?
  • ¥15 ats2837 spi2从机的代码
  • ¥200 wsl2 vllm qwen1.5部署问题
  • ¥100 有偿求数字经济对经贸的影响机制的一个数学模型,弄不出来已经快要碎掉了
  • ¥15 数学建模数学建模需要