0

So yes I freely admit that I am a total noob when it comes to doing web dev beyond basic html code. So hopefully everyone can help, here is my problem:

I am trying to create a page where I display the last record in a certain database so that I can update it with more information after the record has been created. (The displayed information is just so that I update the correct record.) I keep getting the same error message when loading the site:

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

Here is my conumdrum: I used the same code to pull the information from the database on a seperate page, in which it worked just as expected. All I did is copy and paste that portion of the code from that page into the one that I am now working on. I just update the database name etc. Am I missing something? The Code from the complete page is:

<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>

Please tell me is something simple...hehehe The bottom of the code hasnt been update for the new page as it is also copied and pasted into the new file. Thank you for any advice. Trying to learn off the web is a nightmare...

Community
  • 1
  • 1
  • Where's the `WHERE 1` coming from? That's completely extraneous and can be omitted. – tadman Apr 21 '20 at 02:30
  • Hint: You never connect here, you just assume `$conn` springs into being out of nothingness. – tadman Apr 21 '20 at 02:31
  • If you're just getting started with PHP and want to build applications, I'd strongly recommend looking at various [development frameworks](https://www.cloudways.com/blog/best-php-frameworks/) to see if you can find one that fits your style and needs. They come in various flavors from lightweight like [Fat-Free Framework](https://fatfreeframework.com/) to far more comprehensive like [Laravel](http://laravel.com/). These give you concrete examples to work from and guidance on how to write your code and organize your project's files. – tadman Apr 21 '20 at 02:31

1 Answers1

0

You have store connection details in variable:

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

and below that you have check is connection created properly

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

But you haven't specify that I want to use above connection details to connect my application with the database.

PHP use mysqli for that

.... 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
Paritosh Mahale
  • 1,238
  • 2
  • 14
  • 42
  • Please read: [Should we ever check for mysqli_connect() errors manually?](https://stackoverflow.com/q/58808332/1839439) – Dharman Apr 21 '20 at 23:55