Click to See Complete Forum and Search --> : Database question


Mariol
11-22-2004, 03:21 PM
I am writing a code and I managed to make my program that when people log in it gets their information from the database and uses it to run a program but now i want it to replace the information that it got with the new results from the program. Please help. thanks.

Genixdeae
11-22-2004, 05:46 PM
are you tring to make it so that after the program has been run it will take the results from the program and insert it into the database?

if that's the case then you can just use the UPDATE (http://dev.mysql.com/doc/mysql/en/UPDATE.html)function.

give us a little bit more information about the program and we can help u a little bit more.

Mariol
11-22-2004, 07:07 PM
that is exactly what i want to do is update but in php format.

Genixdeae
11-22-2004, 10:38 PM
what kind of database are you connecting to? is it a MySQL? i can't exactly give you an exact update query without knowing what your code looks like but here is the just of it

$query = "UPDATE `table` SET `colname` = 'value' WHERE `uniqueid` = 'value'";
$result = mysql_query($query, "database connection");

is there a way i can see the program or could you send me part of the code that grabs from the database then i can give you more help

Mariol
11-23-2004, 07:19 AM
here is my code:

<?php
$conn = mysql_connect("localhost","USERNAME","PASSWORD");
$db = mysql_select_db("Fgame");
$handle = $_POST["handle"];
$password = $_POST["password"];
$result = MYSQL_QUERY("SELECT * from reset WHERE handle='$handle'and password='$password'")
or die ("not found or not matched");
$worked = mysql_fetch_array($result);

$player_one = $worked[handle];

$player1_dexterity = $worked[dexterity];

$player1_dodge = $worked[dodge];

$player1_strength = $worked[strength];

$player_image = $worked[url];

$player1_random;

$player1_difficulty;

$player1_critical;

$player1_damage;

$player1_hit_roll;

$player1_random_two;

$player1_random_one;

$player1_critical;

$player_two='computer';

$player2_dexterity=1;

$player2_dodge=1;

$player2_strength=2;

$player2_random;

$player2_difficulty;

$player2_critical;

$player2_damage;

$player2_hit_roll;

$player2_random_two;

$player2_random_one;

$player2_critical;

$a=rand(0, 6);

$success;

$player1_hp = $worked[hp];

$player2_hp=39;

$turn = 0;


if($worked)
do
{
$turn++;
echo "<br /><font color='red'>Round " . $turn . " Begins</font><br />";

if ($player1_hp <= 0)

{

echo "<font color='blue'>$player_one falls and $player_two wins<br /></font>";

}
echo "<br /><b>$player_one Strikes<br /></b>";

$c=rand(-100, 100);

echo "Random number is $c<br />";

$player1_hit_roll=$player1_dexterity*20+$c;

echo "Hit Roll is $player1_hit_roll<br />";

$player2_difficulty=$player2_dodge*20;

echo "Difficulty is $player2_difficulty<br />";

$player1_random_two=rand(0, 5);

if ($player1_hit_roll >= $player2_difficulty)

{

echo "success<br />";

$player2_damage=2-2+$player1_random_two;

echo "Damage is $player2_damage<br />";

$player2_hp=$player2_hp-$player2_damage;

echo "<i>$player_two hit points are $player2_hp<br /></i>";

}

else

{


echo "Miss<br />";

}

if ($c == 100)

{

echo "Critical<br />";

$player2_critical=$player2_damage*2;

echo "Damage is = $player2_critical<br />";

$player2_hp=$player2_hp-$player2_critical+$player2_damage;

echo "<i>$player_two hit points are $player2_hp<br /></i>";

}

if ($player2_hp <= 0)

{

echo "<font color='blue'>$player_two falls and $player_one wins<br /></font>";

}

echo "<b>$player_two Strikes<br /></b>";

$d=rand(-100, 100);

echo "Random number is $d<br />";

$player2_hit_roll=$player2_dexterity*20+$d;

echo "Hit Roll is $player2_hit_roll<br />";

$player1_difficulty=$player1_dodge*20;

echo "Difficulty is $player1_difficulty<br />";

$player2_random_two=rand(0, 5);

if ($player2_hit_roll >= $player1_difficulty)

{

echo "success<br />";

$player1_damage=2-2+$player2_random_two;

echo "Damage is $player1_damage<br />";

$player1_hp=$player1_hp-$player1_damage;

echo "<i>$player_one hit points are $player1_hp<br /></i>";

}

else

{

echo "Miss<br />";

}

if ($d == 100)

{

echo "Critical<br />";

$player1_critical=$player1_damage*2;

echo "Damage is = $player1_critical<br />";

$player1_hp=$player1_hp-$player1_critical+$player1_damage;

echo "<i>$player_one hit points are $player1_hp<br /></i>";

}
}
while ($player1_hp > 0 and $player2_hp > 0);
?>

