Jump to content

Call to a member function prepare() on null


phreak3r

Recommended Posts

Excerpts of code:

        function addUser() {
	    $username = $_POST['username'];
	    $password = password_hash($_POST['password'], PASSWORD_DEFAULT);
	    $bio = $_POST['bio'];
	    $email = $_POST['email'];
	    $c_status = 0;
            //$avatar = 

	    //$username_query = $pdo->prepare("SELECT * from profiles001 WHERE username=':username'");
            //$username_query->bindValue(':username', $username);
            //$username_query->execute();

            $query = $pdo->prepare("INSERT into profiles001 (username, password, email, c_status, bio) VALUES (:username, :password, :email, :cstat, :bio)");

            $query->bindValue(':username', $username);
            $query->bindValue(':password', $password);
            $query->bindValue(':email', $email);
            $query->bindValue(':cstat', $c_status);
            $query->bindValue(':bio', $bio);

            $query->execute();

            setAvatar();
        }

        function setAvatar() {
            // check if avatar is set, if not give default avatar
            if (isset($file) && $fileError === UPLOAD_ERR_OK) {
                $file = $_FILES['userfile'];
                $fileName = $file['name'];
                $fileTmpName = $file['tmp_name'];
                $fileSize = $file['size'];
                $fileError = $file['error'];
                $fileType = $file['type'];
                $fileExt = explode('.', $fileName);
                $fileActualExt = strtolower(end($fileExt));

                $allowedExtensions = array('jpg', 'jpeg', 'png');
            }

           

            // if user has not assigned avatar, assign the default.
            if (empty($file)) {
                $avatar = "assets/soap.jpg";
                
                $query = $pdo->prepare("INSERT INTO profiles001 (avatar) VALUES (:avatar)");

                $query->bindValue(':avatar', $avatar);
                $query->execute();
            }
        }

       addUser(); 
}

From the database file:

<?php
$host   = "localhost";
$database = "soapbox";
$username = "drb";
$password = "m1n3craft";

// Create connection
 		$pdo = new PDO('mysql:host=localhost;dbname=soapbox;', $username, $password);

/*

Print error message and or code to the screen if there is an error.

*/

?>

NOTE: I also require dbcon.php at the top of the confirmation.php file which is NOT included in the excerpt at the top.

Making pdo a global variable would probably fix it, but from what I heard globals are frowned upon.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.