在While循环中为每次迭代运行公共函数

时间:2018-03-28 18:19:26

标签: php mysql

我是PHP的新手,并且已经完成了一些关于如何编写页面来运行代码的教程。虽然我弄清楚为什么我的While循环只是在循环的第一次迭代中执行公共函数,但我遇到了麻烦。有什么想法吗?

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

上面的代码为While循环的每次迭代打印$ info数组,但create_record函数只将第一次迭代的结果插入到相应的MYSQL数据库中。 PHP / Instantiation中的逻辑是否存在固有的东西,这意味着它只在第一次迭代时执行?

以下是它要求参考的函数(对于第一次迭代正常工作,然后不再重复)

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

1 个答案:

答案 0 :(得分:1)

解决问题

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

}

现在

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 . "')");

}

查询失败是基于它从需要转义的源电子表格中读取的内容

相关问题