PHP not doing what I'm telling it to do...

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Red Blaze
Forum Commoner
Posts: 40
Joined: Mon Mar 27, 2006 3:45 pm

PHP not doing what I'm telling it to do...

Post by Red Blaze »

I'm pretty confused. This is the piece of code I'm using:

Code: Select all

if($_GET['remove']){
$removeid = $_GET['remove'];
mysql_query('DELETE FROM items WHERE itemname = "'. $removeid .'"');
}

$total_query = 'SELECT * FROM items WHERE cartid = "'. $cartid .'"';
$result = mysql_query($total_query, $prophot) or die(mysql_error());
$num = mysql_num_rows($result);
$i=0;
$grandtotal = 0;
while ($i < $num) {
$subtotal=mysql_result($result,$i,"subtotal");
$id=mysql_result($result,$i,"itemid");
$quantity=mysql_result($result,$i,"quantity");
$original=mysql_result($result,$i,"original");

$grandtotal += $subtotal;
			  $i++;
}
When a user clicks a remove link with "?remove=$itemname" in the end, I want the specified row to be removed from the database (the entire row) depending on the itemname. After it does that, I also want it to do calculations of the remaining rows. However, it's not doing that; it's only ignoring it. I have to click on an update button. I'd rather it be automated, rather than click 2 buttons to get the right calculations. Help would be appriciated.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

mayhaps you have a syntax error in your deletion query?
Red Blaze
Forum Commoner
Posts: 40
Joined: Mon Mar 27, 2006 3:45 pm

Post by Red Blaze »

feyd wrote:mayhaps you have a syntax error in your deletion query?
Now that you mention error, it's not the delete function that's wrong. $cartid is from a form, and the remove link is a text link, not a submit button. Sorry I over looked that little detail... been working on this problem since yesterday and barely realized it now. x_X;
Will the following work?

update.php?remove=$itemid&cartid=$cartid

And then do:
$_GET['remove']
$_GET['cartid']
User avatar
a94060
Forum Regular
Posts: 543
Joined: Fri Feb 10, 2006 4:53 pm

Post by a94060 »

Red Blaze wrote:
feyd wrote:mayhaps you have a syntax error in your deletion query?
Now that you mention error, it's not the delete function that's wrong. $cartid is from a form, and the remove link is a text link, not a submit button. Sorry I over looked that little detail... been working on this problem since yesterday and barely realized it now. x_X;
Will the following work?

update.php?remove=$itemid&cartid=$cartid

And then do:
$_GET['remove']
$_GET['cartid']
make sure you conceante the vars in the statment. has PHP spit out any errors?
Red Blaze
Forum Commoner
Posts: 40
Joined: Mon Mar 27, 2006 3:45 pm

Post by Red Blaze »

a94060 wrote:
Red Blaze wrote:
feyd wrote:mayhaps you have a syntax error in your deletion query?
Now that you mention error, it's not the delete function that's wrong. $cartid is from a form, and the remove link is a text link, not a submit button. Sorry I over looked that little detail... been working on this problem since yesterday and barely realized it now. x_X;
Will the following work?

update.php?remove=$itemid&cartid=$cartid

And then do:
$_GET['remove']
$_GET['cartid']
make sure you conceante the vars in the statment. has PHP spit out any errors?
Nope, just ignored it completly. It removed the row correctly, but didn't do any calculations.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Are you sure that there are rows being returned from your select query? If num_rows returns 0 your whole while loop does nothing...

Code: Select all

if(isset($_GET['remove'])){
	$removeid = $_GET['remove'];
	mysql_query("DELETE FROM items WHERE itemname = $removeid");
}

$total_query = "SELECT * FROM items WHERE cartid = $cartid";
$result = mysql_query($total_query, $prophot) or die(mysql_error());
$num = mysql_num_rows($result);
//testing
echo $num;

$i=0;
$grandtotal = 0;
while ($i < $num) {
	// If $num is 0 then none of this happens
	// Have you checked to make sure there is a number for $num?
	$subtotal=mysql_result($result,$i,"subtotal");
	$id=mysql_result($result,$i,"itemid");
	$quantity=mysql_result($result,$i,"quantity");
	$original=mysql_result($result,$i,"original");

	$grandtotal += $subtotal;
    $i++;
}
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

Note:

Code: Select all

while ($row = mysql_fetch_assoc($result)) {
   // ...
   // other columns accessible through $row array
   // ...
   $grandtotal += $row['subtotal'];   
}
This way, you don't have to bother with multiple mysql_result calls.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

That's what I'm talkin' 'bout baby! Fast, compact, clean code. Sweet.
Red Blaze
Forum Commoner
Posts: 40
Joined: Mon Mar 27, 2006 3:45 pm

Post by Red Blaze »

Thanks for the tips. I outta bookmark all my threads. ^_^;
I got it now, thank you all so very much. I really appreciate your help. =)
Post Reply