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;
                }  
    
            }  
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 求一个html代码,有偿
  • ¥100 关于使用MATLAB中copularnd函数的问题
  • ¥20 在虚拟机的pycharm上
  • ¥15 jupyterthemes 设置完毕后没有效果
  • ¥15 matlab图像高斯低通滤波
  • ¥15 针对曲面部件的制孔路径规划,大家有什么思路吗
  • ¥15 钢筋实图交点识别,机器视觉代码
  • ¥15 如何在Linux系统中,但是在window系统上idea里面可以正常运行?(相关搜索:jar包)
  • ¥50 400g qsfp 光模块iphy方案
  • ¥15 两块ADC0804用proteus仿真时,出现异常