Problems with "Uncaught error: Call to a member function

Problems with "Uncaught error: Call to a member function

所以,是的,我坦率地承认,在进行超出基本 html 代码的 Web 开发方面,我是一个彻头彻尾的菜鸟。所以希望大家能帮忙,这是我的问题:

我正在尝试创建一个页面,在其中显示特定数据库中的最后一条记录,以便在创建记录后可以用更多信息更新它。 (显示的信息只是为了让我更新正确的记录。)我在加载站点时一直收到相同的错误消息:

PHP Fatal error: Uncaught Error: Call to a member function query() on null in /home/****/public_html/ins/end/index.php:32 Stack trace:

0 {main}

thrown in /home/****/public_html/ins/end/index.php on line 32

这是我的密码:我使用相同的代码在单独的页面上从数据库中提取信息,它按预期工作。我所做的只是将该页面的那部分代码复制并粘贴到我现在正在处理的页面中。我只是更新数据库名称等。我错过了什么吗?完整页面的代码是:

<html>
<title>Jake's Instacart Info: Add a New Batch</title>
<header>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<h1 align="center">Step 1: Updating a batch</h1>
</header>
<style>
    table, th, td {
    padding: 10px;
    border: 2px solid black; 
    border-collapse: collapse;
    }
      
    p {
    font-size:x-large;
    }
</style>
<body>
<?php
$servername = "localhost";
$username = "******";
$password = "******";
$dbname = "******";
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully <br><br>" ;

$query = "SELECT * FROM `batch` WHERE 1";
 
if ($result = $conn->query($query)) {
 
    while ($row = $result->fetch_assoc()) {
        $field1name = $row["id"];
        $field2name = $row["batch"];
        $field3name = $row["date"];
         
        echo '<b>Last entry information:</b> <br><br>';
        echo '<b>Record ID: '.$field1name.'</b><br />';
        echo '<b>Date: '.$field3name.'</b><br>';
        echo '<b>Batch Code: '.$field2name.'</b><br>';
      
    }
 
/*freeresultset*/
$result->free();
}
?>

<center>
<form action="ins1.php" method="post">
<table>
    <tr>
        <th>
            <p>Batch Date Code:</p>
        </th>
        <td>
            <input type="NUMBER" name="code">
        </td>
    </tr>
    <tr>
        <th>
            <p>Date:</p>
        </th>
        <td>
            <input type="DATE" name="date">
        </td>
    </tr>
    <tr>
        <th>
            <p>Store:</p>
        </th>
        <td>
            <p>
            <select name="store">
                <option>Fred Myers Nampa</option>
                <option>CostCo Nampa</option>
                <option>Albertsons 415 Caldwell</option>
                <option>Albertsons 2500 Caldwell</option>
            </select>
            </p>
        </td>
    </tr>
    <tr>
        <th>
            <p>Estimated IC Payment:</p>
        </th>
        <td>
            <input type="number" min="1" step="any" name="esicpay"/>

        </td>
    </tr>
    <tr>
        <th>
            Estimated Tip:
        </th>
        <td>
            <input type="number" min="1" step="any" name="estippay"/>
        </td>
    </tr>
    <tr>
        <th>
            Beginning Mileage Reading:
        </th>
        <td>
            <input type="number" min="1" step="any" name="begmile"/>
        </td>
    </tr>
    <tr>
        <th>
        </th>
        <td>
            <input type="submit" value="Next">
        </td>
    </tr>
</table>
</form>
</center>
</body>
</html>

请告诉我一些简单的事情...hehehe 代码的底部还没有为新页面更新,因为它也被复制并粘贴到新文件中。谢谢你的任何建议。试图通过网络学习是一场噩梦...

您在变量中有商店连接详细信息:

$servername = "localhost";
$username = "******";
$password = "******";
$dbname = "******";

以及您检查的下方是否正确创建了连接

if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

但是您没有指定我要使用上面的连接详细信息来连接我的应用程序和数据库。

PHP 使用 mysqli

.... rest of code
// store connection details in variables
$servername = "localhost";
$username = "******";
$password = "******";
$dbname = "******";

//create connection
$conn = new mysqli($servername,$username,$password,$dbname);

// Check if connection using connection details is created successfully or not 
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}
.... rest of code