我正在做一个测试,朋友派我去测试我的PHP技能 - 但我已经碰壁了。我需要能够在插入记录后使用新的详细信息更新数据库。这完全是通过脚本完成的 - 没有GUI。
以下是代码:
<?php
class UserModel {
public $name = null, $occupation = null, $email = null, $oldname = null, $oldoccupation = null, $oldemail = null, $me, $handler, $result;
public function __construct(){
try{
$this->handler = new PDO('mysql:host=127.0.0.1;dbname=lab19', 'root', 'root');
$this->handler->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDOException $e) {
echo $e->getMessage();
die();
}
}
public function create($fields = array()){
if(count($fields) == 3){
$this->name = $fields['name'];
$this->occupation = $fields['occupation'];
$this->email = $fields['email'];
}
}
public function _save(){
if($this->oldname == null && $this->oldoccupation == null && $this->oldemail == null){
$sql = "INSERT INTO users (name, occupation, email) VALUES (:name, :occupation, :email)";
$this->result = $this->handler->prepare($sql);
$this->result->execute(array(
':name'=>$this->name,
':occupation'=>$this->occupation,
':email'=>$this->email
));
} else {
$sql = "UPDATE users SET name = :name, occupation = :occupation, email = :email WHERE name = :oldname";
$this->result = $this->handler->prepare($sql);
$this->result->execute(array(
':name'=>$this->name,
':occupation'=>$this->occupation,
':email'=>$this->email,
':oldname'=>$this->oldname
));
}
}
public function name($given_name = null) {
if($this->name == null){
if($given_name != null){
$this->name = $given_name;
}
} else {
if($given_name != null){
$this->oldname = $this->name;
$this->name = $given_name;
}
}
return $this->name;
}
public function occupation($given_occupation = null) {
if($this->occupation == null){
if($given_occupation != null){
$this->occupation = $given_occupation;
}
} else {
if($given_occupation != null){
$this->oldoccupation = $this->occupation;
$this->occupation = $given_occupation;
}
}
return $this->occupation;
}
public function email($given_email = null){
$this->verifyEmail($given_email);
if($given_email != null){
// $this->oldemail = $this->email; // THIS LINE IS THE ISSUE
$this->email = $given_email;
}
return $this->email;
}
public function verifyEmail($givenemail = null){
if($givenemail == null){
$email = $this->email;
} else {
$email = $givenemail;
}
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
throw new Exception('Email not valid.');
die();
}
}
}
$user = new UserModel();
$user->create(array(
'name' => 'Luke',
'occupation' => 'Programmer',
'email' => 'luke@gmail.com'
));
$user->_save();
// $user->name('Jack');
// $user->occupation();
try {
$user->email('demo@example.co.za');
} catch (Exception $e) {
echo $e->getMessage();
}
$user->_save();
但是当我有这行$this->oldemail = $this->email;
时,电子邮件不会在数据库中发生变化 - 但是当我把它取出时一切正常。可能是什么问题??
答案 0 :(得分:0)
哪里你有oldemail设置?是在你尝试做_save之前吗?如果是这样,您最终可能会在不存在的记录上执行UPDATE,而不是INSERT。代码不是很好 - 也许不是试图弄清楚是做INSERT还是UPDATE,你可以简单地做一个REPLACE INTO。
答案 1 :(得分:0)
当前拥有$user->name('Jack');
和$user->occupation();
注释掉$this->oldname
和$this->oldoccupation
的方式永远不会从null
的初始设置更改。
在UPDATE
声明中,您的条件为WHERE name = :oldname
。由于$this->oldname
为null
,因此您不会更新任何内容。