Jump to content

Adding two variables and assigning to a hidden field


thechris

Recommended Posts

Title says it all. I have a hidden feild and would like to assign it a integer based of two numbers from different recordsets here is my code for what i tried

 

<input type="hidden" name="player1_attack" value="<?php $row_player1['attack'] + $row_player1_character['attack']; ?>"/>

Link to comment
Share on other sites

it works when it is not in the hidden field and displays the correct number but when it is in the hidden field the database just reads it as zero.

 

I think that it is just not updating the table correctly because the forumla works when its out of the hidden field and i can put it in a variable and it'll print out the variable correctly but when I try to put the variable into the table it still reads as a zero.  ???

Link to comment
Share on other sites

ok i'm at a complete loss here is all the code for my page that i'm working on where it updates the form

 

<?php require_once('../Connections/conn_users.php'); ?>
<?php
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{
  $theValue = (!get_magic_quotes_gpc()) ? addslashes($theValue) : $theValue;

  switch ($theType) {
    case "text":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;    
    case "long":
    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case "double":
      $theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL";
      break;
    case "date":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;
    case "defined":
      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
      break;
  }
  return $theValue;
}

$editFormAction = $_SERVER['PHP_SELF'];
if (isset($_SERVER['QUERY_STRING'])) {
  $editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
}

if ((isset($_POST["MM_update"])) && ($_POST["MM_update"] == "form1")) {
  $updateSQL = sprintf("UPDATE battles SET player1_attack=%s, player1_defense=%s, player1_stamina=%s, player1_dexterity=%s, player1_special=%s, player1_health=%s, player2_attack=%s, player2_defense=%s, player2_stamina=%s, player2_dexterity=%s, player2_special=%s, player2_health=%s WHERE battle_id=%s",
                       GetSQLValueString($_POST['player1_attack'], "int"),
                       GetSQLValueString($_POST['player1_defense'], "int"),
                       GetSQLValueString($_POST['player1_stamina'], "int"),
                       GetSQLValueString($_POST['player1_dexterity'], "int"),
                       GetSQLValueString($_POST['player1_special'], "int"),
                       GetSQLValueString($_POST['player1_health'], "int"),
                       GetSQLValueString($_POST['player2_attack'], "int"),
                       GetSQLValueString($_POST['player2_defense'], "int"),
                       GetSQLValueString($_POST['player2_stamina'], "int"),
                       GetSQLValueString($_POST['player2_dexterity'], "int"),
                       GetSQLValueString($_POST['player2_special'], "int"),
                       GetSQLValueString($_POST['player2_health'], "int"),
                       GetSQLValueString($_POST['battle_id'], "int"));

  mysql_select_db($database_conn_users, $conn_users);
  $Result1 = mysql_query($updateSQL, $conn_users) or die(mysql_error());
}


mysql_select_db($database_conn_users, $conn_users);
$query_battle = "SELECT * FROM battles ORDER BY battle_id DESC";
$battle = mysql_query($query_battle, $conn_users) or die(mysql_error());
$row_battle = mysql_fetch_assoc($battle);
$totalRows_battle = mysql_num_rows($battle);

$_POST['player_1'] = $row_battle['player_1'];
$_POST['player_2'] = $row_battle['player_2'];
$_POST['player1_char'] = $row_battle['player1_char'];
$_POST['player2_char'] = $row_battle['player2_char'];

$colname_player1 = "-1";
if (isset($_POST['player_1'])) {
  $colname_player1 = (get_magic_quotes_gpc()) ? $_POST['player_1'] : addslashes($_POST['player_1']);
}
mysql_select_db($database_conn_users, $conn_users);
$query_player1 = sprintf("SELECT * FROM system_users WHERE username = '%s'", $colname_player1);
$player1 = mysql_query($query_player1, $conn_users) or die(mysql_error());
$row_player1 = mysql_fetch_assoc($player1);
$totalRows_player1 = mysql_num_rows($player1);

$colname_player2 = "-1";
if (isset($_POST['player_2'])) {
  $colname_player2 = (get_magic_quotes_gpc()) ? $_POST['player_2'] : addslashes($_POST['player_2']);
}
mysql_select_db($database_conn_users, $conn_users);
$query_player2 = sprintf("SELECT * FROM system_users WHERE username = '%s'", $colname_player2);
$player2 = mysql_query($query_player2, $conn_users) or die(mysql_error());
$row_player2 = mysql_fetch_assoc($player2);
$totalRows_player2 = mysql_num_rows($player2);

$colname_player1_character = "-1";
if (isset($_POST['player1_char'])) {
  $colname_player1_character = (get_magic_quotes_gpc()) ? $_POST['player1_char'] : addslashes($_POST['player1_char']);
}
mysql_select_db($database_conn_users, $conn_users);
$query_player1_character = sprintf("SELECT * FROM characters WHERE char_name = '%s'", $colname_player1_character);
$player1_character = mysql_query($query_player1_character, $conn_users) or die(mysql_error());
$row_player1_character = mysql_fetch_assoc($player1_character);
$totalRows_player1_character = mysql_num_rows($player1_character);

