简体   繁体   English

我的php文件中的foreach循环怎么了?

[英]What's wrong with the foreach loop in my php file?

So here's some code that the professor went over in class. 这是教授在课堂上讲的一些代码。 I zoned out for a portion and I didn't get something right. 我划了一部分,但没有得到正确的结果。

Here's what the vardump said: 这是vardump所说的:

Notice: Undefined variable: records in < file location > on line 40 注意:未定义的变量:在第40行的<文件位置>中的记录

Warning: Invalid argument supplied for foreach() in < file location > on line 40 警告:第40行的<文件位置>中为foreach()提供了无效的参数

Fatal error: Uncaught Error: Call to a member function bind_param() on boolean in < file location> on line 15 致命错误:未捕获的错误:在第15行的<文件位置>中的布尔值上调用成员函数bind_param()

Error: Call to a member function bind_param() on boolean in < file location > on line 15 错误:在第15行的<文件位置>中的布尔值上调用成员函数bind_param()

I have line 40 and 15 labeled. 我有第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>

EDIT: Added errors for line 15 编辑:添加了第15行的错误

I reviewed your question. 我审查了你的问题。 You should define $records outside the loop as an array . 您应该在循环外将$records定义为array

$res=$mysqli->query('select first, last from 240Insert');
$records = array();
if($res){
    while($rowHolder = mysqli_fetch_array($res,MYSQLI_ASSOC)){
        $records[] = $rowHolder;
    }
}

Hope this will make sense. 希望这会有意义。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM