OO mysqli准备好的语句请帮忙 [英] OO mysqli prepared statements help please

查看:70
本文介绍了OO mysqli准备好的语句请帮忙的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个类,我一直在绞尽脑汁,试图弄清楚如何从用户那里获取表单信息,并使用oop预备语句将其存储在数据库中.我将介绍代码.请有人告诉我我在做错什么.

I've created a class and I've been wrecking my brain trying to figure how to obtain form info from a user and store it in a database using oop prepared statements. I'll present the code. Please someone tell me what I'm I doing wrong.

我班级的代码

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

class User {

    public $id,
    $fname,
    $lname,
    $email,
    $username,
    $password,
    $conf_pass;
    protected $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();
            exit();
        } else {
            echo 'Connected';
        }
    }

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



        if ($this->db_conn->prepare("INSERT INTO USERS (`user_id`,`user_fname`,`user_lname`,
            `user_email`,`username`,`user_pass`) VALUES (NULL,?,?,?,?,?)")) {


            //Bind-para to values from form
            $stmt->bind_param('isssss', $id, $fname, $lname, $email, $username, $password);

            //execute statement
            $stmt->execute();

            $this->id = $id;
            $this->fname = $fname;
            $this->lname = $lname;
            $this->email = $email;
            $this->username = $username;
            $this->password = $password;

            //end statement
            $stmt->close();
        }
    }

}

?>

这是我用来创建对象的代码.

    <?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);
?>

这是我的测试表单的代码.请告诉我我在做什么错.任何帮助,我们将不胜感激.

And here is the code for my test form. Please tell me what I'm doing wrong. Any help is greatly appreciated.

    <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>

推荐答案

我猜user_id是主键,并且是自动递增的.您不需要为user_id插入值,也无需将其从绑定参数中删除.

I guess user_id is the primary key and is auto increment. you dont need to insert value for user_id and remove it from bind params as well.

if ($this->db_conn->prepare("INSERT INTO USERS (`user_fname`,`user_lname`,
        `user_email`,`username`,`user_pass`) VALUES (?,?,?,?,?)"))

并将bind_param更改为

and change bind_param to

$stmt->bind_param('sssss', $fname, $lname, $email, $username, $password);

这篇关于OO mysqli准备好的语句请帮忙的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