$colname_player2_character = "-1";
if (isset($_POST['player2_char'])) {
  $colname_player2_character = (get_magic_quotes_gpc()) ? $_POST['player2_char'] : addslashes($_POST['player2_char']);
}
mysql_select_db($database_conn_users, $conn_users);
$query_player2_character = sprintf("SELECT * FROM characters WHERE char_name = '%s'", $colname_player2_character);
$player2_character = mysql_query($query_player2_character, $conn_users) or die(mysql_error());
$row_player2_character = mysql_fetch_assoc($player2_character);
$totalRows_player2_character = mysql_num_rows($player2_character);

$player1_attack = $row_player1['attack'] + $row_player1_character['attack'];
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Cosplay Combat</title>
<meta http-equiv="Content-Type"
content="text/html; charset=iso-8859-1" />
<meta name="Robots" content="NOINDEX" />
<meta http-equiv="PRAGMA" content="NO-CACHE" /><!--
  Web Site: www.ssi-developer.net
  Comments: Copyright 2003 www.ssi-developer.net
  Licence:  Creative Commons - Non-commercial Share-Alike
-->
<link rel="stylesheet" type="text/css"
href="../2c-hd-lc-static-layout.css" />
<link rel="stylesheet" type="text/css"
href="../2c-hd-lc-static-presentation.css" />
</head>
<body>
<!-- Header -->
<div id="hdr">Cosplay Combat </div>
<!-- left column -->
<div id="lh-col">
<ul>
  <li><a href="../index.php">Index</a></li>
  <li><a href="../gallery.php">Gallery</a></li>
  <li><a href="../rankings.php">Rankings</a></li>
  <li><a href="../rules.php">Rules</a></li>
  <li><a href="../request.php">Request Event</a></li>
</ul> 
</div>
<!-- end of left column -->
<!-- right column -->
<div id="rh-col">
<div id="info"><a href="../login.php">Log In</a> | <a>Log Out</a> | <a href="../registration.php">Register </a></div>
<form id="form1" name="form1" method="POST" action="<?php echo $editFormAction; ?>">
  <table width="648" border="1">
    <tr>
      <td width="300">Player 1 Name </td>
      <td width="332"><?php echo $row_battle['player_1']; ?></td>
    </tr>
    <tr>
      <td>Player 1 Character </td>
      <td><?php echo $row_battle['player1_char']; ?></td>
    </tr>

    <tr>
      <td> </td>
      <td> </td>
    </tr>
    <tr>
      <td>Player 2 Name </td>
      <td><?php echo $row_battle['player_2']; ?></td>
    </tr>
    <tr>
      <td>Player 2 Character </td>
      <td><?php echo $row_battle['player2_char']; ?></td>
    </tr>
    <tr>
      <td><a href="fight.php?battle_id=<?php echo $row_battle['battle_id']; ?>">Fight</a></td>
      <td><input type="submit" name="Submit" value="Submit" /></td>
    </tr>
  </table>
  <p>
  	<input type="hidden" name="battle_id" value="<?php $row_battle['battle_id']; ?>" />
   	<input type="hidden" name="player1_attack" value="<?php echo ($row_player1['attack'] + $row_player1_character['attack']); ?>"/>
<input type="hidden" name="player1_defense" value="<?php echo ($row_player1['defense'] + $row_player1_character['defense']); ?>"/>
<input type="hidden" name="player1_stamina" value="<?php echo($row_player1['stamina'] + $row_player1_character['stamina']); ?>"/>
<input type="hidden" name="player1_dexterity" value="<?php echo($row_player1['dexterity'] + $row_player1_character['dexterity']); ?>"/>
<input type="hidden" name="player1_special" value="<?php echo($row_player1['special'] + $row_player1_character['special']); ?>"/>
<input type="hidden" name="player1_health" value="<?php echo ($row_player1['health']); ?>"/>

<input type="hidden" name="player2_attack" value="<?php echo($row_player2['attack'] + $row_player2_character['attack']); ?>"/>
<input type="hidden" name="player2_defense" value="<?php echo($row_player2['defense'] + $row_player2_character['defense']); ?>"/>
<input type="hidden" name="player2_stamina" value="<?php echo($row_player2['stamina'] + $row_player2_character['stamina']); ?>"/>
<input type="hidden" name="player2_dexterity" value="<?php echo($row_player2['dexterity'] + $row_player2_character['dexterity']); ?>"/>
<input type="hidden" name="player2_special" value="<?php echo($row_player2['special'] + $row_player2_character['special']); ?>"/>
<input type="hidden" name="player2_health" value="<?php echo($row_player2['health']); ?>" />
  </p>
  <input type="hidden" name="MM_update" value="form1">
</form>
<p></p>
</div>
<!-- end of right column -->

</body>
</html>
<?php
mysql_free_result($battle);

mysql_free_result($player1);

mysql_free_result($player2);

mysql_free_result($player1_character);

mysql_free_result($player2_character);
?>

Link to comment
Share on other sites

On the page with the hidden field on it, do a view source and check the hidden value is set to something.

 

Why not pass player 1 ID and player 2 ID instead of all that crap. Then when the form is processed, look all these details up in the database.

 

If not, liberal use of:

 

print_r( $_POST );

 

is useful for debugging.

 

If you are doing this for money, then invest in: http://www.nusphere.com/, it is the roxor

 

monk.e.boy

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.