supanoob Posted April 29, 2006 Share Posted April 29, 2006 well, i am trying to do a gym for my Browser based game. now i know what i want to do i just dont know where to start.now the basic idea is i want to add a random number between 0.1 and 0.7 to the number that is already in the database (which to start with is 10) then i want it to update it straight away. any advice on how to do this? Link to comment https://forums.phpfreaks.com/topic/8718-adding-and-updating-numbers-in-a-database/ Share on other sites More sharing options...
Orio Posted April 29, 2006 Share Posted April 29, 2006 Something like this?<?php$sql="SELECT num FROM `table` WHERE user='$user'";$result=mysql_query($sql);$the_num=mysql_result($result, 0);$rand=rand(0,7);$rand=$rand/10;$update=$the_num+$rand;$sql2="UPDATE `table` SET num='$update' WHERE user='$user'";if(mysql_query($sql2)){echo("updated");}?>Orio. Link to comment https://forums.phpfreaks.com/topic/8718-adding-and-updating-numbers-in-a-database/#findComment-32001 Share on other sites More sharing options...
supanoob Posted April 29, 2006 Author Share Posted April 29, 2006 Warning: mysql_result(): supplied argument is not a valid MySQL result resource in /home/content/A/r/c/ArchAngel390/html/game/power.php on line 24i get that error :Sthis is the code after i changed what i thought needed to be changed[code]<?phpsession_start();require_once('header.php');if (!$_SESSION['valid_user']) { echo 'you are not logged in'; die();} $user=$_SESSION['valid_user'];$query="select playerid, user, email, gold, fatigue, maxfat, power, speed, dex, intel, health, maxhealth, gender from players where user='$user'";$result=mysql_query($query);if (!$result){die (mysql_error());}$num_rows=mysql_num_rows($result);$sql="SELECT power FROM $tablename WHERE user='$user'";$result=mysql_query($sql);$power=mysql_result($result, 0);$rand=rand(0,7);$rand=$rand/10;$update=$the_num+$rand;$sql2="UPDATE `table` SET num='$update' WHERE user='$user'";if(mysql_query($sql2)){echo "you gained $rand power";die();}{echo "Power is now $power";die();}?>[/code] Link to comment https://forums.phpfreaks.com/topic/8718-adding-and-updating-numbers-in-a-database/#findComment-32046 Share on other sites More sharing options...
.josh Posted April 30, 2006 Share Posted April 30, 2006 so which one is line 24? i think it might be here:$sql2="UPDATE `table` SET num='$update' WHERE user='$user'";shouldn't 'table' actually be your table name instead of 'table' ?i see earlier on in the code you have a table named players and then after that you refer to your table name as $tablename Link to comment https://forums.phpfreaks.com/topic/8718-adding-and-updating-numbers-in-a-database/#findComment-32133 Share on other sites More sharing options...
Brandon Jaeger Posted April 30, 2006 Share Posted April 30, 2006 [code]$result=mysql_query($query);if (!$result){die (mysql_error());}[/code]A slight optimization:[code]$result=mysql_query($query) or die(mysql_error());[/code] Link to comment https://forums.phpfreaks.com/topic/8718-adding-and-updating-numbers-in-a-database/#findComment-32134 Share on other sites More sharing options...
supanoob Posted April 30, 2006 Author Share Posted April 30, 2006 thanks i will let you know how i get on :D Link to comment https://forums.phpfreaks.com/topic/8718-adding-and-updating-numbers-in-a-database/#findComment-32140 Share on other sites More sharing options...
supanoob Posted April 30, 2006 Author Share Posted April 30, 2006 well, good news it works kinda :Pi click the train button. and it adds to the power. but evey time it does so it resets the power to 1 instead of adding it to the 10 that is alread in there.for example i click it once, and it added .2 to my power. so i went and checked the database to make sure i added and the power was then 1.2. so i went and clicked again and gained .7 and check the database and it was 1.7 instead of the 10.9 i wanted it too be :( Link to comment https://forums.phpfreaks.com/topic/8718-adding-and-updating-numbers-in-a-database/#findComment-32172 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.