hyeteck Posted February 17, 2007 Share Posted February 17, 2007 hey guys, i have a piece of code from my program that is not functioning the way i want it to. It is not going into the "if" statement. $result = mysql_query("SELECT * from `products` WHERE pID='prodId' ") or die(mysql_error()); if($row = mysql_fetch_array($result)) { echo "$prodId"; if($prodMap > 0 || $prodMap!= NULL) $prodPrice = $prodMap; elseif($prodPrice < 10) $prodPrice = $prodPrice + ($prodPrice * .50); elseif($prodPrice >= 10 && $prodPrice < 50) $prodPrice = $prodPrice + ($prodPrice * .25); elseif($prodPrice >= 50 && $prodPrice < 150) $prodPrice = $prodPrice + ($prodPrice * .15); elseif($prodPrice >= 150 && $prodPrice < 400) $prodPrice = $prodPrice + ($prodPrice * .10); elseif($prodPrice >= 400 && $prodPrice < 100) $prodPrice = $prodPrice + ($prodPrice * .06); else $prodPrice = $prodPrice + ($prodPrice * .05); $prodPrice = floor($prodPrice); $prodPrice = $prodPrice + 0.99; $result = mysql_query("UPDATE `products` SET `pPrice`='$prodPrice', `pInStock`='$prodQuant' WHERE `pID`='$prodId' "); } basically, i want it to go into the "if" statement if the product id exists in the database already. any ideas Link to comment https://forums.phpfreaks.com/topic/38960-solved-loops-and-mysql/ Share on other sites More sharing options...
sspoke Posted February 17, 2007 Share Posted February 17, 2007 strange but could work replace if($row = mysql_fetch_array($result)) to while($row = mysql_fetch_array($result)) Link to comment https://forums.phpfreaks.com/topic/38960-solved-loops-and-mysql/#findComment-187431 Share on other sites More sharing options...
only one Posted February 17, 2007 Share Posted February 17, 2007 $result = mysql_query("SELECT * from `products` WHERE pID='prodId' ") or die(mysql_error()); while($row = mysql_fetch_array($result)) { echo "$prodId"; if($prodMap > 0 || $prodMap!= NULL) $prodPrice = $prodMap; elseif($prodPrice < 10) $prodPrice = $prodPrice + ($prodPrice * .50); elseif($prodPrice >= 10 && $prodPrice < 50) $prodPrice = $prodPrice + ($prodPrice * .25); elseif($prodPrice >= 50 && $prodPrice < 150) $prodPrice = $prodPrice + ($prodPrice * .15); elseif($prodPrice >= 150 && $prodPrice < 400) $prodPrice = $prodPrice + ($prodPrice * .10); elseif($prodPrice >= 400 && $prodPrice < 100) $prodPrice = $prodPrice + ($prodPrice * .06); else $prodPrice = $prodPrice + ($prodPrice * .05); $prodPrice = floor($prodPrice); $prodPrice = $prodPrice + 0.99; $result = mysql_query("UPDATE `products` SET `pPrice`='$prodPrice', `pInStock`='$prodQuant' WHERE `pID`='$prodId' "); } use a while loop not an if... Link to comment https://forums.phpfreaks.com/topic/38960-solved-loops-and-mysql/#findComment-187432 Share on other sites More sharing options...
hyeteck Posted February 17, 2007 Author Share Posted February 17, 2007 i have an "else" clause with the "if" so i need an if there....any other way of checking if the product ID exists in the database? Link to comment https://forums.phpfreaks.com/topic/38960-solved-loops-and-mysql/#findComment-187440 Share on other sites More sharing options...
sspoke Posted February 17, 2007 Share Posted February 17, 2007 if it exists in the loop it will go there if it doenst exist it wont even go anywhere to make it simple everytime inside that while loop exists nothing that doesnt exist never goes in that loop plus i dont see any else other than the nested one Link to comment https://forums.phpfreaks.com/topic/38960-solved-loops-and-mysql/#findComment-187445 Share on other sites More sharing options...
JJohnsenDK Posted February 17, 2007 Share Posted February 17, 2007 <?php $result = mysql_query("SELECT * from `products` WHERE pID='prodId' ") or die(mysql_error()); if(!$result){ echo "product key doesnt exist"; }else{ while($row = mysql_fetch_array($result)) { echo "$prodId"; if($prodMap > 0 || $prodMap!= NULL) $prodPrice = $prodMap; elseif($prodPrice < 10) $prodPrice = $prodPrice + ($prodPrice * .50); elseif($prodPrice >= 10 && $prodPrice < 50) $prodPrice = $prodPrice + ($prodPrice * .25); elseif($prodPrice >= 50 && $prodPrice < 150) $prodPrice = $prodPrice + ($prodPrice * .15); elseif($prodPrice >= 150 && $prodPrice < 400) $prodPrice = $prodPrice + ($prodPrice * .10); elseif($prodPrice >= 400 && $prodPrice < 100) $prodPrice = $prodPrice + ($prodPrice * .06); else $prodPrice = $prodPrice + ($prodPrice * .05); $prodPrice = floor($prodPrice); $prodPrice = $prodPrice + 0.99; $result = mysql_query("UPDATE `products` SET `pPrice`='$prodPrice', `pInStock`='$prodQuant' WHERE `pID`='$prodId' "); } } ?> Link to comment https://forums.phpfreaks.com/topic/38960-solved-loops-and-mysql/#findComment-187450 Share on other sites More sharing options...
hyeteck Posted February 17, 2007 Author Share Posted February 17, 2007 sorry, i should have posted a bit more of my code $approved_chunks = explode(" ", $approved_string); $prodId = $approved_chunks[0]; $prodPrice = $approved_chunks[1]; $prodMap = $approved_chunks[2]; $prodQuant = $approved_chunks[3]; $result = mysql_query("SELECT * from `products` WHERE pID='prodId' ") or die(mysql_error()); if($row = mysql_fetch_array($result)) { echo "$prodId"; if($prodMap > 0 || $prodMap!= NULL) $prodPrice = $prodMap; elseif($prodPrice < 10) $prodPrice = $prodPrice + ($prodPrice * .50); elseif($prodPrice >= 10 && $prodPrice < 50) $prodPrice = $prodPrice + ($prodPrice * .25); elseif($prodPrice >= 50 && $prodPrice < 150) $prodPrice = $prodPrice + ($prodPrice * .15); elseif($prodPrice >= 150 && $prodPrice < 400) $prodPrice = $prodPrice + ($prodPrice * .10); elseif($prodPrice >= 400 && $prodPrice < 100) $prodPrice = $prodPrice + ($prodPrice * .06); else $prodPrice = $prodPrice + ($prodPrice * .05); $prodPrice = floor($prodPrice); $prodPrice = $prodPrice + 0.99; $result = mysql_query("UPDATE `products` SET `pPrice`='$prodPrice', `pInStock`='$prodQuant' WHERE `pID`='$prodId' "); } else { there is my else clause in the end. Basically if the prodID is not in there, it goes in to the else clause where it is created...otherwise it just updates the price and quantity for that product id. Link to comment https://forums.phpfreaks.com/topic/38960-solved-loops-and-mysql/#findComment-187462 Share on other sites More sharing options...
JJohnsenDK Posted February 17, 2007 Share Posted February 17, 2007 hmm... just swicth the code then: <?php $result = mysql_query("SELECT * from `products` WHERE pID='prodId' ") or die(mysql_error()); if($result){ while($row = mysql_fetch_array($result)) { echo "$prodId"; if($prodMap > 0 || $prodMap!= NULL) $prodPrice = $prodMap; elseif($prodPrice < 10) $prodPrice = $prodPrice + ($prodPrice * .50); elseif($prodPrice >= 10 && $prodPrice < 50) $prodPrice = $prodPrice + ($prodPrice * .25); elseif($prodPrice >= 50 && $prodPrice < 150) $prodPrice = $prodPrice + ($prodPrice * .15); elseif($prodPrice >= 150 && $prodPrice < 400) $prodPrice = $prodPrice + ($prodPrice * .10); elseif($prodPrice >= 400 && $prodPrice < 100) $prodPrice = $prodPrice + ($prodPrice * .06); else $prodPrice = $prodPrice + ($prodPrice * .05); $prodPrice = floor($prodPrice); $prodPrice = $prodPrice + 0.99; $result = mysql_query("UPDATE `products` SET `pPrice`='$prodPrice', `pInStock`='$prodQuant' WHERE `pID`='$prodId' "); } }else { create blabla... } ?> Link to comment https://forums.phpfreaks.com/topic/38960-solved-loops-and-mysql/#findComment-187471 Share on other sites More sharing options...
hyeteck Posted February 17, 2007 Author Share Posted February 17, 2007 that doesn't work either. That just makes it go in the "if" every single time no matter if the product id is in there or not and never goes into the while loop. $prodId = $approved_chunks[0]; $prodPrice = $approved_chunks[1]; $prodMap = $approved_chunks[2]; $prodQuant = $approved_chunks[3]; $result = mysql_query("SELECT * from `products` WHERE pID='prodId' ") or die(mysql_error()); if($result) { echo "$prodId<br>"; while($row = mysql_fetch_array($result)) { echo "Boom!, I'm Innnnnnnnn"; if($prodMap > 0 || $prodMap!= NULL) $prodPrice = $prodMap; elseif($prodPrice < 10) $prodPrice = $prodPrice + ($prodPrice * .50); elseif($prodPrice >= 10 && $prodPrice < 50) $prodPrice = $prodPrice + ($prodPrice * .25); elseif($prodPrice >= 50 && $prodPrice < 150) $prodPrice = $prodPrice + ($prodPrice * .15); elseif($prodPrice >= 150 && $prodPrice < 400) $prodPrice = $prodPrice + ($prodPrice * .10); elseif($prodPrice >= 400 && $prodPrice < 1000) $prodPrice = $prodPrice + ($prodPrice * .06); else $prodPrice = $prodPrice + ($prodPrice * .05); $prodPrice = floor($prodPrice); $prodPrice = $prodPrice + 0.99; $result = mysql_query("UPDATE `products` SET `pPrice`='$prodPrice', `pInStock`='$prodQuant' WHERE `pID`='$prodId' "); } } else { Link to comment https://forums.phpfreaks.com/topic/38960-solved-loops-and-mysql/#findComment-187478 Share on other sites More sharing options...
sspoke Posted February 17, 2007 Share Posted February 17, 2007 ok listen your while loop mysql_fetch_array($result) only returns the product ids and everything that exists.. nothing that doesn't exist in database wont be put in the while loop sounds like you need to work more with SQL problem then PHP Link to comment https://forums.phpfreaks.com/topic/38960-solved-loops-and-mysql/#findComment-187482 Share on other sites More sharing options...
hyeteck Posted February 17, 2007 Author Share Posted February 17, 2007 hmm...still confused..ok so i broke down the code into pieces. $result = mysql_query("SELECT * from `products` WHERE pID='prodId' ") or die(mysql_error()); $row = mysql_fetch_array($result); echo "testing<br>".$row['pID']; that only prints the word "testing" without the product id next to it. why? Link to comment https://forums.phpfreaks.com/topic/38960-solved-loops-and-mysql/#findComment-187524 Share on other sites More sharing options...
sspoke Posted February 17, 2007 Share Posted February 17, 2007 I believe WHERE pID='prodId' thats the productID?? so there is only 1 product id replace maybe 'prodID' with '$prodID' Link to comment https://forums.phpfreaks.com/topic/38960-solved-loops-and-mysql/#findComment-187525 Share on other sites More sharing options...
hyeteck Posted February 17, 2007 Author Share Posted February 17, 2007 WOWOWOWOW...i can't believe...finally works. The entire time my problem was a stupid dollar sign. thanks! Link to comment https://forums.phpfreaks.com/topic/38960-solved-loops-and-mysql/#findComment-187526 Share on other sites More sharing options...
sspoke Posted February 17, 2007 Share Posted February 17, 2007 haha Link to comment https://forums.phpfreaks.com/topic/38960-solved-loops-and-mysql/#findComment-187533 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.