Php 用户数据更新导致PDO异常
它不更新数据,它一直给我一个错误。如果电子邮件已经存在,它应该告诉我电子邮件存在,但它不能更新用户数据 它给了我这个错误: 警告:PDOStatement::execute():SQLSTATE[HY093]:参数编号无效:第37行的C:\xampp\htdocs\php.dev\classes\Model.php中未绑定任何参数 classed/Model.phpPhp 用户数据更新导致PDO异常,php,pdo,Php,Pdo,它不更新数据,它一直给我一个错误。如果电子邮件已经存在,它应该告诉我电子邮件存在,但它不能更新用户数据 它给了我这个错误: 警告:PDOStatement::execute():SQLSTATE[HY093]:参数编号无效:第37行的C:\xampp\htdocs\php.dev\classes\Model.php中未绑定任何参数 classed/Model.php abstract class Model { protected $dbh; protected $stmt;
abstract class Model {
protected $dbh;
protected $stmt;
public function __construct() {
$this->dbh = new PDO("mysql:host=".DB_HOST.";dbname=".DB_NAME, DB_USER, DB_PASS);
}
public function query($query) {
$this->stmt = $this->dbh->prepare($query);
}
// binds the prepare statement
public function bind($param, $value, $type = null) {
if (is_null($type)) {
switch (true) {
case is_int($value):
$type = PDO::PARAM_INT;
break;
case is_bool($value):
$type = PDO::PARAM_BOOL;
break;
case is_null($value):
$type = PDO::PARAM_NULL;
break;
default:
$type = PDO::PARAM_STR;
}
}
$this->stmt->bindValue($param, $value, $type);
}
public function execute() {
$this->stmt->execute();
}
public function resultSet() {
$this->execute();
return $this->stmt->fetchAll(PDO::FETCH_ASSOC);
}
public function lastInsertId() {
return $this->dbh->lastInsertId();
}
public function single(){
$this->execute();
return $this->stmt->fetch(PDO::FETCH_ASSOC);
}
public function emailExist() {
$this->execute();
return $this->stmt->fetch(PDO::FETCH_ASSOC);
}
}
class Users extends Controller{
protected function profile(){
if (!isset($_SESSION['is_logged_in'])) {//if user do not login they can not profile page
header('Location: '.ROOT_URL.'shares');
}
$viewmodel = new UserModel();
$this->returnView($viewmodel->profile(), true);
}
protected function register(){
if (isset($_SESSION['is_logged_in'])) {//if user do not logout they can not access register page
header('Location: '.ROOT_URL.'shares');
}
$viewmodel = new UserModel();
$this->returnView($viewmodel->register(), true);
}
protected function login(){
if (isset($_SESSION['is_logged_in'])) {//if user do not logout they can not access login page
header('Location: '.ROOT_URL.'shares');
}
$viewmodel = new UserModel();
$this->returnView($viewmodel->login(), true);
}
protected function logout(){
unset($_SESSION['is_logged_in']);
unset($_SESSION['user_data']);
session_destroy();
// Redirect
header('Location: '.ROOT_URL);
}
}
class UserModel extends Model {
public function profile() {
// Sanitize POST
$post = filter_input_array(INPUT_POST, FILTER_SANITIZE_STRING);
if($post['updateProfile']) {
#$name = $post['name'];
#$email = $post['email'];
#$id = $post['id'];
if (empty($post['name']) || empty($post['email'])) {
Messages::setMsg('Please Fill All Form Fields', 'error');
return;
}
// check if email is already taken
$this->query('SELECT * FROM users WHERE email = :email');
$this->bind(':email', $post['email']);
$row = $this->emailExist();
if ($row) {
Messages::setMsg('Email already Exist', 'error');
return;
} else {
# Update the MySQL
$this->query("UPDATE users SET name =:name, email =:email WHERE id =:id");
$this->execute();
// Verify
if($this->lastInsertId()){
Messages::setMsg('Successfull Updated', 'success');
return;
} else {
Messages::setMsg('Error while updating data', 'error');
return;
}
}
}
return;
}
}
<div class="panel panel-default">
<div class="panel-heading">
<h3 class="panel-title">Update Data</h3>
</div>
<div class="panel-body">
<form method="post" action="<?php $_SERVER['PHP_SELF']; ?>">
<div class="form-group">
<label>Name</label>
<input type="text" name="name" class="form-control" value="<?php echo $_SESSION['user_data']['name'];?>" />
</div>
<div class="form-group">
<label>Email</label>
<input type="text" name="email" class="form-control" value="<?php echo $_SESSION['user_data']['email'];?>" />
<input type="hidden" name="id" class="form-control" value="" />
</div>
<input class="btn btn-primary" name="updateProfile" type="submit" value="Submit" />
</form>
</div>
</div>
controllers/users.php
abstract class Model {
protected $dbh;
protected $stmt;
public function __construct() {
$this->dbh = new PDO("mysql:host=".DB_HOST.";dbname=".DB_NAME, DB_USER, DB_PASS);
}
public function query($query) {
$this->stmt = $this->dbh->prepare($query);
}
// binds the prepare statement
public function bind($param, $value, $type = null) {
if (is_null($type)) {
switch (true) {
case is_int($value):
$type = PDO::PARAM_INT;
break;
case is_bool($value):
$type = PDO::PARAM_BOOL;
break;
case is_null($value):
$type = PDO::PARAM_NULL;
break;
default:
$type = PDO::PARAM_STR;
}
}
$this->stmt->bindValue($param, $value, $type);
}
public function execute() {
$this->stmt->execute();
}
public function resultSet() {
$this->execute();
return $this->stmt->fetchAll(PDO::FETCH_ASSOC);
}
public function lastInsertId() {
return $this->dbh->lastInsertId();
}
public function single(){
$this->execute();
return $this->stmt->fetch(PDO::FETCH_ASSOC);
}
public function emailExist() {
$this->execute();
return $this->stmt->fetch(PDO::FETCH_ASSOC);
}
}
class Users extends Controller{
protected function profile(){
if (!isset($_SESSION['is_logged_in'])) {//if user do not login they can not profile page
header('Location: '.ROOT_URL.'shares');
}
$viewmodel = new UserModel();
$this->returnView($viewmodel->profile(), true);
}
protected function register(){
if (isset($_SESSION['is_logged_in'])) {//if user do not logout they can not access register page
header('Location: '.ROOT_URL.'shares');
}
$viewmodel = new UserModel();
$this->returnView($viewmodel->register(), true);
}
protected function login(){
if (isset($_SESSION['is_logged_in'])) {//if user do not logout they can not access login page
header('Location: '.ROOT_URL.'shares');
}
$viewmodel = new UserModel();
$this->returnView($viewmodel->login(), true);
}
protected function logout(){
unset($_SESSION['is_logged_in']);
unset($_SESSION['user_data']);
session_destroy();
// Redirect
header('Location: '.ROOT_URL);
}
}
class UserModel extends Model {
public function profile() {
// Sanitize POST
$post = filter_input_array(INPUT_POST, FILTER_SANITIZE_STRING);
if($post['updateProfile']) {
#$name = $post['name'];
#$email = $post['email'];
#$id = $post['id'];
if (empty($post['name']) || empty($post['email'])) {
Messages::setMsg('Please Fill All Form Fields', 'error');
return;
}
// check if email is already taken
$this->query('SELECT * FROM users WHERE email = :email');
$this->bind(':email', $post['email']);
$row = $this->emailExist();
if ($row) {
Messages::setMsg('Email already Exist', 'error');
return;
} else {
# Update the MySQL
$this->query("UPDATE users SET name =:name, email =:email WHERE id =:id");
$this->execute();
// Verify
if($this->lastInsertId()){
Messages::setMsg('Successfull Updated', 'success');
return;
} else {
Messages::setMsg('Error while updating data', 'error');
return;
}
}
}
return;
}
}
<div class="panel panel-default">
<div class="panel-heading">
<h3 class="panel-title">Update Data</h3>
</div>
<div class="panel-body">
<form method="post" action="<?php $_SERVER['PHP_SELF']; ?>">
<div class="form-group">
<label>Name</label>
<input type="text" name="name" class="form-control" value="<?php echo $_SESSION['user_data']['name'];?>" />
</div>
<div class="form-group">
<label>Email</label>
<input type="text" name="email" class="form-control" value="<?php echo $_SESSION['user_data']['email'];?>" />
<input type="hidden" name="id" class="form-control" value="" />
</div>
<input class="btn btn-primary" name="updateProfile" type="submit" value="Submit" />
</form>
</div>
</div>
models/user.php
abstract class Model {
protected $dbh;
protected $stmt;
public function __construct() {
$this->dbh = new PDO("mysql:host=".DB_HOST.";dbname=".DB_NAME, DB_USER, DB_PASS);
}
public function query($query) {
$this->stmt = $this->dbh->prepare($query);
}
// binds the prepare statement
public function bind($param, $value, $type = null) {
if (is_null($type)) {
switch (true) {
case is_int($value):
$type = PDO::PARAM_INT;
break;
case is_bool($value):
$type = PDO::PARAM_BOOL;
break;
case is_null($value):
$type = PDO::PARAM_NULL;
break;
default:
$type = PDO::PARAM_STR;
}
}
$this->stmt->bindValue($param, $value, $type);
}
public function execute() {
$this->stmt->execute();
}
public function resultSet() {
$this->execute();
return $this->stmt->fetchAll(PDO::FETCH_ASSOC);
}
public function lastInsertId() {
return $this->dbh->lastInsertId();
}
public function single(){
$this->execute();
return $this->stmt->fetch(PDO::FETCH_ASSOC);
}
public function emailExist() {
$this->execute();
return $this->stmt->fetch(PDO::FETCH_ASSOC);
}
}
class Users extends Controller{
protected function profile(){
if (!isset($_SESSION['is_logged_in'])) {//if user do not login they can not profile page
header('Location: '.ROOT_URL.'shares');
}
$viewmodel = new UserModel();
$this->returnView($viewmodel->profile(), true);
}
protected function register(){
if (isset($_SESSION['is_logged_in'])) {//if user do not logout they can not access register page
header('Location: '.ROOT_URL.'shares');
}
$viewmodel = new UserModel();
$this->returnView($viewmodel->register(), true);
}
protected function login(){
if (isset($_SESSION['is_logged_in'])) {//if user do not logout they can not access login page
header('Location: '.ROOT_URL.'shares');
}
$viewmodel = new UserModel();
$this->returnView($viewmodel->login(), true);
}
protected function logout(){
unset($_SESSION['is_logged_in']);
unset($_SESSION['user_data']);
session_destroy();
// Redirect
header('Location: '.ROOT_URL);
}
}
class UserModel extends Model {
public function profile() {
// Sanitize POST
$post = filter_input_array(INPUT_POST, FILTER_SANITIZE_STRING);
if($post['updateProfile']) {
#$name = $post['name'];
#$email = $post['email'];
#$id = $post['id'];
if (empty($post['name']) || empty($post['email'])) {
Messages::setMsg('Please Fill All Form Fields', 'error');
return;
}
// check if email is already taken
$this->query('SELECT * FROM users WHERE email = :email');
$this->bind(':email', $post['email']);
$row = $this->emailExist();
if ($row) {
Messages::setMsg('Email already Exist', 'error');
return;
} else {
# Update the MySQL
$this->query("UPDATE users SET name =:name, email =:email WHERE id =:id");
$this->execute();
// Verify
if($this->lastInsertId()){
Messages::setMsg('Successfull Updated', 'success');
return;
} else {
Messages::setMsg('Error while updating data', 'error');
return;
}
}
}
return;
}
}
<div class="panel panel-default">
<div class="panel-heading">
<h3 class="panel-title">Update Data</h3>
</div>
<div class="panel-body">
<form method="post" action="<?php $_SERVER['PHP_SELF']; ?>">
<div class="form-group">
<label>Name</label>
<input type="text" name="name" class="form-control" value="<?php echo $_SESSION['user_data']['name'];?>" />
</div>
<div class="form-group">
<label>Email</label>
<input type="text" name="email" class="form-control" value="<?php echo $_SESSION['user_data']['email'];?>" />
<input type="hidden" name="id" class="form-control" value="" />
</div>
<input class="btn btn-primary" name="updateProfile" type="submit" value="Submit" />
</form>
</div>
</div>
view/users/profile.php
abstract class Model {
protected $dbh;
protected $stmt;
public function __construct() {
$this->dbh = new PDO("mysql:host=".DB_HOST.";dbname=".DB_NAME, DB_USER, DB_PASS);
}
public function query($query) {
$this->stmt = $this->dbh->prepare($query);
}
// binds the prepare statement
public function bind($param, $value, $type = null) {
if (is_null($type)) {
switch (true) {
case is_int($value):
$type = PDO::PARAM_INT;
break;
case is_bool($value):
$type = PDO::PARAM_BOOL;
break;
case is_null($value):
$type = PDO::PARAM_NULL;
break;
default:
$type = PDO::PARAM_STR;
}
}
$this->stmt->bindValue($param, $value, $type);
}
public function execute() {
$this->stmt->execute();
}
public function resultSet() {
$this->execute();
return $this->stmt->fetchAll(PDO::FETCH_ASSOC);
}
public function lastInsertId() {
return $this->dbh->lastInsertId();
}
public function single(){
$this->execute();
return $this->stmt->fetch(PDO::FETCH_ASSOC);
}
public function emailExist() {
$this->execute();
return $this->stmt->fetch(PDO::FETCH_ASSOC);
}
}
class Users extends Controller{
protected function profile(){
if (!isset($_SESSION['is_logged_in'])) {//if user do not login they can not profile page
header('Location: '.ROOT_URL.'shares');
}
$viewmodel = new UserModel();
$this->returnView($viewmodel->profile(), true);
}
protected function register(){
if (isset($_SESSION['is_logged_in'])) {//if user do not logout they can not access register page
header('Location: '.ROOT_URL.'shares');
}
$viewmodel = new UserModel();
$this->returnView($viewmodel->register(), true);
}
protected function login(){
if (isset($_SESSION['is_logged_in'])) {//if user do not logout they can not access login page
header('Location: '.ROOT_URL.'shares');
}
$viewmodel = new UserModel();
$this->returnView($viewmodel->login(), true);
}
protected function logout(){
unset($_SESSION['is_logged_in']);
unset($_SESSION['user_data']);
session_destroy();
// Redirect
header('Location: '.ROOT_URL);
}
}
class UserModel extends Model {
public function profile() {
// Sanitize POST
$post = filter_input_array(INPUT_POST, FILTER_SANITIZE_STRING);
if($post['updateProfile']) {
#$name = $post['name'];
#$email = $post['email'];
#$id = $post['id'];
if (empty($post['name']) || empty($post['email'])) {
Messages::setMsg('Please Fill All Form Fields', 'error');
return;
}
// check if email is already taken
$this->query('SELECT * FROM users WHERE email = :email');
$this->bind(':email', $post['email']);
$row = $this->emailExist();
if ($row) {
Messages::setMsg('Email already Exist', 'error');
return;
} else {
# Update the MySQL
$this->query("UPDATE users SET name =:name, email =:email WHERE id =:id");
$this->execute();
// Verify
if($this->lastInsertId()){
Messages::setMsg('Successfull Updated', 'success');
return;
} else {
Messages::setMsg('Error while updating data', 'error');
return;
}
}
}
return;
}
}
<div class="panel panel-default">
<div class="panel-heading">
<h3 class="panel-title">Update Data</h3>
</div>
<div class="panel-body">
<form method="post" action="<?php $_SERVER['PHP_SELF']; ?>">
<div class="form-group">
<label>Name</label>
<input type="text" name="name" class="form-control" value="<?php echo $_SESSION['user_data']['name'];?>" />
</div>
<div class="form-group">
<label>Email</label>
<input type="text" name="email" class="form-control" value="<?php echo $_SESSION['user_data']['email'];?>" />
<input type="hidden" name="id" class="form-control" value="" />
</div>
<input class="btn btn-primary" name="updateProfile" type="submit" value="Submit" />
</form>
</div>
</div>
更新数据
类函数execute()需要接受sql字符串中绑定参数的数组值
在classed/Model.php中更改:
public function execute() {
$this->stmt->execute();
}
public function execute($params=NULL) {
$this->stmt->execute($params);
}
$this->execute();
至:
public function execute() {
$this->stmt->execute();
}
public function execute($params=NULL) {
$this->stmt->execute($params);
}
$this->execute();
您没有将变量绑定到sql字符串中指定的变量名
models/user.php-替换:
public function execute() {
$this->stmt->execute();
}
public function execute($params=NULL) {
$this->stmt->execute($params);
}
$this->execute();
与:
$this->execute(array(':name' => $post['name'], ':email' => $post['email'], ':id'=>$post['id']));
正如错误消息所说,你没有绑定更新查询的参数。请你给我举个例子,说明我的意思是ideas@peter它可能的副本仍然不起作用。更新数据时不断给我错误。你能粘贴更新的-classed/Model.php
和models/user.php
?那么我应该怎么做呢新建粘贴中的代码
字段>>向下滚动>>单击创建新粘贴
将URL复制粘贴到此处。