Napisano: 14 Avg 2015 23:15
Postavio sam temu i na StackOverflow , ali do konkretnog rešenja nismo došli - verovatno je u pitanju neka greška u SQL-u, ali ne razumem kako sa jednom kolonom radi, a sa drugom ne?! Prvi put sam funkciju update() testirao na update.php (tu korisnik ažurira informacije). Tamo sve radi super tj. menja podatke korisnika (za sada samo puno ime). Sada to pokušavam da odradim na admin.php, ali ne ide. Hoću da ažuriram kolonu tako da izmeni grupu korisniku u 4 gde su blokirani korisnici (grupe su u vidu JSON-a, naprimer {"admin" : 1} (admin true)). Evo i stranica:
+ admin.php<?php
require_once 'core/init.php';
$user = new User();
if($user->isLoggedIn()) {
if(!$user->hasPermission('admin')) {
Redirect::to('index.php');
}
} else {
Redirect::to('login.php');
}
?>
<?php echo $head; ?>
<body>
<div class="jumbotron">
<h1 class="header">Admin Hub</h1>
<p>Welcome <?php echo $user->data()->username; ?>!</p>
<a class="btn btn-primary btn-lg" href="index.php" role="button">Back to index</a> <a class="btn btn-danger btn-lg" href="logout.php" role="button">
Log out</a>
<?php
$db = DB::getInstance();
?>
<div class="adminhub-user" align="center">
<form action="" method="post">
<label for="username">Username: </label>
<input type="text" name="username" id="username">
<input type="submit" name="find-username" class="btn btn-default">
</form>
<?php if(Input::exists()) {
if(isset($_POST['find-username'])) {
$username = Input::get('username');
if(!empty($username)) {
if($user->find($username) == false) {
echo 'That user doesn\'t exists.';
} else {
?>
<table border="1">
<tr>
<td><b>Username</b></td>
<td><b>Full name</b></td>
<td><b>Date and time of join</b></td>
<td><b>Group</b></td>
<td><b>Rank</b></td>
</tr>
<tr>
<td><?php echo $user->data()->username; ?></td>
<td><?php echo $user->data()->name; ?></td>
<td><?php echo $user->data()->joined; ?></td>
<td><?php echo $user->data()->group; ?></td>
<td>
<?php
if($user->hasPermission('admin')) {
echo 'Admin';
} else if($user->hasPermission('moderator')) {
echo 'Mod';
} else {
echo 'User';
}
?>
</td>
</tr>
</table>
<?php } } } } ?>
</div>
<div class="adminhub-ban" align="center">
<form action="" method="post">
<label for="ban">Block user:</label>
<input type="text" name="ban" id="ban">
<input type="submit" name="block-username" class="btn btn-default">
</form>
<?php
if(Input::exists()) {
if(isset($_POST['block-username'])) {
$banUser = Input::get('ban');
if(!empty($banUser)) {
if($user->find($banUser) == false)
{
echo "That user doesn't exists.";
}
else
{
try {
$user->update(array('group' => 4), $user->data()->id);
Session::flash('home', 'User is blocked.');
Redirect::to('index.php');
} catch (Exception $e) {
print_r($e);
die($e->getMessage());
}
}
}
}
}
?>
</div>
</div>
</div>
</body>
</html>
Ovo je funkcija u User.php koju pozivam sa $user->update()
public function update($fields = array(), $id = null) {
if(!$id && $this->isLoggedIn()) {
$id = $this->data()->id;
}
if(!$this->_db->update('users', $id, $fields)) {
throw new Exception('There was a problem updating');
}
}
A ovo je funkcija update, ali u DB.php (opšta):
public function update($table, $id, $fields) {
$set = '';
$x = 1;
foreach($fields as $name => $value) {
$set .= "{$name} = ?";
if($x < count($fields)) {
$set .= ',';
}
$x++;
}
$sql = "UPDATE {$table} SET {$set} WHERE ID = {$id}";
echo $sql;
if(!$this->query($sql, $fields)->error()) {
return true;
}
return false;
}
Po Rastinom predlogu sam uključio error_reporting u init.php , ali ne pokazuje mi se nikakva greška. Sa print_r($e) hvatam Exception i dobijam sledeće:
Exception Object ( [message:protected] => There was a problem updating [string:Exception:private] => [code:protected] => 0 [file:protected] => /home/nikola/public_html/app/classes/User.php [line:protected] => 52 [trace:Exception:private] => Array ( [0] => Array ( [file] => /home/nikola/public_html/app/admin.php [line] => 111 [function] => update [class] => User [type] => -> [args] => Array ( [0] => Array ( [group] => 4 ) [1] => 32 ) ) ) [previous:Exception:private] => )
Dopuna: 15 Avg 2015 13:44
Rešeno! Umesto
$user->update(array('group' => 4), $user->data()->id);
je trebalo
$user->update(array('`group`' => 4), $user->data()->id);
Video sam da je GROUP rezervisana SQL komanda pa sam pretpostavio da bi ovo moglo da pomogne
|