2013-10-21 50 views
0

你好我正在使用followng代碼來更新密碼,但我需要選擇哪些用戶應該在我的SQL查詢內部更新有一個WHERE子句,如果我把數字作爲id像23是工作,但我希望這個ID來從POST方法,其發佈的ID還從形式,這裏是這個版本的代碼,它給我的錯誤:在PDO連接中的WHERE子句中使用變量

`SQLSTATE[HY093]: Invalid parameter number: number of bound variables does not match number of tokens` 

這裏這是代碼

<?php 


class Users { 
public $password = null; 
public $salt = "Zo4rU5Z1YyKJAASY0PT6EUg7BBYdlEhPaNLuxAwU8lqu1ElzHv0Ri7EM6irpx5w"; 

public function __construct($data = array()) { 
if(isset($data['id'])) $this->id = stripslashes(strip_tags($data['id'])); 
if(isset($data['password'])) $this->password = stripslashes(strip_tags($data['password'])); 
} 


public function storeFormValues($params) { 
//store the parameters 
$this->__construct($params); 
} 


public function register() { 
$correct = false; 
try { 
$con = new PDO(DB_DSN, DB_USERNAME, DB_PASSWORD); 
$con->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); 
$sql = "Update users SET password = :password WHERE userID = :id"; 

$stmt = $con->prepare($sql); 
$stmt->bindValue("password", hash("sha256", $this->password . $this->salt), PDO::PARAM_STR); 
$stmt->execute(); 



return "Registration Successful <br/> <a href='index.php'>Login Now</a>"; 
     }catch(PDOException $e) { 
       return $e->getMessage(); 
     } 
} 
} 


?> 

所以問題是如何把張貼的WHERE userID =:id;

+1

'$ stmt-> bindValue(「id」,...);' – tttony

+0

錯誤消息很明顯。 – Mihai

+0

工作,你可以發佈它作爲答案,我會接受它。謝謝。 –

回答

0

您忘記bindValue()id

$stmt->bindValue("id",...); 

編輯:我知道Marc B答案是第一個,但我只是回答,因爲John Siniger要求寫前面回答

3

你永遠不會綁定:id的值。你需要像

$stmt->bindValue(':id', $_POST['id']);