Genixdeae
11-23-2004, 10:33 AM
ok, so you are tring to change the database stats during the fighting or at the end? if you are tring to change it during the fight what are you tring to change, the hp, the dexterity...if you want to change it at the end of the fight are you having it so that the winner will gain stats?

here is a update statement for the things in the database. you can modify it for what you need/want.
$query = "UPDATE `reset` SET `dexterity` = 'Value', `dodge` = 'value', `strength` = 'value', `url` = 'value', `hp` = 'value' WHERE `handle` = '". $handle ."' AND `password` = '". $password ."'";
$result = mysql_query($query, $db); That should work

Mariol
11-23-2004, 11:14 AM
i am trying to update the database at the end of the fight, by putting in the database the new hp for that person. I do want to make it so that more than one person will be able to fight against each other but i am still working on my code and little by little I add new things to it.

Genixdeae
11-23-2004, 11:29 AM
ok to update the hp at the end. put the following at the end of the fight.
$query = "UPDATE `reset` SET `hp` = 'value' WHERE `handle` = '". $handle ."' AND `password` = '". $password ."'";
$result = mysql_query($query, $db); that should work. how are you having the people access their account? through a login form then store in a session or cookie? or do you just have it coming from a form?

Mariol
11-23-2004, 11:47 AM
well at the moment only one person logs in trough a form and then is directed to this form but that will not be like that once I finish the code, i want to set it up so that the players could do other things and pick who they want to fight out of all the people in the database that i have set up.

Genixdeae
11-23-2004, 11:54 AM
what you could do for multiple battles is, have a person 'create' a game which takes them to another screen that will have, lets say 4 spots open. in those 4 spots other people can join.

when a game is made create a database that will create the columns with all the fighters information.(e.g. dexterity, hp, strength, ect). and insert the user information. when someone else joines insert their information into the database then reload the create game page and have it display the people who are in the game.

that prolly jus absolutly confused you but it makes sence in my head lol. umm i could try to make it more clear if you want to try and use it.

Mariol
11-23-2004, 12:03 PM
That did confused me a little, I think I get it a little don't you think that would be a lot of work, what i want is it would show who are registered in the database and the people would select the person they want to attack and then the code would use the person's stats from the database and run and at the end the database would be updated with the new Hp for both of them and at the end of the day i would reset their hp so they could fight again with anyone they want.

Genixdeae
11-23-2004, 12:17 PM
o, ok...i see i was kinda looking at it in a real time situation(people who are online and logged in can fight) but all you are looking for is just fighting in general. in that case you could just make a checkbox list. the person checks who they want to fight, click next and it's on.

Mariol
11-23-2004, 03:03 PM
yes something like that.

Genixdeae
11-23-2004, 03:24 PM
all you have to do then is jus make a for statement. i dont fully know how checkboxes work cuz i never use them but if i remember right they have a value of 1 or 0(0=not checked).


$total = "";//this is the total number of records in the database
for($i="1";$i<=$total;$i++){
echo "Fight: <input type="checkbox" name="username">";
echo username;
}

Mariol
11-23-2004, 05:06 PM
Hey you know that sounds great i will try that maybe tomorrow. Thanks again and i will try that and let you know what happened.

Mariol
11-23-2004, 10:56 PM
Ok you know what i tried but couldn't get it to work at all.