Page 1 of 1

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

Posted: Tue Apr 04, 2006 11:16 am
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.

Posted: Tue Apr 04, 2006 11:31 am
by feyd
mayhaps you have a syntax error in your deletion query?

Posted: Tue Apr 04, 2006 11:37 am
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']

Posted: Tue Apr 04, 2006 12:33 pm
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?

Posted: Tue Apr 04, 2006 12:37 pm
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.

Posted: Tue Apr 04, 2006 1:06 pm
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++;
}

Posted: Tue Apr 04, 2006 1:35 pm
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.

Posted: Tue Apr 04, 2006 1:44 pm
by RobertGonzalez
That's what I'm talkin' 'bout baby! Fast, compact, clean code. Sweet.

Posted: Tue Apr 04, 2006 2:25 pm
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. =)