Jump to content

Subtraction Problem


OctoShock

Recommended Posts

Recruit.php

<?php 
require('loginonly.php');
require('db.php');
$array = mysql_query("SELECT * FROM $tbl_name WHERE id='$_SESSION[id]'");
$row = mysql_fetch_array($array);
//retrieve info
$username = $row['username'];
$soldiers = $row['soldiers'];
$money = $row['money'];
$citizens = $row['population'];
$pop = $soldiers + $citizens;
$cost = round($pop / 3);
$max = round(((($citizens / 2) - $soldiers) - 1));
$maxsell = $soldiers;
?>
<br><br>
<h1>Soldier Recruit</h1>
<p>Recruit soldiers to fight for your colony, defend it, and police citizen geckos.</p>

<center><table border="1" cellpadding="4" width="400" bgcolor="#E6FFCC">
<tr>
<td>Total Population</td>
<td><div align="right"><?php echo $pop; ?></div></td>
</tr>
<tr>
<td>Current Soldier Count</td>
<td><div align="right"><?php echo $soldiers; ?></div></td>
</tr>
<tr>
<td>Current Citizen Count</td>
<td><div align="right"><?php echo $citizens; ?></div></td>
</tr>
<tr>
<td>Max Soldier Sale</td>
<td><div align="right"><?php echo $maxsell; ?></div></td>
</tr>
<tr>
<td>Max Soldier Purchase</td>
<td><div align="right"><?php echo $max; ?></div></td>
</tr>
<tr>
<td>Total Money</td>
<td><div align="right"><?php echo $money; ?></div></td>
</tr>
<tr>
<td>Cost Per Soldier</td>
<td><div align="right"><?php echo $cost; ?></div></td>
</tr>
</table>
<table border="1" cellpadding="4" width="400" bgcolor="#E6FFCC">
<tr>
<td>Buy/Sell Soldiers</td>
<td><form name="input" action="index.php?id=militarygeckos" method="post"><div align="left"><input type="text" name="amount" /></div></td></tr>
</table>
<input type="submit" value="Exchange" name="submit"></form>
</center>
<p>When buying soldiers, enter a positive value. (Example: 10) When selling soldiers, enter a negative value. (Example: -10)</p>
<br>
<br>
<br>

 

militarygeckos.php

<?php
require('loginonly.php');
require('db.php');
$array = mysql_query("SELECT * FROM $tbl_name WHERE id='$_SESSION[id]'");
$row = mysql_fetch_array($array);
//20 citizens for 1 level. Citizens / 2 - Soldiers = max. Cost = total population / 3
$level = $row['level'];
$money = $row['money'];
$soldiers = $row['soldiers'];
$citizens = $row['population'];
$pop = $citizens + soldiers;
$max = round((($citizens / 2) - $soldiers));
$cost = round($pop / 3);
$amount = $_POST['amount'];
$amount = strip_tags($amount);
$amount = round($amount);
$ttlamount = round($cost * $amount);
if(!is_numeric($amount))
{
$error .= "<br><p>You entered an invalid character.</p><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br>";
exit($error);
} elseif($ttlamount > $money)
{
$error .= "<br><p>You don't have enough money to perform that exchange.</p><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br>";
exit($error);
} elseif($amount < 0)
{
if($amount < (0 - $soldiers)) {
$error .= "<br><p>You don't have that many soldiers to sell!</p><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br>";
exit($error);
}
} elseif($amount > $max)
{
$error .= "<br><p>You cannot buy that many soldiers at this time.</p><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br>";
exit($error);
} else {
$new_money = $money - $ttlamount;
$new_soldiers = $soldiers + $amount;
mysql_query("UPDATE members SET money='$new_money', soldiers='$new_soldiers' WHERE id='$_SESSION[id]'");
mysql_close($con);
$good = "<p>You have successfully purchased $amount soldiers at $amount each. Your total was $ttlamount Geckoleons and you now have $new_soldiers soldiers.</p><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br>";
$good2 = "<p>You have successfully sold $amount soldiers at $amount each. Your total was $ttlamount Geckoleons and you now have $new_soldiers soldiers.</p><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br>";
if($amount > 0)
{
echo $good;
} else {
echo $good2;
}
}
?>

 

It's not working correctly when you enter a negative integer into the form. I'm not sure why, as far as I know all the math is right.

Link to comment
https://forums.phpfreaks.com/topic/206841-subtraction-problem/
Share on other sites

Where are the <form> tags for the first "Soldier Recruit" form?

And I only see an opening <form> tag for the "Buy/Sell Soldiers" form.

 

What do you mean it's not working correctly when you enter a negative integer into the form.

What happens? Does it treat the negative integer as a positive integer?

Link to comment
https://forums.phpfreaks.com/topic/206841-subtraction-problem/#findComment-1084336
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.