dsgdfh302506 2018-03-28 18:19
浏览 53
已采纳

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

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 />
";
            $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 . "')");
}
  • 写回答

1条回答 默认 最新

  • dqd3690 2018-03-29 12:38
    关注

    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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