Jump to content

MySqli prepared Statements inside custom class


gork4life

Recommended Posts

I've searched all over for the past few days trying to figure out what I'm doing wrong. Basically what I'm trying to do is create a prepared statement inside my User class. I can connect to the database, but my query does not execute as expected.

 

Here's the code for my User class

<?php include '../includes/Constants.php'; ?>
<?php

/**
* Description of User
*
* @author Eric Evas
*/
class User {

    var $id,
    $fname,
    $lname,
    $email,
    $username,
    $password,
    $conf_pass;
    protected static $db_conn;

    //declare variables
    public function __construct() {
        $host = DB_HOST;
        $user = DB_USER;
        $pass = DB_PASS;
        $db = DB_NAME;

        //Connect to database
        $this->db_conn = new mysqli($host, $user, $pass, $db);

        //Check database connection
        if ($this->db_conn->connect_error) {
            echo 'Connection Fail: ' . mysqli_connect_error();
        } else {
            echo 'Connected';
        }
    }

    function regUser($fname, $lname, $email, $username, $password, $conf_pass) {


        if ($stmt = $this->db_conn->prepare("INSERT INTO USERS (user_fname,user_lname,
            user_email,username,user_pass) VALUES (?,?,?,?,?)")) {
            $stmt->bind_param('sssss', $this->fname, $this->lname, $this->email, $this->username, $this->password);

            $stmt->execute();

            $stmt->store_result();

            $stmt->close();
        }
    }

}

?>

 

And here's the file that I created to instantiate an instance of the user class.

 

<?php include_once 'User.php'; ?>
<?php

//Creating new User Object
$newUser = new User();

$newUser->fname = $_POST['fname'];
$newUser->lname = $_POST['lname'];
$newUser->email = $_POST['email'];
$newUser->username = $_POST['username'];
$newUser->password = $_POST['password'];
$newUser->conf_pass = $_POST['conf_pass'];


$newUser->regUser($newUser->fname, $newUser->lname, $newUser->email,
        $newUser->username, $newUser->password, $newUser->conf_pass);
?>

 

 

And lastly heres the form that I want to get info from the user to insert into the database

 

<html>
    <head>
        <title></title>
        <link href="stylesheets/styles.css" rel="stylesheet" type="text/css"/>
    </head>


    <body>
        <form action = "Resources/testClass.php" method="post" enctype="multipart/form-data">
            <label>First Name: </label>
            <input type="text" name="fname" id="fname" size="25" maxlength="25"/>
            <label>Last Name: </label>
            <input type="text" name="lname" id="lname" size="25" maxlength="25"/>
            <label>Email: </label>
            <input type="text" name="email" id="email" size="25" maxlength="40"/>
            <label>Username: </label>
            <input type="text" name="username" id="username" size="25" maxlength="32"/>
            <label>Password: </label>
            <input type="password" name="password" id="password" size="25" maxlength="32"/>
            <label>Re-enter Password: </label>
            <input type="password" name="conf_pass" id="conf_pass" size="25" maxlength="32"/>
            <br /><br />
            <input type="submit" name="submit" id="submit" value="Register"/>
            <input type="reset" name="reset" id="reset" value="Reset"/>
        </form>

    </body>
</html>

Link to comment
Share on other sites

Variables must be set as class properties before trying to access them as class properties.  Since your variables are passed into the function as arguments, then they must be accessed inside the function as function variables.  In other words lose the $this->, or declare them as properties with $this->.

 

In your second script you are assigning the variables as class properties inside your script.  If you do that, there is no need to pass them as function arguments, and you can forget the rules above.  In this script your flow will read as: assign these variables to the class as properties, then retrieve the same properties from the class, and pass them to a function that resides in that same class.  Or, as I like to say, leave by the front door, and run around the neighborhood to get to the back door.

 

So, do this:

function regUser($fname, $lname, $email, $username, $password, $conf_pass) {


        if ($stmt = $this->db_conn->prepare("INSERT INTO USERS (user_fname,user_lname,
            user_email,username,user_pass) VALUES (?,?,?,?,?)")) {
            $stmt->bind_param('sssss', $fname, $lname, $email, $username, $password);

            $stmt->execute();

            $stmt->store_result();

            $stmt->close();
        }
    }

 

