-1

Im worink with the 'shop-app' and I want to get error response when a-b<0 , because if user have for example 10$ and an item cost 20$, he cant buy it, but in my app it look that: 10-20=-10 Here is php script:

<?php
if($_SERVER['REQUEST_METHOD']=='POST'){

include 'DatabaseConfig.php';

$con = mysqli_connect($HostName,$HostUser,$HostPass,$DatabaseName);

$id = $_POST['UserID'];
$S_cash = $_POST['Cash'];
$S_substract = $_POST['Substract'];



$Sql_Query = "UPDATE user SET cash= '$S_cash' - '$S_substract' WHERE id = $id";

if(mysqli_query($con,$Sql_Query))
{
echo 'Success!';
}
else
if('$S_cash' - '$S_substract' < 0)
///this is what ive tried
{
 echo 'error';
}
}
mysqli_close($con);
?>
Master
  • 37
  • 9
  • The problem is you are working with strings not numbers. Convert your strings into numbers and do the math. Also, try to read on how to properly use user input without SQL injection. – Ibu May 11 '18 at 23:04
  • if you have a real shop, please please please read up on prepared statement, escaping and/or sanitizing input, sql injections (as linked in the related links) – Jakumi May 11 '18 at 23:55

2 Answers2

1

your putting '$S_cash' - '$S_substract' in single quotes and because of that php is interpreting it as a literal $S_cash - $S_substract

instead of the numbers they stand for.

try using double quotes as in

"$S_cash" - "$S_substract"

or no quotes at all as in

$S_cash - $S_substract

also consider using prepared statements

electricjelly
  • 366
  • 1
  • 2
  • 14
1

I don't know why you are using

if('$S_cash' - '$S_substract' < 0)

this. normally it would be

if($S_cash - $S_substract < 0)

plus, looking at your code. You are checking whether your response is correct, by outputing success here

if(mysqli_query($con,$Sql_Query))
{
echo 'Success!';
}

and if it is not, then you are using your if statement

else
if('$S_cash' - '$S_substract' < 0)
///this is what ive tried
{
 echo 'error';
}

I don't know if you mean it or it's error. Maybe you want to get rid of else statement

MadLordDev
  • 260
  • 2
  • 12