它不会更新它一直给我错误的数据。如果电子邮件已经存在,它应该告诉我电子邮件存在,但它无法更新用户数据。
它给了我这个错误:
警告:PDOStatement :: execute():SQLSTATE [HY093]:参数号无效:第37行的C:\ xampp \ htdocs \ php.dev \ classes \ Model.php中没有绑定任何参数
归类/ Model.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);
}
}
控制器/ users.php
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);
}
}
模型/ user.php的
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;
}
}
查看/用户/ profile.php
<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>
答案 0 :(得分:0)
您的类函数execute()需要接受sql字符串中绑定参数的数组值。
在分类/ Model.php中更改:
rootTreeNode = new tree{DataToInsert, nullptr, nullptr};
要强>
public function execute() {
$this->stmt->execute();
}
您没有将变量绑定到sql字符串中的指定变量名。
models / user.php - 替换:
public function execute($params=NULL) {
$this->stmt->execute($params);
}
使用:
$this->execute();