DeltaMike
DeltaMike

Reputation: 33

Running public function for each iteration in a While Loop

I am new to PHP and have done a few tutorials on how to code pages to run code behind. I am having trouble though figuring out why my While loop is only executing a Public Function for the first iteration through the loop. Any ideas?

while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
$num = count($data);
$info = array("name" => "", "class" => "", "description" => "", "characteristics" => "");
        for  ($c=0; $c < $num; $c++) {
            echo $data[$c] . "<br />\n";
            $info[$c] = $data[$c];
        }
        echo print_r($info);
        infoDB::getInstance()->create_record($info[0],$info[1],$info[2],$info[3]);}

The code above prints the $info array for each iteration of the While loop, but the create_record function only inserts the results of the first iteration into the corresponding MYSQL Database. Is there something inherent to the logic in PHP/Instantiation that means it only executes on the first iteration?

Below is the function it is calling to for reference (works correctly for the 1st iteration and then does not recur)

    public function create_record ($info, $class, $description, $characteristics) {
    $this->query("INSERT INTO tbl_info (toon, zeta, description, characteristics)" . " VALUES ('" . $toon . "', '" . $zeta . "', '" . $description . "', '" . $characteristics . "')");
}

Upvotes: 0

Views: 150

Answers (1)

DeltaMike
DeltaMike

Reputation: 33

Problem Solved

public function create_record ($info, $class, $description, $characteristics) {
$this->query("INSERT INTO tbl_info (toon, zeta, description, characteristics)" . " VALUES ('" . $toon . "', '" . $zeta . "', '" . $description . "', '" . $characteristics . "')");

}

Is Now

public function create_record ($info, $class, $description, $characteristics) {
    $name = $this->real_escape_string($name);
    $class = $this->real_escape_string($class);
    $description = $this->real_escape_string($description);
    $characteristics = $this->real_escape_string($characteristics);
$this->query("INSERT INTO tbl_info (toon, zeta, description, characteristics)" . " VALUES ('" . $toon . "', '" . $zeta . "', '" . $description . "', '" . $characteristics . "')");

}

The query was failing based on something it was reading in from the source spreadsheet needing to be escaped

Upvotes: 1

Related Questions