1

I've written a .php page for submitting a log in form. It scans the database to check any matches, however, i stopped working on my website about 2 weeks ago and i've come to work on it again, however it's now decided to stop working? I get this error:

Fatal error: Call to a member function prepare() on a non-object in C:\xampp\htdocs\loginsubmit.php on line 16

Here's my PHP for the loginsubmit

<font face="ClearSans-Thin">
<font color="lightgray">

<?php

include 'loginform.php';
include 'connection.php';

if (isset($_POST['submit'])) {
    $user = $_POST['username'];
    global $db;
    $pass = $_POST['password'];

    $query = "SELECT COUNT(*) AS cnt FROM Users WHERE Username='" . mysqli_real_escape_string($connection,$user) . "' && Password='" . mysqli_real_escape_string($connection,$pass). "'";
    $query = "INSERT INTO Users (FirstName, LastName, Email, DateofBirth, Username, Password) VALUES (?, ?, ?, ?, ?, ?)";
    $stmt = $db->prepare($query);
$stmt->bind_param("ssssss", firstname, $lastname, $email, $dob, $user, $pass);
$stmt->execute();
$stmt->close();
    $result = mysqli_query($connection, $query) or die(mysqli_error($connection));
    $row = mysqli_fetch_assoc($result);
    if ($row["cnt"] > 0) {
        session_start();
        $_SESSION['login'] = "1";

        header('Location: homepage.php');
    } else {
        header('Location: loginform.php');

    }
}
15
  • 2
    $db (your global) isn't an object =) Commented Dec 12, 2014 at 16:51
  • 2
    possible duplicate of Call to a member function prepare() on a non-object PHP Help Commented Dec 12, 2014 at 16:52
  • 1
    You're trying to do a multi query, that's why it's failing. Commented Dec 12, 2014 at 16:57
  • 2
    Your'e using two different type of connections, $dbo (PDO object) and $connection(a handle from mysqli_connect()). Also, the first query is never executed. I will look like all is well because the mysqli_query() function is executing successfully. You should really just choose one and stick to it. This looks quite messy Commented Dec 12, 2014 at 17:03
  • 1
    @ksealey I'd say that's worthy of an answer. I'd be glad to upvote. Commented Dec 12, 2014 at 17:10

1 Answer 1

3

"Your'e using two different type of connections, $dbo (PDO object) and $connection(a handle from mysqli_connect()). Also, the first query is never executed. I will look like all is well because the mysqli_query() function is executing successfully. You should really just choose one and stick to it. This looks quite messy"

To continue, I would assume that your connection.php file contains your mysqli_connect() so I'll take that route. You would want to remove that global variable and any references to the statement and PDO objects, clean up the values you will be inserting and set them in your query string, replacing the '?'s.

Sign up to request clarification or add additional context in comments.

1 Comment

That you are sir, that you are. haha.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.