dongyong1942 2015-12-14 17:36
浏览 23
已采纳

PHP / MySQL跟踪用途

I'm trying to update my tables with a total number of uses when a function is called. What I need to do is grab the former number of uses, and add one. I have a pretty good general idea how to go about this, but I don't quite know how to get unique ID's with their respective uses. This is what I have so far...

<?php
 $query = $conn->query('select name, uses from users');
    $i = 0;
    while($r = $query->fetch(PDO::FETCH_OBJ)){
        $name[$i] = $r->name;
        $uses[$i] = $r->uses;
    $i = $i+1;
    }
if(isset($_POST['mName']))
{
    $mName = urldecode($_POST['mName']);
    $mUses = $uses + 1;

"UPDATE users 
                        SET uses=:uses WHERE name=:name";
$stmt = $conn->prepare($sqlUPDATE);
$stmt->bindParam(':uses', $mUses);
$stmt->bindParam(':name', $mName);
$stmt->execute();
 ?>

I can see my issue is that I'm assigning the uses variable to an array, I don't know how to get it specific to the username. Is this a simple SQL query I'm missing?

  • 写回答

1条回答 默认 最新

  • douan2478 2015-12-14 18:11
    关注

    You can store the data in an array called $uses with the key being the user name and the value being the number of uses. Then if you detect a POST with the mName parameter set, you can reference your $uses array with that name and get the number of uses, and add 1.

    <?php
        $query = $conn->query('select name, uses from users');
        while($r = $query->fetch(PDO::FETCH_OBJ)){
            $uses[$r->name] = $r->uses;
        }
        if(isset($_POST['mName'])) {
            $mName = urldecode($_POST['mName']);
            $mUses = $uses[$mName] + 1;
            $sqlUPDATE = "UPDATE users SET uses=:uses WHERE name=:name";
            $stmt = $conn->prepare($sqlUPDATE);
            $stmt->bindParam(':uses', $mUses);
            $stmt->bindParam(':name', $mName);
            $stmt->execute();
        }
    ?>
    

    Though there is no error checking and handling in here. If there happens to be a POST with mName and that name doesn't exist, nothing will update, but nothing will insert for new users. Also, instead of using a name, it would be better to use an id for the user if possible.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度