How to Subtract numbers from tables properly

hello… i have a question to a problem i’ve been trying to figure out for a month.

i’m working on a “bidding” script that i can’t get to work.

i set mc_gross to 4.00 from the paypal ipn. now once “[mc_gross]” reaches ‘0.00’ it’s meant to

go to “pay.html”, but instead it keeps saying “bid placed” when there is no money left in mc_gross.

please tell me what i’m doing wrong.

thank you in advance!

members table:

<?php mysql_connect("localhost", "user", "pass") or die(mysql_error()); mysql_Select_db("mydatabase") or die(mysql_error()); mysql_query("CREATE TABLE members( id INT NOT NULL auto_increment, PRIMARY KEY(id), first_name VARCHAR(100), last_name VARCHAR(100), username VARCHAR(100), password VARCHAR(100), payer_email varchar(100), mc_gross FLOAT(6,2) )")or die(mysql_error()); echo "table created!"; ?>

bids table:

<?php mysql_connect("localhost", "user", "pass") or die(mysql_error()); mysql_Select_db("mydatabase") or die(mysql_error()); mysql_query("CREATE TABLE bids( id INT NOT NULL auto_increment, PRIMARY KEY(id), highbidder VARCHAR(100), payer_email varchar(100), seller VARCHAR(100), seller_email VARCHAR(100), weight varchar(100), composition varchar(100), color VARCHAR(100), mc_gross FLOAT(6,2) )") or die(mysql_error()); echo "table created!"; ?>

login script:

<?php session_start(); $host="localhost"; $username="user"; $password="pass"; $db_name="mydatabase"; mysql_connect("$host", "$username", "$password")or die("cannot connect"); mysql_select_db("$db_name")or die("cannot select DB"); $_SESSION['id'] = $_GET['id']; $_SESSION['payer_email'] = $_GET['payer_email']; $_SESSION['mc_gross'] = $_GET['mc_gross']; session_register("id"); session_register("payer_email"); session_register("mc_gross"); session_register("username"); session_register("password"); $_SESSION['mc_gross'] = $_POST['mc_gross']; $_SESSION['username'] = $_POST['username']; $_SESSION['password'] = $_POST['password']; $_SESSION['payer_email'] = $_POST['payer_email']; $_SESSION['mc_gross'] = $_POST['mc_gross']; $mc_gross = $_POST['mc_gross']; $username = $_POST['username']; $password = $_POST['password']; $payer_email = $_POST['payer_email']; $_SESSION['mc_gross'] = $mc_gross; $_SESSION['username'] = $username; $_SESSION['password'] = $password; $_SESSION['payer_email'] = $payer_email; $data=mysql_query("SELECT * FROM members WHERE username = '$username' AND password = '$password'") or die(mysql_error()); if($info=mysql_fetch_array($data)){ print $info['username']; print "
"; print $info['password']; print "
"; print $info['payer_email']; print "
"; print $info['mc_gross']; print "
"; print 'Go to Auctions'; print "
"; print 'Post an Auction'; print "
"; }else{ header('Location:failed.html'); } $data2 = mysql_query("SELECT * FROM bids WHERE payer_email = '$_SESSION[payer_email]'") or die(mysql_error()); while($info = mysql_fetch_array($data2)){ print "These are your Bids!"; print "
"; print $info['highbidder']; print "
"; print $info['payer_email']; print "
"; print $info['seller']; print "
"; print $info['seller_email']; print "
"; print $info['weight']; print "
"; print $info['composition']; print "
"; print $info['color']; print "
"; print "
"; } ?>

this next script uses the paypal IPN to load the value into “[mc_gross]”… the value being $4.00.

ipn.php:

<?php session_start(); $host="localhost"; $username="user"; $password="pass"; $db_name="mydatabase"; mysql_connect("$host", "$username", "$password")or die("cannot connect"); mysql_select_db("$db_name")or die("cannot select DB"); $mc_gross = $_POST['mc_gross']; $last_name = $_POST['last_name']; $payer_email = $_POST['payer_email']; mysql_query("UPDATE members SET mc_gross = '$mc_gross' WHERE last_name = '$last_name' AND payer_email = '$payer_email'") or die(mysql_error()); ?>

this next script is supposed to go to “pay.html” when the value in “[mc_gross]” decrements to 0.00… it’s not doing this for
some reason. it keeps on going to “bid placed”.

output-bids.php:

<?php session_start(); mysql_connect("localhost", "user", "pass") or die(mysql_error()); mysql_select_db("mydatabase") or die(mysql_error()); if($_SESSION[mc_gross] == 0.00){ header ('LOCATION:pay.html'); }else{ mysql_query("UPDATE members SET mc_gross = mc_gross - 1 WHERE id = '$_GET[id]'") or die(mysql_error()); print "bid placed"; } ?>

if the “else” statement is successful it’s supposed to decrement “[mc_gross]” by $1.00 each time… instead it keeps
saying “bid placed” even when “[mc_gross]” drops to 0.00.

i’ll add the rest of the code to output-bids.php when i can get the if/else working.

thank you for any help!

Hiya,

[php]if($_SESSION[mc_gross] == 0.00){
header (‘LOCATION:pay.html’);
}else{
mysql_query(“UPDATE members SET mc_gross = mc_gross - 1 WHERE id = ‘$_GET[id]’”) or die(mysql_error());
print “bid placed”;
}[/php]

Try checking mc_gross for 0 neat.
Calculate the new mc_gross outside of mysql_query.

[php]if($_SESSION[mc_gross] == 0){
header (‘LOCATION:pay.html’);
}else{
$newmc_gross = $_SESSION[mc_gross] - 1;
mysql_query(“UPDATE members SET mc_gross = ‘$newmc_gross’ WHERE id = ‘$_GET[id]’”) or die(mysql_error());
print “bid placed”;
}[/php]

Hope this works for you.

<?php session_start(); mysql_connect("localhost", "root", "bluemail") or die(mysql_error()); mysql_select_db("wackytoaster") or die(mysql_error()); if($_SESSION[mc_gross] > 0){ $newmc_gross = $_SESSION[mc_gross] - 1; mysql_query("UPDATE members SET mc_gross = '$newmc_gross' WHERE id = '$_GET[id]'") or die(mysql_error()); print "bid placed"; }else{ header ('LOCATION:pay.html'); } ?>

this isn’t working either… i have 3.00 in the [mc_gross] yet it still goes to “header” instead of “bid placed”

[php]$_SESSION[‘mc_gross’][/php]

Try that

still doesn’t work… i’ve turned off magic quotes in php cgi… don’t know if that makes a diff.

thanks for the help… :slight_smile:

Check what $_SESSION[mc_gross] actually ='s

[php]print “bid placed”;
}else{
print "bid not placed - ".$_SESSION[mc_gross];
}[/php]

it prints out -1 and then -2 and then -3 etc… every time i hit the “place bid” button.

Remove your session_register()'s because they are Deprecated.

I’m really not sure whats happening with your script. It doesn’t make sense.
I put together a mock situation on my local host.

test.php
Sets mc_gross session as 4 and forwards user to test1.php

test1.php
Provides link to place a bid.

test2.php
Checks mc_gross session. If > 0 removes 1 off mc_gross session. Updates mc_gross session which is essentially what you do with your database and returns to test1.php.
I can repeat this between test1 and test2 until mc_gross session is 0 and then says bid not placed.

I really dunno why yours is still updating the session beyond 0.

how did you setup your tables?

varchar float or int?

thanks!

Sponsor our Newsletter | Privacy Policy | Terms of Service