首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >这是在oop php中使用预准备insert语句的正确方式吗?

这是在oop php中使用预准备insert语句的正确方式吗?
EN

Stack Overflow用户
提问于 2016-07-08 06:41:30
回答 1查看 316关注 0票数 2

大家好,我已经写了这个注册页面脚本,我想将用户信息注册到数据库中,但我试图通过使用带有oop语法的准备好的insert语句来安全地做到这一点,但我不确定我这样做是否正确,因为当我注册虚拟数据时,它不会在数据库中放入任何东西。

index.php页面

代码语言:javascript
复制
    <!DOCTYPE html>
    <html>
    <head>
    <title>Registration form</title>
    <link rel="stylesheet" type="text/css" href="regisform.css">
    </head>
    <body>
        <div id="form">
        <div id="header"><h2>Registration Form</h2></div>
            <form method="post" action="process.php">
                <label>Username:</label>
                <input type="text" name="username" placeholder="Enter a Username please" required="required" />
                <label>Email:</label>
                <input type="text" name="email" placeholder="Enter your email please" required="required" />
                <label>Password:</label>
                <input type="text" name="password" placeholder="Enter a Password please" required="required" />
                <input type="submit" name="signup" value="Sign up"/>
            </form>
        </div>
    </body>

    </html>

    <?php 
    include "process.php";
       $db = new db();
    if(isset($_POST['signup'])) {
       $user = $_POST['username'];
       $email = $_POST['email'];
       $password = $_POST['password'];

       $query = "INSERT INTO users (user_name, user_email, user_pass) VALUES (?, ?, ?)";

       $run = $db->insert($query);
       $run->bind_param('sss', $user, $email, $password);
       $run->execute();
       $run->close();
    }
?>

process.php页面

代码语言:javascript
复制
<?php

class db {
    public $host = "localhost";
    public $user = "root";
    public $pass = "";
    public $db_name = "pros";

    public $link;

    public function __construct(){
        $this->connect();
    }

    private function connect() {
        $this->link = new mysqli($this->host, $this->user, $this->pass, $this->db_name);
    }

    public function insert ($query) {
        $result = $this->link->prepare($query);
        if($result){
            echo "<center><h2>Registration Successfull!</h2></center>";
        }
        else
        {
            echo "<center><h2>Registration failed!</h2></center>";
        }
      return $result;
    }
}

?>
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/38256386

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档