Jump to content

Adding and updating numbers in a database


supanoob

Recommended Posts

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
Share on other sites

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
Share on other sites

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

[code]<?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();
}
?>[/code]
Link to comment
Share on other sites

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
Share on other sites

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 :(
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.