0

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
  • 1
    Not sure what are you expecting? You are generating $sql in foreach and calling query outside, which means only one record will be inserted. Commented Oct 9, 2016 at 14:12

1 Answer 1

3

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;
            }  

        }  
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.