[英]How to make a dice roll UPDATE table data based on the roll result in php
i have a problem figuring out, how to roll the dice/s, so that the result/s will either do nothing or only UPDATE the selected users inventory. 我在弄清楚如何掷骰子时遇到问题,因此结果将不执行任何操作或仅更新所选用户的广告资源。
<?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
}
?>
Please, help me to debug, I get this error on successful catch: Warning: mysqli_query() expects parameter 1 to be mysqli, null given in ... on line 72 请帮助我进行调试,在成功捕获时出现此错误:警告:mysqli_query()期望参数1为mysqli,在第72行的...中给出null
Line 72 72行
<?
if(mysqli_query($conn, $sql)){
echo("You caught a $result");
}
?>
If I understood your question correctly: 如果我正确理解了您的问题:
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.';
Not entirely sure I understood perfectly, but this code: 不能完全确定我完全理解,但是这段代码:
Hopefully this works for you 希望这对你有用
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...");
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.