...

View Full Version : Form Updating



Smackie
09-03-2005, 05:36 PM
Im not new to php but i never really used the UPDATE function :confused: but i was wondering if someone could help me.. im making a php game and well im making the armor and weapon shops for it and well i ran into a problem with the forms im trying to make it where it will update the users info on the armor or the weapons and subtract the gold that the armor or weapon cost :-\ and also update the att or defense as well..
here is what i got on the armorshop2.php page (this is where it will update the database)


<?php
include 'db.php';

$armor = $_POST['armor'];

header("Location: ../index.php?pages=armorshop");

$sql = mysql_query("UPDATE users (armor)
VALUES('$armor')") or die (mysql_error());


?>

now here is the form page
armorshop.php

<center>
<?php
include 'db.php';

$sql = mysql_query("SELECT * FROM users WHERE user_name = '".$_SESSION['user_name']."'");
while ($row = mysql_fetch_array($sql)) {

if (($_SESSION['user_level'] == 1) || ($_SESSION['user_level'] == 2) || ($_SESSION['user_level'] == 3) || ($_SESSION['user_level'] == 4) || ($_SESSION['user_level'] == 5)) {

echo '<font class="txt">You have Gold: '. $row["gold"] .' and Money '. $row["money"] .'</font>';

}
}
?>
<?php
include 'db.php';

$sql = mysql_query("SELECT * FROM users WHERE user_name = '".$_SESSION['user_name']."'");
while ($row = mysql_fetch_array($sql)) {
?>
<br><br>
<font class="txt">
<form name="armor" method="post" action="../index.php?pages=armorshop2">
<table width="317" border="1" cellpadding="0" cellspacing="0" class="txt">
<tr>
<td width="21"></td>
<td width="123">Armor Name</td>
<td width="60">Att/Def</td>
<td width="113">Cost</td>
</tr>
<tr>
<td><?php
include 'db.php';
$gold = $row['gold'];
if ($gold >= 50) {
?>
<input type="radio" name="armor" value="Shield" <?php echo isset($armor) ? armor == "Shield" ? 'checked="checked"' : '' : ''; ?>>
<?php
}else{
echo '&nbsp;';
}
?></td>
<td>Shield</td>
<td>0/5</td>
<td>5 gold</td>
</tr>
<tr>
<td><?php
include 'db.php';
$gold = $row['gold'];
if ($gold >= 100) {
?>
<input type="radio" name="armor" value="Leather Helmet" <?php echo isset($armor) ? armor == "Leather Helmet" ? 'checked="checked"' : '' : ''; ?>>
<?php
}else{
echo'&nbsp;';
}
?></td>
<td>Leather Helmet</td>
<td>0/5</td>
<td>10 gold</td>
</tr>
<tr>
<td><?php
include 'db.php';
$gold = $row['gold'];
if ($gold >= 100) {
?>
<input type="radio" name="armor" value="Body Guard" <?php echo isset($armor) ? armor == "Body Guard" ? 'checked="checked"' : '' : ''; ?>>
<?php
}else{
echo'&nbsp;';
}
?></td>
<td>Body Guard</td>
<td>0/10</td>
<td>25 gold</td>
</tr>
<tr>
<td><?php
include 'db.php';
$gold = $row['gold'];
if ($gold >= 100) {
?>
<input type="radio" name="armor" value="Steel Armor" <?php echo isset($armor) ? armor == "Steel Armor" ? 'checked="checked"' : '' : ''; ?>>
<?php
}else{
echo'&nbsp;';
}
?></td>
<td>Steel Armor</td>
<td>0/25</td>
<td>50 gold</td>
</tr>
</table>
<table width="317" border="1" cellpadding="0" cellspacing="0" class="txt">
<tr>
<td><div align="left"><input name="submit" type="submit" value="Submit"></div></td>
</tr>
</form>
</font>
</center>
<?php
}
?>

Thank you
Smackie

Fou-Lu
09-03-2005, 07:05 PM
Ah. #1 don't ever trust any type of form field as valid. Always instead requery the data required.
Now, with that out of the way your alterations are simple. Your UPDATE function is supplying inaccurate values, these are the methods used for an INSERT function. Update requires the new values set:
UPDATE `table` SET `item` = 'new_value', `item2` = 'new_value2' WHERE condition
Thats it.
BTW, send your header after the update function has completed.

Smackie
09-03-2005, 07:11 PM
so make the update part like this?

UPDATE `users` SET `Shield` = 'armor', `Leather Helmet` = 'armor' WHERE condition

Im not really following you that well :confused: been up for 3 1/2 days just trying to get this script to work right

Fou-Lu
09-03-2005, 07:29 PM
Yes, like that. Your values will probably be variables however:
UPDATE `users` SET `Shield` = 'armor', `Leather Helmet` = 'armor' WHERE condition
would become:


$sql = "UPDATE `users` SET `shield` = '" . $shield . "', `helmet` = '" . $helmet . '" WHERE `user_id` = '" . $user_id . "'";


I first got into php by developing an addon to the vBulletin that was an RPG system as well. I probably still have the system somewhere here so I can use it for reference, but I'm almost afraid to seek it out due to my initial poor coding techniques; I'm afraid to see what it looks like as I know for sure that it would take me hours to decypher. It was sweet though, had 1000+ items available for purchase and a fully integrated turned based battle system. I was at the time working on an upgrade to a flash based system with fully implimented user environment. I never completed that...

Now, from your example here, your radio buttons tell me that you can only select one of these items to equip as your armor. The bigger problem is that its hardcoded, especially with the gold attached to it. You cannot control this method without hardcoding it into the updating files.
So, for your example, you can only update the armor section for the user, as no other variables are passed. You will need to add something like this to your update as well:


if ($armor == 'Shield')
{
$cost = 5;
}
else if ($armor == 'Leather Helmet')
{
$cost = 10;
}
else if ($armor == 'Body Guard')
{
$cost = 25;
}
else if ($armor == 'Steel Armor')
{
$cost = 50;
}

// Now, I believe you can combine the value of your `gold` field so long as its stored as an integer value:
$sql = "UPDATE `user` SET `armor` = '" . $armor . "', gold = gold - " . $cost . " WHERE user_id = '" . $user_id . "'"; // Where your user_id is of course the purchasing user.

Hope that helps you somewhat!


Oh, BTW, you're going to want to change these to a numerical referencing ID as well. It will save you tonns of time in the long run.

Smackie
09-03-2005, 07:40 PM
Thank you.. You ever thought about making more php turn based game and other type of php game? i was thinking about putting together a big php game but i want someone that can help build it. this game im building right now is just something i can just set up and be bored on when there is not much else to do and i wanted always to build a game and i guess this what i picked out but if you want to help me out more contact me at Smackie@streetfight.webcom-online.org, Smackie@hauntedgraveyard.net, or Smackie@webcom-online.org



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum