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

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
    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;
                }  
    
            }  
    
    点赞 评论

相关推荐