我的php文件中的foreach循环有什么问题?

时间:2018-04-16 03:47:20

标签: php mysqli foreach

所以这是教授在课堂上看到的一些代码。我划出了一部分,我没有得到正确的答案。

这是vardump所说的:

  

注意:未定义的变量:<中的记录文件位置>在第40行

     

警告:为<中的foreach()提供的参数无效文件位置>在第40行

     

致命错误:未捕获错误:在<中的布尔值上调用成员函数bind_param()文件位置>在第15行

     

错误:在<中的boolean上调用成员函数bind_param()文件位置>在第15行

我标有第40和第15行。

<?php
    $path = './';
    require $path.'../../../dbInfo.inc';
    if($mysqli){
        //IF we are adding a new user
        if( !empty($_GET['fName']) && !empty($_GET['lName'])){
            /*
                we are using client entered data - therefore we HAVE TO USE a prepared statement
                1)prepare my query
                2)bind
                3)execute
                4)close
            */
            $stmt=$mysqli->prepare("insert into 240Insert (last, first) values (?, ?)");
            $stmt->bind_param("ss",$_GET['lName'],$_GET['fName']); // LINE 15
            $stmt->execute();
            $stmt->close();
        }
        //get contents of table and send back...
        $res=$mysqli->query('select first, last from 240Insert');
        if($res){
            while($rowHolder = mysqli_fetch_array($res,MYSQLI_ASSOC)){
                $records[] = $rowHolder;
            }
        }
    }
?>
<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8" />
        <title>DB Insert</title>
    </head>
    <body>
        <h3>The List</h3>
        <div id="list">
            <ul>
            <?php
                //var_dump($records);
                foreach($records as $this_row){ // LINE 40
                    echo '<li>' . $this_row['first'] . " " . $this_row['last'].'</li>';
                }
            ?>
            </ul>
        </div>
        <hr/>
        <h3>Add to the list</h3>
        <form action="dbInsertDone.php" method="get">
            First name: <input type="text" id="first" name="fName" />
            Last name: <input type="text" id="last" name="lName"/>
            <input type="submit" value="Add to the List"/>
        </form>
    </body>
</html>

编辑:为第15行添加了错误

1 个答案:

答案 0 :(得分:2)

我回顾了你的问题。您应该将循环外的 ... Days till Service Predicted Service Date 0 ... 0 16/04/2018 1 ... 28 14/05/2018 2 ... 7 23/04/2018 3 ... 54 09/06/2018 4 ... 0 16/04/2018 5 ... 6 22/04/2018 6 ... 365 16/04/2019 7 ... 0 16/04/2018 8 ... 16 02/05/2018 9 ... 200 02/11/2018 定义为$records

array

希望这是有道理的。

相关问题