Jump to content
phreak3r

Call to a member function prepare() on null

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.

Share this post


Link to post
Share on other sites

$pdo isn't defined within the function. Pass it as an argument.


 

function AddUser($pdo) {
  // code here
}

 

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×

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.