4 Replies - 511 Views - Last Post: 15 November 2009 - 10:20 PM Rate Topic: -----

#1 nick1200  Icon User is offline

  • Php Coder
  • member icon

Reputation: -19
  • View blog
  • Posts: 922
  • Joined: 21-March 09

taking 10 away from a column in the db

Post icon  Posted 15 November 2009 - 10:37 AM

on my db i have a table called battle and in that table i have 4 coullums
username,hp,ehp and inbattle
when a user goes to the page im coding it adds them intothe table it makes hp and ehp 100 and makes in battle 1

now i have just added a drop down menu with moves and a button
now i want the ehp to go down
but i am getting a white blank page heres my code


<?php	
if (!ini_get('display_errors')) {
	ini_set('display_errors', 1);
}

	include('include/session.php'); 	
	
	if (!$session->logged_in) {
		header("Location: login.php");
	}

	// this checks and increments the battle counter accordingly
	$session->checkPageJumping(true);
	
	// all the stuff at the top of the page
	require('sections/new_pagehead.php');

	// all the stuff on the left column of the page
	include_once('sections/new_leftbar.php');
?>

<?php

$result = mysql_query("SELECT * FROM battle WHERE username='".$_SESSION['username']."'");
$num_rows = mysql_num_rows($result);
if($num_rows < 1) {
mysql_query("INSERT INTO battle (username,hp,ehp, inbattle) VALUES ('" . $_SESSION['username'] . "','100','100','1')") or die(mysql_error());
}

?> 


<link rel="shortcut icon" href="/favicon2.gif" type="image/x-icon" /> 

	<style type="text/css">
<!--
.style3 {font-size: 18px}
-->
	</style>
	<div id="mid_section">
		<div class="mid_section_inner">
		
			<div class="mid_box">
					<div class="mid_box_title">Your Pokemon</div>
					<?php
						// show the user their pokemon
						include_once('pokemon_level.php');
						// end file include
					?>
			</div>
			
		  <div class="mid_box">
				<div class="mid_box_title">Current Battle</div>
				
				<table width="200" border="1">
				  <tr>
					<td><span class="style3">Your Hp </span></td>
					<td><span class="style3">There hp </span></td>
				  </tr>
				  <tr>
					<td><?php

$connect = mysql_connect("localhost", "pokemont_shadow", "mypass");
$database = mysql_select_db("pokemont_pokemon", $connect);

$sql = "SELECT hp from battle WHERE username='".$_SESSION['username']."'";
$res = mysql_query($sql)  or die('Invalid query: ' . mysql_error());
$val = mysql_fetch_array($res);
	  
echo $val['hp'];


?></td>
					<td><?php

$connect = mysql_connect("localhost", "pokemont_shadow", "mypass");
$database = mysql_select_db("pokemont_pokemon", $connect);

$sql = "SELECT ehp from battle WHERE username='".$_SESSION['username']."'";
$res = mysql_query($sql)  or die('Invalid query: ' . mysql_error());
$val = mysql_fetch_array($res);
	  
echo $val['ehp'];


?></td>
				  </tr>
				</table>
				<p><?php 


$sql = mysql_query("SELECT username FROM battle WHERE username='".$_SESSION['username']."'");
$num = mysql_num_rows($sql);
echo $num  
 

?>&nbsp;				</p>
				<p>&nbsp;</p>
				<p><img src="/pokemon/<?php echo $pokemon->{ 'pok' . $_SESSION['current_pokemon']}['pokePIC']; ?>" width="107" height="102" />
				</p>
				<div align="right"><img src="pokemon/Shinnyregirock.png" width="80" height="80"></div>
<form name="myform" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<div align="center">
  <p>
	<select name="mydropdown">
	  <option value="Move1">Move1</option>
	  <option value="Move2">Move2</option>
	  <option value="Move3">Move3</option>
	</select>
</p>
  <p>
	<input name="submit" type="submit" />
