2014-01-17 60 views
-1

我正在做一個測試朋友送我來測試我的PHP技能 - 但我已經打了一堵牆。我需要能夠在插入記錄後用新的細節更新數據庫。這完全是通過腳本完成的 - 沒有GUI。PHP更新數據庫中的字段 - 1行停止更新字段

下面是代碼:

<?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' => '[email protected]' 
)); 
$user->_save(); 
// $user->name('Jack'); 
// $user->occupation(); 
try { 
$user->email('[email protected]'); 
} catch (Exception $e) { 
echo $e->getMessage(); 
} 
$user->_save(); 

但是,當我在這行$this->oldemail = $this->email;,電子郵件不會在DB改變 - 但我把它拿出來時,一切工作正常。可能是什麼問題呢??

回答

0

Where你有oldemail套餐嗎?是否在你試圖做一個_save之前?如果是這樣,你最終可能會對不存在的記錄執行UPDATE,而不是INSERT。代碼不是很好 - 也許不是試圖找出是否執行INSERT或UPDATE,而是簡單地執行REPLACE INTO。

+0

'oldemail'設置在最頂端 – user3185528

+0

不,你在哪裏做'$ this-> oldemail = $ this-> email;'你說它打破了嗎?如果它在執行_save之前,oldemail將不會爲空,並且_save將嘗試執行UPDATE而不是INSERT。 –

0

你現在擁有它,與$user->name('Jack');$user->occupation();註釋掉,$this->oldname$this->oldoccupation的方式永遠不會從他們的null初始設置改變。

在您的UPDATE聲明中,您的條件爲WHERE name = :oldname。由於$this->oldnamenull,您沒有更新任何內容。