Home > Back-end >  Inserting data into database error: Call to a member function query() on null
Inserting data into database error: Call to a member function query() on null

Time:08-28

I am having difficulty inserting into a database from the following html and php

Here is the html:

<body>
<form action="insert.php" method="post">
<div id="sgc">
    <div id="logo">
        Student Grade Checker App
    </div>
    <div>
        <textarea  id="input-text" rows="5" cols="35" name="input_text" placeholder="Enter the module names and marks separated by comma [put each mod>
    </div>
    <div>
        <textarea  id="output-text" rows="5" cols="35" readonly=1 placeholder="Results here..." value="">
        </textarea>
    </div>
    <div>
        <button  name="total_marks" onclick="verify();getTotal();">Total Marks</button>
    </div>
    <div>
        <button  onclick="verify();getMaxMin();">Highest & Lowest Scoring Modules</button>
    </div>
    <div>
        <button  onclick="verify();getSortedModules();">Sort Modules</button>
    </div>
    <div>
        <button  onclick="verify();getClassification();">Classify Grade</button>
    </div>
    <div>
        <button  onclick="verify();getAverage();">Overall Average</button>
    </div>
    <div>
        <button  onclick="verify();getIndividualClassification();">Individual Module Classification</button>
    </div>
    <div>
        <button  onclick="clearText();">Clear</button>
    </div>
</div>
</form>
</body>

The php code:

<?php

if (isset($_POST['total_marks'])){
        $totalMarks = $_POST['total_marks'];}


$insertsql = "INSERT INTO total_marks (Module) VALUES('$totalMarks')";

$result = $conn->query($insertsql);

if (!$result) {

echo $conn->error;
}
?>

I am only working with the first button to get it working i will want to eventually insert the others too.

I am getting this error:

Fatal error: Uncaught Error: Call to a member function query() on null in /var/www/html/insert.php:9 Stack trace: #0 {main} thrown in /var/www/html/insert.php on line 9

My database connection is successful.

Any help is always appricated.

CodePudding user response:

if I'm not mistake add to button type="submit"

<button type="submit"  name="total_marks" onclick="verify();getTotal();">Total Marks</button>

CodePudding user response:

What you want to do now requires a button with the name total_marks and type submit all the code should look like this

<?php
if (isset($_POST['total_marks'])){
    $totalMarks = $_POST['total_marks'];}
$insertsql = "INSERT INTO total_marks (Module) VALUES('$totalMarks')";

$result = $conn->query($insertsql);

if (!$result) {

    echo $conn->error;
}
?>

<body>
<form action="insert.php" method="post">
<div id="sgc">
    <div id="logo">
        Student Grade Checker App
    </div>
    <div>
        <textarea  id="input-text" rows="5" cols="35" name="input_text" placeholder="Enter the module names and marks separated by comma [put each mod>
    </div>
<div>
        <textarea  id="output-text" rows="5" cols="35" readonly=1 placeholder="Results here..." value="">
    </textarea>
    </div>
    <div>
        <button  name="total_marks" onclick="verify();getTotal();">Total Marks</button>
    </div>
    <div>
        <button  onclick="verify();getMaxMin();">Highest & Lowest Scoring Modules</button>
    </div>
    <div>
        <button  onclick="verify();getSortedModules();">Sort Modules</button>
    </div>
    <div>
        <button  onclick="verify();getClassification();">Classify Grade</button>
    </div>
    <div>
        <button  onclick="verify();getAverage();">Overall Average</button>
    </div>
    <div>
        <button  onclick="verify();getIndividualClassification();">Individual Module Classification</button>
    </div>
    <div>
        <button  type="submit" name="total_marks" onclick="clearText();">Clear</button>
</div>
</div>
</form>
</body>
  • Related