gpt4 book ai didi

php - PDO bind_param 是未定义的方法

转载 作者:可可西里 更新时间:2023-11-01 07:48:13 25 4
gpt4 key购买 nike

<分区>

我正在远离 mysql 和 mysqli,因为 stackoverflow 上的许多用户一直在说它的好话。

我制作了一个数据库类并对其进行了测试,它可以很好地连接到数据库。我试图更新我准备好的陈述以匹配但是我在不熟悉的领域并最终收到以下错误:

fatal error :在第 50 行调用 E:\xampp\htdocs\imanage\insert.php 中未定义的方法 PDOStatement::bind_param()

反射(reflect)了这一行:

$stmt->bind_param("s", $_POST['email']);

此外,关于这一点,我得到了返回给我的数据库连接成功和关闭语句以及 fatal error ,例如:

成功连接数据库!成功连接数据库!成功断开数据库连接!

我将解释我要实现的目标:

  • 在注册用户之前检查数据库中是否存在电子邮件
  • 如果是,告诉用户此电子邮件存在
  • 如果不匹配则将用户插入用户表并加密密码

相关代码如下,如果有人能给我一些指导,我将不胜感激。

索引.php

        <form id="loginForm" method="POST" action="class.Login.php">
<input type="text" id="email" name="email" placeholder="E-mail">
<input type="password" id="password" name="password" placeholder="Password" class="showpassword">
<input type="submit" name="submit" value="Log in"></form>

插入.php

public function insert() {

$stmt = $this->pdo->prepare("SELECT COUNT(*) FROM users WHERE email=?");
$stmt->bind_param("s", $_POST['email']);
$stmt->execute();
$stmt->bind_result($email_count);
$stmt->fetch();//fecth
$stmt->close();

if ($email_count > 0) {
echo "email exisits! click here to try <a href='register'>again</a>";
} else {
//escape the POST data for added protection
$username = isset($_POST['username']) ? $_POST['username'] : null;
$cryptedPassword = crypt($_POST['password']);
$password = $cryptedPassword;
$name = isset($_POST['name']) ? $_POST['name'] : null;
$email = isset($_POST['email']) ? $_POST['email'] : null;
$stmta = $this->pdo->prepare("INSERT INTO users (username, password, name, email) VALUES (?, ?, ?, ?)");
//var_dump($this->pdo->error);
$stmta->bind_param('ssss', $username, $password, $name, $email); // bind strings to the paramater

/* execute prepared statement */
$stmta->execute();
printf("%d Row inserted.\n", $stmta->affected_rows);
/* close statement and connection */
$stmta->close();
} // end email_count and insert to table
} // end function

连接/class.Database.php

<?php

// Database connection PDO

class Database {

public function __construct() {
// Connection information
$host = 'localhost';
$dbname = 'imanage';
$user = 'root';
$pass = '';

// Attempt DB connection
try
{
$this->pdo = new PDO("mysql:host=$host;dbname=$dbname", $user, $pass);
$this->pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
echo 'Successfully connected to the database!';
}
catch(PDOException $e)
{
echo $e->getMessage();
}

}

public function __destruct()
{
// Disconnect from DB
$this->pdo = null;
echo 'Successfully disconnected from the database!';
}


}

$run = new Database();
?>

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