doudui1850 2014-01-17 15:20
浏览 61

数据库中的PHP更新字段 - 1行停止字段更新

I am doing a test a friend sent me to test my PHP skills - But I have hit a wall. I need to be able to update the database with the new details after inserting a record. This is purely done through scripts - no GUI's.

Here is the code:

<?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();

But when I have this line $this->oldemail = $this->email; in, the email will not change in the db - but when I take it out all works fine. what could be the problem??

  • 写回答

2条回答 默认 最新

  • dtc88867 2014-01-17 15:33
    关注

    Where do you have the oldemail set? Is it before you attempt to do a _save? If so, you could end up doing an UPDATE on a non-existent record, rather than an INSERT. The code isn't great -- maybe instead of trying to figure out whether to do an INSERT or UPDATE, you could simply do a REPLACE INTO.

    评论

报告相同问题?

悬赏问题

  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法