phpmysqlsqlapachehtdocs

PHP - LocalHost Database Simple Connection SQL


I do not understand why something so simple is so hard.

Now when I hit submit I get the error:

Notice: Undefined variable: conn in C:\xampp\htdocs\DataHandling.php on line 6

Fatal error: Call to a member function prepare() on null in C:\xampp\htdocs\DataHandling.php on line 6

My form works, code:

<html>
<head>
    <title>Gym Form</title>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<form action="DataHandling.php" method="post">

    <span>Gym Membership Registration</span><br><br>
    <Span>Title: </Span><input type ="text" Value =" " name ="Title" /><br>
    <Span>First Name: </Span><input type ="text" Value =" " name ="Fname" /><br>
    <Span>Last Name: </Span><input type ="text" Value =" " name ="Lname" /><br><br>
    <Span>Gender: </Span><select name ="Gender">
        <option value ="Junior">Male</option>
        <option value ="Adult">Female</option>
        <option value ="Senior">Private</option>
    </select><br>

    <Span>DOB: </Span><input type ="date" name ="DOB" /><br><br>
    <Span>MembershipExpiry: </Span> <input type ="date" name ="MemX" /><br>
    <Span>MembershipType: </Span><select name = "MemType">
        <option value ="Junior">Junior</option>
        <option value ="Adult">Adult</option>
        <option value ="Senior">Senior</option>
    </select><br><br>
    <Span>Email Address: </Span><input type ="email" name ="Email" /><br><br>

    <input type="Submit" name="submit" value ="Submit Form">

I then get a nice message telling me the connection to the database is confirmed, conn.php:

<?php


$hostname = 'localhost';
$username = 'root';
$password = '';
$dbName = 'gym';

try
{
    //Attempt connection passing in predefined connection variables.
    $conn = new PDO("mysql:host=$hostname;dbname=$dbName", $username, $password);
    echo ("Connected to Database Successfully. Welcome ".$username);
}
catch(PDOException $e)
{
    //Use exception E to return PDO/MySQL specific error messages
    echo $sql . "<br>" . $e->getMessage();
}



    </body>
</form>
</html>

?>

However I am having an absolutely horrible time getting the data entered from the form to the prepared database.

I have tried Sqli and now tried something else.

<?php


    //Prepare HTML insert statement binding parameters
    $stmt = $conn->prepare("INSERT INTO records (Title,Fname,Lname,Gender,DOB,MemX,MemType,Email) 
    VALUES ('$title', '$fname', '$lname', '$gender', '$dob', '$memx', '$memtype', '$email')");

        $stmt ->bindParam(':Title', $title);
        $stmt ->bindParam(':Fname', $fname);
        $stmt ->bindParam(':Lname', $lname);
        $stmt ->bindParam(':Gender', $gender);
        $stmt ->bindParam(':DOB', $dob);
        $stmt ->bindParam(':MemX', $memx);
        $stmt ->bindParam(':MemType', $memtype);
        $stmt ->bindParam(':Email', $email);

    //Attempt row insertion by executing prepared statement
    try
    {
        //Insert a row

        $title = $_POST['Title'];
        $fname = $_POST['Fname'];
        $lname = $_POST['Lname'];
        $gender = $_POST['Gender'];
        $dob = $_POST['DOB'];
        $memx = $_POST['MemX'];
        $memtype = $_POST['MemType'];
        $email = $_POST['Email'];

        $stmt->execute();
    }
    catch (PDOException $e)
    {
        echo $e->getMessage();
    }

    //Close Connection
    $conn = null;

?>

Solution

  • If you are including conn.php on top of DataHandling.php nothing will work since you're setting

    $conn = null at the end of conn.php.

    conn.php

    <?php
    
    $hostname = 'localhost';
    $username = 'root';
    $password = '';
    $dbName = 'gym';
    
    $conn = null;
    try
    {
        //Attempt connection passing in predefined connection variables.
        $conn = new PDO("mysql:host=$hostname;dbname=$dbName", $username, $password);
    }
    catch(PDOException $e)
    {
        //Use exception E to return PDO/MySQL specific error messages
        echo $sql . "<br>" . $e->getMessage();
    }
    
    ?>
    

    DataHandling.php Your prepared statement is also wrong.

    <?php
    require_once 'conn.php';
    
    //Prepare HTML insert statement binding parameters
    $stmt = $conn->prepare("INSERT INTO records (Title,Fname,Lname,Gender,DOB,MemX,MemType,Email) 
    VALUES (:Title, :Fname, :Lname, :Gender, :DOB, :MemX, :MemType, :Email)");
    
    $title = $_POST['Title'];
    $fname = $_POST['Fname'];
    $lname = $_POST['Lname'];
    $gender = $_POST['Gender'];
    $dob = $_POST['DOB'];
    $memx = $_POST['MemX'];
    $memtype = $_POST['MemType'];
    $email = $_POST['Email'];
    
    //Attempt row insertion by executing prepared statement
    try
    {
        //Insert a row
        $stmt->bindParam(':Title', $title);
        $stmt->bindParam(':Fname', $fname);
        $stmt->bindParam(':Lname', $lname);
        $stmt->bindParam(':Gender', $gender);
        $stmt->bindParam(':DOB', $dob);
        $stmt->bindParam(':MemX', $memx);
        $stmt->bindParam(':MemType', $memtype);
        $stmt->bindParam(':Email', $email);
    
        $stmt->execute();
    }
    catch (PDOException $e)
    {
        echo $e->getMessage();
    }
    
    //Close Connection
    $conn = null;
    
    ?>