伙计们,我在更新我的个人资料时在我的PHP MVC代码中出现错误,它告诉我错误,同时更新我的井,但似乎很烂,不知道该怎么办。请帮我!
它一直警告我这个警告警告:pdostatement :: execute((:sqlstate [hy093]:无效参数编号:界变量的数量不匹配c: xampp xampp htdocs htdocs php.dev php.dev classe classe.php in 37 p>这是我的class/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);
}
}
这是我的控制器/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);
}
}
这是我的型号/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;
}
这是我的视图/用户/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>
如果用户不存在,则运行:
$this->query("UPDATE users SET name =:name, email =:email WHERE id =:id");
试图更新现有用户,而不是添加新用户。该查询因错误而失败,因为您没有将任何内容绑定到:id
(并且您不应该绑定,因为您正在尝试添加新用户(。
而是尝试此查询:
INSERT INTO users SET name =:name, email =:email
它应该起作用。它仍然不允许您编辑用户,但是它可以添加新的。