</p>
</div>
</form>
<?php
if (isset($_POST['teamname']) && $_POST['teamname'] != "") {
	if ($results->inbattle == 1) {
		$sql="UPDATE battle SET hp='"-10"' WHERE username='" . $_SESSION['username'] . "'";
		?>
		&nbsp;</p>
			<table width="200" border="1">
			  <tr>
				<td>&nbsp;</td>
			  </tr>
			</table>
		  
		  </div>
		</div>				
	</div>
<?php
	
	// all the stuff on the right column of the page
		include_once('sections/new_rightbar.php');
		
	// close everything down with a footer
	require('sections/footer.php');
?>




This add the user to the table which works


$result = mysql_query("SELECT * FROM battle WHERE username='".$_SESSION['username']."'");
$num_rows = mysql_num_rows($result);
if($num_rows < 1) {
mysql_query("INSERT INTO battle (username,hp,ehp, inbattle) VALUES ('" . $_SESSION['username'] . "','100','100','1')") or die(mysql_error());
}




Then i use this to take so much away from the ehp

<form name="myform" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<div align="center">
  <p>
	<select name="mydropdown">
	  <option value="Move1">Move1</option>
	  <option value="Move2">Move2</option>
	  <option value="Move3">Move3</option>
	</select>
</p>
  <p>
	<input name="submit" type="submit" />
</p>
</div>
</form>
<?php
if (isset($_POST['teamname']) && $_POST['teamname'] != "") {
	if ($results->inbattle == 1) {
		$sql="UPDATE battle SET hp='"-10"' WHERE username='" . $_SESSION['username'] . "'";
		?>



but im getting a blank page

Is This A Good Question/Topic? 0
  • +

Replies To: taking 10 away from a column in the db

#2 RudiVisser  Icon User is offline

  • .. does not guess solutions
  • member icon

Reputation: 1004
  • View blog
  • Posts: 3,562
  • Joined: 05-June 09

Re: taking 10 away from a column in the db

Posted 15 November 2009 - 11:57 AM

To add/subtract in MySQL you simply do UPDATE `table` SET `column` = `column` - 10 WHERE ... LIMIT ...;
Was This Post Helpful? 0
  • +
  • -

#3 nick1200  Icon User is offline

  • Php Coder
  • member icon

Reputation: -19
  • View blog
  • Posts: 922
  • Joined: 21-March 09

Re: taking 10 away from a column in the db

Posted 15 November 2009 - 12:09 PM

View PostRudiVisser, on 15 Nov, 2009 - 10:57 AM, said:

To add/subtract in MySQL you simply do UPDATE `table` SET `column` = `column` - 10 WHERE ... LIMIT ...;


ya but i need to make it do it when button is pressed lol
and plus i dont know how im gonna make it take ten of the ehp then 10 of the hp then 10 of the ehp and so on
so different things on clicks
Was This Post Helpful? 0
  • +
  • -

#4 felixtgomezjr  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 3
  • View blog
  • Posts: 68
  • Joined: 04-November 09

Re: taking 10 away from a column in the db

Posted 15 November 2009 - 08:59 PM

Do this on your update

$sql="UPDATE battle SET hp='"-10"' WHERE username='" . $_SESSION['username'] . "'";


$sql = "UPDATE battle SET hp=hp-10 WHERE username='$_SESSION[username]'";

btw, much better if you will put the session variable into a local variable.
Was This Post Helpful? 0
  • +
  • -

#5 noorahmad  Icon User is offline

  • Untitled
  • member icon

Reputation: 209
  • View blog
  • Posts: 2,290
  • Joined: 12-March 09

Re: taking 10 away from a column in the db

Posted 15 November 2009 - 10:20 PM

in one form you can use more the one button just change the name of buttons and when you are calling your php script just user name of the button examle:
if(isset($_POST['insert'])){
// insert code...
}elseif(isset($_POST['update'])){
// update code...
}


:)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1