如何根据php

时间:2016-10-19 22:21:14

标签: php mysqli

我有一个问题,弄清楚如何滚动骰子,以便结果将不执行任何操作或仅更新所选用户的库存。

<?php
if(isset($_SESSION['loggedin']))
    {
        include 'system/config.php';
        //SESSION
        $username = $_SESSION['loggedin'];

        //selecting id from table users                         
        $sql = "SELECT id FROM users WHERE username ='$username'";
        $result = mysqli_query($conn, $sql);
        $row = mysqli_fetch_assoc($result);

        //the user id from users
        $user_id      = $row['id'];

        $sql = "SELECT user_id, size_kg, fish1, fish2, fish3, fish4, fish5, seaweed FROM inventory WHERE user_id='$user_id'";
        $result = mysqli_query($conn, $sql);
        $row = mysqli_fetch_assoc($result);

        $userId= $row['user_id'];
        $fish1 = $row['fish1'];
        $fish2 = $row['fish2'];
        $fish3 = $row['fish3'];
        $fish4 = $row['fish4'];
        $fish5 = $row['fish5'];
        $seaweed = $row['seaweed'];

        //for debug
        echo "$userId. id " . "$fish1 . fish1 <br>";


        //$CatchProbability: dice roll for Catch Probability (ex: CatchProbability >= 30; echo You cought a $FishType(fish1, fish2, fish3, fish4, fish5, seaweed))
        function rollcatch() {
        return mt_rand(1,100);
        }
        echo rollcatch()." catch <br>";//for debug


        //$FishType: dice roll for type of Fish (ex: $FishType(fish1) = 1-10 , $FishType(fish2) = 11-20, $FishType(fish4) = 31-40 $FishType(fish5) = 41-50, $FishType(seaweed) = 51-100)
        function rolltype() {
        return mt_rand(1,100);
        } 
        echo rolltype()." type <br>";//for debug


        function catchFish(){
            if(rollcatch() < 30){
                $rolltype = rolltype();
                $result = "";
                if($rolltype > 0 && $rolltype<10){
                    $result = "fish1";
                }
                else if($rolltype > 10 && $rolltype<=20){
                    $result = "fish2";
                }
                else if($rolltype > 20 && $rolltype<=30){
                    $result = "fish3";
                }
                else if($rolltype > 30 && $rolltype<=40){
                    $result = "fish4";
                }
                else if($rolltype > 40 && $rolltype<=50){
                    $result = "fish5";
                }
                else
                {
                $result="seaweed";
                }
                $sql = "UPDATE inventory SET $result = $result + 1 WHERE user_id='$userId'";
                if(mysqli_query($conn, $sql)){
                    echo("You caught a $result");
                }      

            }
            else
            {
                echo("You caught nothing...");
            }
        }
            catchFish(); //for debug
    }

?>

请帮助我调试,我在成功捕获时遇到此错误: 警告:mysqli_query()期望参数1为mysqli,在第72行的...中给出为null

第72行

<?  
    if(mysqli_query($conn, $sql)){
        echo("You caught a $result");
    }     
?>

2 个答案:

答案 0 :(得分:1)

如果我理解你的问题:

if($CatchProbability <= 30) {
    $FishType = rolltype();
    if ($FishType <= 10) {
        $sql = "UPDATE inventory SET fish1 = fish1 +1 WHERE user_id = '$userId'";
        if(mysqli_query($conn, $sql))
        { 
            echo " </br> You caught one Fish1.</br>";
        }
    }
    echo 'You caught a '.$FishType.';

答案 1 :(得分:1)

不完全确定我完全理解,但这段代码:

  1. 检查您是否捕获了鱼(30%的几率)
  2. 确定您捕获的鱼类(鱼类占5%的几率,海藻50%)
  3. 向数据库中捕获的鱼添加1
  4. 输出您抓到的鱼的信息
  5. 希望这对你有用

    function catchFish(){
        if(rollcatch() < 30){
            $rolltype = rolltype();
            $result = "";
            if($rolltype > 0 && $rolltype<=10){
                $result = "fish1";
            }
            else if($rolltype > 10 && $rolltype<=20){
                $result = "fish2";
            }
            else if($rolltype > 20 && $rolltype<=30){
                $result = "fish3";
            }
            else if($rolltype > 30 && $rolltype<=40){
                $result="fish4";
            }
            else if($rolltype > 40 && $rolltype<=50){
                $result="fish5";
            }
            else
            {
            $result="seaweed";
            }
    
            $sql = "UPDATE inventory SET $result = $result + 1 WHERE user_id='$user_id'";
            if(mysqli_query($conn,$sql)){
                echo("You caught a $result.");
            }      
    
        }
        else
        {
            echo("You caught nothing...");
        }
    }