這裏,並試圖更新另一個用戶的組更新查詢PDO傳遞一個查詢。我不確定哪些不起作用。我從DB類中得到了sql回聲讓我看看我正在經歷什麼。 SQL應該是正確的,我會把它放在下面。通過使用我登錄的某個用戶(管理員)類
我會確保指出哪裏找這樣你就不會搜索了很多。而且,即使你只是對可能出現的問題有一點線索,也請分享。
這裏是我正努力更新用戶的形式。我只會發布相關的PHP和表單,以便您可以看到我想要完成的工作。
$selectedUser = new User(Input::get('username'));
try {
$user->update(array(
'group' => Input::get('group')
),'username',$selectedUser->data()->username);
Session::flash('home','The group has been updated');
Redirect::to('index.php');
} catch(Exception $e) {
die($e->getMessage());
}
<h3>1: Standard User</h3>
<h3>2: Administrator</h3>
<h3>3: Moderator</h3>
<h3>4: Admin/Mod</h3>
<form action="" method="post" autocomplete="off">
<label for="group">Change Group to</label>
<input type="number" name="group" id="group">
<label for="username">for this username</label>
<input type="text" name="username" id="username">
<input type="submit" value="Change Group">
<input type="hidden" name="token" value="<?php echo Token::generate(); ?>">
</form>
當我輸入2成團場雅各到用戶名字段,這是我收到:
UPDATE設置用戶組=?其中username =雅各布
有問題的更新。
這個類是很長,所以我要發佈相關材料,如果發生混淆請你只問,我會詳細說明。如果你看第二個方法更新,你可以看到我在哪裏回聲SQL。這裏是數據庫類:
public function query($sql, $params = array()) {
$this->_error = false;
if($this->_query = $this->_pdo->prepare($sql)){
$x = 1;
if(count($params)){
foreach($params as $param){
$this->_query->bindValue($x, $param);
$x++;
}
}
if($this->_query->execute()){
$this->_results = $this->_query->fetchAll(PDO::FETCH_OBJ);
$this->_count = $this->_query->rowCount();
} else {
$this->_error = true;
}
}
return $this;
}
public function update($table, $chosenfield, $id, $fields){
$set = '';
$x = 1;
foreach($fields as $name => $value){
$set .="{$name} = ?";
if($x < count($fields)){
$set .= ', ';
}
$x++;
}
$sql = "UPDATE {$table} SET {$set} WHERE {$chosenfield} = {$id}";
echo $sql, '<br>';
if(!$this->query($sql, $fields)->error()){
return true;
}
return false;
}
這是我在此使用的用戶類方法的難題的最後部分。這也是我將返回錯誤「出現了問題更新」:
public function update($fields = array(), $chosenfield = 'id', $id = null) {
if(!$id && $this->isLoggedIn()) {
$id = $this->data()->id;
}
if(!$this->_db->update('users', $chosenfield, $id, $fields)) {
throw new Exception('There was a problem updating.');
}
}
您是否'var_dump($ selectedUser)'確保構造函數正常工作? –
你也應該'var_dump(Input :: get('group'))'來確保它正常工作。 –
當我做他們都返回:致命錯誤:調用未定義的函數var_dumb()在C:\ xampp \ htdocs \ PHP_Registration_Login \ admin.php在行15 – NewbieCoder