AND

//Creating new User Object
$newUser = new User();

$fname = $_POST['fname'];
$lname = $_POST['lname'];
$email = $_POST['email'];
$username = $_POST['username'];
$password = $_POST['password'];
$conf_pass = $_POST['conf_pass'];


$newUser->regUser($fname, $lname, $email,
        $username, $password, $conf_pass);
?>

 

 

If this doesn't produce the results you desire, it is time to find the database errors.  For this you will want to look at: mysqli::error()

Link to comment
Share on other sites

Thanks I did what you said and it still just connected to the database with any errors. But It's not inserting what I type into the form.

Any ideas why is that.

 

User class

 

<?php include '../includes/Constants.php'; ?>
<?php

/**
* Description of User
*
* @author Eric Evas
*/
class User {

    var $id,
    $fname,
    $lname,
    $email,
    $username,
    $password,
    $conf_pass;
    protected static $db_conn;

    //declare variables
    public function __construct() {
        $host = DB_HOST;
        $user = DB_USER;
        $pass = DB_PASS;
        $db = DB_NAME;

        //Connect to database
        $this->db_conn = new mysqli($host, $user, $pass, $db);

        //Check database connection
        if ($this->db_conn->connect_error) {
            echo 'Connection Fail: ' . mysqli_connect_error();
        } else {
            echo 'Connected';
        }
    }

    function regUser($fname, $lname, $email, $username, $password, $conf_pass) {


        if ($stmt = $this->db_conn->prepare("INSERT INTO USERS (user_fname,user_lname,
            user_email,username,user_pass) VALUES (?,?,?,?,?)")) {
            $stmt->bind_param('sssss', $fname, $lname, $email, $username, $password);

            $stmt->execute();

            $stmt->store_result();

            $stmt->close();
        }
    }

}

?>

 

Instance file modified.

 

<?php include_once 'User.php'; ?>
<?php

//Creating new User Object
$newUser = new User();

$fname = $_POST['fname'];
$lname = $_POST['lname'];
$email = $_POST['email'];
$username = $_POST['username'];
$password = $_POST['password'];
$conf_pass = $_POST['conf_pass'];


$newUser->regUser($fname, $lname, $email,username, $password, $conf_pass);
?>

Link to comment
Share on other sites

Try this:

 

<?php
error_reporting(-1);
ini_set('display_errors',1);
include_once 'User.php'; 

//Creating new User Object
$newUser = new User();

$fname = $_POST['fname'];
$lname = $_POST['lname'];
$email = $_POST['email'];
$username = $_POST['username'];
$password = $_POST['password'];
$conf_pass = $_POST['conf_pass'];


$newUser->regUser($fname, $lname, $email,username, $password, $conf_pass);
?>

 

And,

 

<?php include '../includes/Constants.php'; ?>
<?php

/**
* Description of User
*
* @author Eric Evas
*/
class User {

    var $id,
    $fname,
    $lname,
    $email,
    $username,
    $password,
    $conf_pass;
    protected static $db_conn;

    //declare variables
    public function __construct() {
        $host = DB_HOST;
        $user = DB_USER;
        $pass = DB_PASS;
        $db = DB_NAME;

        //Connect to database
        $this->db_conn = new mysqli($host, $user, $pass, $db);

        //Check database connection
        if ($this->db_conn->connect_error) {
            echo 'Connection Fail: ' . mysqli_connect_error();
        } else {
            echo 'Connected';
        }
    }

    function regUser($fname, $lname, $email, $username, $password, $conf_pass) {


        if ($stmt = $this->db_conn->prepare("INSERT INTO USERS (user_fname,user_lname,
            user_email,username,user_pass) VALUES (?,?,?,?,?)")) {
            $stmt->bind_param('sssss', $fname, $lname, $email, $username, $password);

            if(!$stmt->execute()) {
               printf("Error: %s.\n", $stmt->error);
            } elseif($stmt->affected_rows > 0) {
               printf('Your query was successful!');
            }
            //$stmt->store_result(); //there is nothing to store here.

            $stmt->close();
        }
    }

}

?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.