gpt4 book ai didi

php - 使用 PDO/PHP 注册不起作用

转载 作者:行者123 更新时间:2023-11-29 13:20:18 24 4
gpt4 key购买 nike

我在构建注册系统时遇到了问题。我不知道问题出在哪里,因为我是 PHP OOP 的初学者。

这是我的注册类:

<?php
/*
*=======================
* REGISTRATION CLASS !!!
* ======================
*/
include_once '/../core/registration.php';
include_once '/../core/db_connect.php';

class registration {

private $db;

function __construct() { /* Connecting with MySQL */
$this->db = new db_connect();
$this->db = $this->db->connect();
} /* End of connection */

function registration($username, $password1, $password2, $email, $date) {
if(!empty($username) && !empty($password1) && !empty($password2) && !empty($email)) {

$q = $this->db->prepare("INSERT INTO users(username, password, email, date, logged, admin) VALUES (?, ?, ?, ?, ?, ?)");
$q->bindParam(1, $username);
$q->bindParam(2, $password1);
$q->bindParam(3, $email);
$q->bindParam(4, $date);
$q->bindParam(5, '0');
$q->bindParam(6, '0');
$q->execute();

}
}

}
?>

这是一个文件,当用户单击“注册”按钮时正在运行:

<?php
include_once '/../classes/registration.php';

if(isset($_POST['regUser'])) {

$username = $_POST['regUsername'];
$pass1 = $_POST['regPassword1'];
$pass2 = $_POST['regPassword2'];
$email = $_POST['regEmail'];
$date = date("Y-m-d H:i:s");

$obj = new registration();
$obj->registration($username, $password1, $password2, $email, $date);

}
?>

为了安全起见,我还会放置一个连接文件:

<?php
class db_connect {

function connect() {
return new PDO("mysql:host=127.0.0.1;dbname=oop", "root", "");
}

}
?>

最佳答案

这是 PDO 最常见的“错误”之一。

使用bindParam,您只能传递变量,而不能传递值。

在您的 Registration() 方法中,替换:

$q->bindParam(5, '0');
$q->bindParam(6, '0');

作者:

$q->bindValue(5, '0');
$q->bindValue(6, '0');

使用bindValue,您可以传递值和变量。

您可以阅读 PHP 手册以获取更多信息( herehere )。

关于php - 使用 PDO/PHP 注册不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20934364/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com