为什么我不能在PHP中使用OOP将数据插入数据库? [等候接听] [英] Why Can't I Insert Data Into Database Using OOPs in PHP? [on hold]

查看:54
本文介绍了为什么我不能在PHP中使用OOP将数据插入数据库? [等候接听]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试使用PHP中的OOP从php表单插入数据。

我收到错误 - >



< pre lang =text>您的SQL语法有错误;检查与MySQL服务器版本对应的手册,以便在用户名,电子邮件,密码附近使用正确的语法)VALUES(Bob','bob @ example.com','$ 1 $ RL4.GJ4 。$ FX4D'在第1行





我在这方面失败,因为我是初学者用php。

这是代码,用于处理php register.class.php中的数据库类 - >



 <?php  
class DataBase {
public $连接;
private $ hostName ;
private $ userName ;
private $ pasWord ;
private $ db ;

public function connect($ host,$ user,$ pass,$ dtb){
$ this-> hostName = $ host ;
$ this-> userName = $ user ;
$ this-> password = $ pass ;
$ this-> db = $ dtb ;

return $ this-> connection = mysqli_connect($ host,$ user,$ pass,$ dtb)
die(' 无法连接。') ;
}

公共函数插入($ fields,$ data,$ table){
try {
$ queryFields = implode( ',',$ fields);
$ queryValues = implode( ', ',$ data);

$ insert = ' INSERT INTO'。$ table。' ('。$ queryFields。' )VALUES('。$ queryValues。' )';

if (mysqli_query($ this-> connection,$ insert)){
返回 true;
} else {
die(mysqli_error($ this-> connection));
}
} catch (例外$ ex){
echo 某些例外情况 $ ex ;
}
}
}
?>





这是表格的代码register.php - >



 <?php  
包括' register.class.php'< /跨度>;

$ con = new DataBase();

$ con> connect(' localhost'' root'' '' test');

if (isset($ _ POST [' submit'])){
$ uName = $ _POST [' userName'];
$ email = $ _POST [' email ];

$ pass = $ _POST [' 密码];
$ passHash = crypt($ pass);

$ fields = array(' 用户名'' email'' password');
$ values = array($ uName,$ email,$ passHash);

$ res = $ con> insert($ fields,$ values,' users');

if ($ res) echo 1记录已插入;
}
?>





< pre lang =HTML> < 表格 action = register.php 方法 = POST enctype = multipart / form-data >
用户名:< span class =code-keyword>< input type = text name = userName 必需 >
< br / >
电子邮件:< 输入 type = email 名称 = email 必需 >
< br / >
密码:< input type = passWord name < span class =code-keyword> = passWord 必需 >
< br / >
< input type = 提交 名称 = 提交 value = 注册 >
< / form >

解决方案

1


RL4.GJ4。


FX4D'在第1行





由于我不熟悉使用php,所以很难实现这一点。

这是代码,用于处理php register.class.php中的数据库类 - >



 <?php  
class DataBase {
public

Am trying to insert data from a php form using OOPs in PHP.
Am getting the error that ->

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '"username','email','password") VALUES ("Bob','bob@example.com','$1$RL4.GJ4.$FX4D' at line 1



Am failing in this as i am new to oops with php.
Here's the code for that handles the class of database in php register.class.php ->

<?php
class DataBase {
    public $connection;
    private $hostName;
    private $userName;
    private $pasWord;
    private $db;

    public function connect($host, $user, $pass, $dtb) {
        $this->hostName = $host;
        $this->userName = $user;
        $this->password = $pass;
        $this->db   = $dtb;

        return $this->connection = mysqli_connect($host, $user, $pass, $dtb)
                                    or die('Could Not Connect.');
    }

    public function insert($fields, $data, $table) {
        try {
            $queryFields = implode("','", $fields);
            $queryValues = implode("','", $data);

            $insert = 'INSERT INTO '.$table.'("'.$queryFields.'") VALUES ("'.$queryValues.'")';

            if (mysqli_query($this->connection, $insert)) {
                return true;
            } else {
                die(mysqli_error($this->connection));
            }
        } catch (Exception $ex) {
            echo "Some Exception Occured " . $ex;
        }
    }
}
?>



Here's the code of the form register.php ->

<?php
        include 'register.class.php';

        $con = new DataBase();

        $con->connect('localhost', 'root', '', 'test');

        if (isset($_POST['submit'])) {
            $uName = $_POST['userName'];
            $email = $_POST['email'];

            $pass = $_POST['passWord'];
            $passHash = crypt($pass);

            $fields = array('username', 'email', 'password');
            $values = array($uName, $email, $passHash);

            $res = $con->insert($fields, $values, 'users');

            if ($res) echo "1 Record Inserted";
        }
    ?>



<form action="register.php" method="POST" enctype="multipart/form-data">
    Username: <input type="text" name="userName" required>
    <br />
    Email: <input type="email" name="email" required>
    <br />
    Password: <input type="passWord" name="passWord" required>
    <br />
    <input type="submit" name="submit" value="Register">
</form>

解决方案

1


RL4.GJ4.


FX4D' at line 1



Am failing in this as i am new to oops with php.
Here's the code for that handles the class of database in php register.class.php ->

<?php
class DataBase {
    public 


这篇关于为什么我不能在PHP中使用OOP将数据插入数据库? [等候接听]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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