伙计我在我的php mvc代码中有错误,当我更新我的个人资料时它告诉我错误,同时更新l检查我的好,但它似乎很糟糕,不知道该怎么做。请帮帮我!
它一直警告我这个警告 警告:PDOStatement :: execute():SQLSTATE [HY093]:参数号无效:绑定变量的数量与第37行的C:\ xampp \ htdocs \ php.dev \ classes \ Model.php中的标记数不匹配
这是我的classes / 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);
}
}
这里是我的controllers / user.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);
}
}
这是我的models / user.php代码
public function profile(){
// Sanitize POST
//$post = filter_input_array(INPUT_POST, FILTER_SANITIZE_STRING);
if(isset($_POST['updateProfile'])){
$name = $_POST['name'];
$email = $_POST['email'];
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->bind(':name', $_POST['name']);
$this->bind(':email', $_POST['email']);
$this->execute();
// Verify
if($this->lastInsertId()){
Messages::setMsg('Successfull Updated', 'success');
return;
} else {
Messages::setMsg('Error while updating data', 'error');
return;
}
}
}
return;
}
这里是我的观点/ users / profile.php代码
<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="<!?php echo $_SESSION['user_data']['id']?>" / -->
</div>
<input class="btn btn-primary" name="updateProfile" type="submit" value="Submit" />
</form>
答案 0 :(得分:0)
如果用户不存在,则会运行:
$this->query("UPDATE users SET name =:name, email =:email WHERE id =:id");
尝试更新现有用户,而不是添加新用户。查询失败并显示错误,因为您没有将任何内容绑定到:id
(您不应该,因为您正在尝试添加新用户)。
而是尝试此查询:
INSERT INTO users SET name =:name, email =:email
它应该有效。它仍然不允许您编辑用户,但它允许您添加新用户。