Jump to content


Photo

Adding and updating numbers in a database


  • Please log in to reply
6 replies to this topic

#1 supanoob

supanoob
  • Members
  • PipPipPip
  • Advanced Member
  • 342 posts

Posted 29 April 2006 - 06:00 PM

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?

#2 Orio

Orio
  • Staff Alumni
  • Advanced Member
  • 2,491 posts

Posted 29 April 2006 - 06:20 PM

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.
Think you're smarty?

(Gone until 20 to November)

#3 supanoob

supanoob
  • Members
  • PipPipPip
  • Advanced Member
  • 342 posts

Posted 29 April 2006 - 10:16 PM

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 24

i get that error :S

this is the code after i changed what i thought needed to be changed

<?php
session_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();
}
?>


#4 .josh

.josh
  • Staff Alumni
  • .josh
  • 14,871 posts

Posted 30 April 2006 - 09:41 AM

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
Did I help you? Feeling generous? Buy me lunch! 
Please, take the time and do some research and find out how much it would have cost you to get your help from a decent paid-for source. A "roll-of-the-dice" freelancer will charge you $5-$15/hr. A decent entry level freelancer will charge you around $15-30/hr. A professional will charge you anywhere from $50-$100/hr. An agency will charge anywhere from $100-$250/hr. Think about all this when soliciting for help here. Think about how much money you are making from the work you are asking for help on. No, we do not expect you to pay for the help given here, but donating a few bucks is a fraction of the cost of what you would have paid, shows your appreciation, helps motivate people to keep offering help without the pricetag, and helps make this a higher quality free-help community :)

#5 Brandon Jaeger

Brandon Jaeger
  • Members
  • PipPipPip
  • Advanced Member
  • 155 posts
  • LocationPA
  • Age:25

Posted 30 April 2006 - 09:51 AM

$result=mysql_query($query);
if (!$result)
{
die (mysql_error());
}
A slight optimization:
$result=mysql_query($query) or die(mysql_error());

PHP is my friend (=

#6 supanoob

supanoob
  • Members
  • PipPipPip
  • Advanced Member
  • 342 posts

Posted 30 April 2006 - 10:24 AM

thanks i will let you know how i get on :D

#7 supanoob

supanoob
  • Members
  • PipPipPip
  • Advanced Member
  • 342 posts

Posted 30 April 2006 - 01:59 PM

well, good news it works kinda :P

i 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 :(




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users