试图向用户添加点数,但不起作用

时间:2014-02-22 02:31:11

标签: php

所以我试图找出一种方法,每次点击一个按钮时为用户添加一个点,但我无法弄明白。现在,按钮显示,当用户点击它时,没有任何反应。谢谢你的帮助:)

 <table align="center">
    <caption><b>Users</b></caption>
    <br>
    <thead>
        <tr>
            <th>Username</th>
            <th></th>
            <th></th>
            <th></th>
        </tr>
        <tr>
        <th><img src="1.png" align="left"></th>


        </tr>
    </thead>
    <tbody>     
    <?php 
    $user = new User();
    $get_all = DB::getInstance()->query("SELECT * FROM users ORDER BY points DESC Limit 0 , 20");
        foreach($get_all->results() as $r) {
    ?>
        <tr> 
            <td><?php echo ucfirst($r->username); ?>
            <?php echo ucfirst($r->points); if($r->points==140) {
                echo $r->username;
                }?> 
            <?php ucfirst($r->rank); if($r->points==140) {
                echo $r->rank; } ?>


<?php
$user = new User();


if(Input::exists()) {
    if(Token::check(Input::get('token'))) {
        $newPoints = Input::get('points') + 1; 

        try {
            $user->update(array(
                'points' => $newPoints
            ));
        } catch(Exception $e) {
            die($e->getMessage());
        }

        Session::flash('home', 'Your details have been updated.');
        Redirect::to('index.php');
    }
}
?>
<form method="post">
    <input type="submit" name="points_add" value="Points +1">
    <input type="hidden" name="token" value="<?php echo Token::generate(); ?>">
</form></td>

        </tr>
    <?php   }
    ?>  
    </tbody>
</table>

1 个答案:

答案 0 :(得分:0)

这是一个近似的答案。我不得不假设你的表模式(我猜测有一个userid列)以及数据库API(我写了一个参数化查询,其值为query方法的参数 - 你可能需要将其重新编码为适当的API方法,我不承认。

<table align="center">
     <caption><b>Users</b></caption>
     <br>
     <thead>
     <tr>
     <th>Username</th>
     <th></th>
     <th></th>
     <th></th>
     </tr>
     <tr>
     <th><img src="1.png" align="left"></th>


     </tr>
     </thead>
     <tbody>     
     <?php 
     $get_all = DB::getInstance()->query("SELECT * FROM users ORDER BY points DESC Limit 0 , 20");
     foreach($get_all->results() as $r) {
    ?>
    <tr> 
    <td><?php echo ucfirst($r->username); ?>
    <?php echo ucfirst($r->points); if($r->points==140) {
         echo $r->username;
    } ?> 
    <?php if($r->points==140) {
         echo ucfirst($r->rank);
     } ?>


    <?php
    $user = new User();


    if(Input::exists()) {
        if(Token::check(Input::get('userid'))) {
            $userid = Input::get('userid');

            DB::getInstance()->query("UPDATE users SET points = points + 1 WHERE userid = ?", $userid);

            Session::flash('home', 'Your details have been updated.');
            Redirect::to('index.php');
        }
    }
    ?>
    <form method="post">
    <input type="submit" name="points" value="submit">
    <input type="hidden" name="userid" value="<?php echo $r->userid; ?>">
    </form></td>

    </tr>
    <?php   }
?>  
        </tbody>
</table>