jj20051 Posted July 30, 2008 Share Posted July 30, 2008 I Have A Script I Made That Is Supposed To Add Products to A Database. First It Checks to See If There Is A Product With The Same Barcode In The Database - If There Is It Adds The Quantity's Of The Product Entered and The Product In The Database Together. If There Is No Matching Barcode It Simply Creates A New Listing... For some Reaso It Doesn't Work and It Creates A New Listing Every Time. if(mysql_result(mysql_query("SELECT * FROM `products` WHERE `barcode`='{$barcode}'"))){ mysql_query("UPDATE `products` SET `quantity`=(`quantity` + $quantity) WHERE `barcode`='{$barcode}'") or die(mysql_error()); }else{ //Nope, add a new one. mysql_query("INSERT INTO products (id, name, barcode, price, quantity) VALUES('$id', '$name', '$barcode', '$price', '$quantity') ") or die(mysql_error()); } echo "<center><b><h3>Product Added - Returning!</h3></b></center>"; Quote Link to comment https://forums.phpfreaks.com/topic/117347-adding-products/ Share on other sites More sharing options...
pocobueno1388 Posted July 30, 2008 Share Posted July 30, 2008 Try <?php if(mysql_result(mysql_query("SELECT * FROM `products` WHERE `barcode`='{$barcode}'"), 0)){ mysql_query("UPDATE `products` SET `quantity`=(`quantity` + $quantity) WHERE `barcode`='{$barcode}'") or die(mysql_error()); } else { //Nope, add a new one. mysql_query("INSERT INTO products (id, name, barcode, price, quantity) VALUES('$id', '$name', '$barcode', '$price', '$quantity') ") or die( mysql_error()); } echo "<center><h3>Product Added - Returning!</h3></center>"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/117347-adding-products/#findComment-603602 Share on other sites More sharing options...
jj20051 Posted July 30, 2008 Author Share Posted July 30, 2008 Thank You So Much... I Couldn't Figure Out What Was Wrong So Thanks! BTW I Will Be Working On This script For A While and May Need Some More Help... Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/117347-adding-products/#findComment-603604 Share on other sites More sharing options...
unkwntech Posted July 30, 2008 Share Posted July 30, 2008 Pssst... don't forget to mark the topic solved the button is down and the to left. Quote Link to comment https://forums.phpfreaks.com/topic/117347-adding-products/#findComment-603606 Share on other sites More sharing options...
jj20051 Posted July 30, 2008 Author Share Posted July 30, 2008 Ok... I Got That Working Now I Have Another Problem... I Made This Script To Update The Price and The Quantity Of The Product Based On User Input. It Should Update The Price In The DB To What The User Inputs... ( The Update Quantity Portion Already Works the Way I Need It To ) if(mysql_result(mysql_query("SELECT * FROM `products` WHERE `barcode`='{$barcode}'"), 0)){ mysql_query("UPDATE `products` SET `price`=(`price` = $price) WHERE `barcode`='{$barcode}'") or die(mysql_error()); mysql_query("UPDATE `products` SET `quantity`=(`quantity` + $quantity) WHERE `barcode`='{$barcode}'") or die(mysql_error()); } else { //Nope, add a new one. mysql_query("INSERT INTO products (id, name, barcode, price, quantity) VALUES('$id', '$name', '$barcode', '$price', '$quantity') ") or die( mysql_error()); } Quote Link to comment https://forums.phpfreaks.com/topic/117347-adding-products/#findComment-603619 Share on other sites More sharing options...
craygo Posted July 30, 2008 Share Posted July 30, 2008 do it in one query if(mysql_result(mysql_query("SELECT * FROM `products` WHERE `barcode`='{$barcode}'"), 0)){ mysql_query("UPDATE `products` SET `price`= $price, `quantity`=(`quantity` + $quantity) WHERE `barcode`='{$barcode}'") or die(mysql_error()); } else { //Nope, add a new one. mysql_query("INSERT INTO products (id, name, barcode, price, quantity) VALUES('$id', '$name', '$barcode', '$price', '$quantity') ") or die( mysql_error()); } Ray Quote Link to comment https://forums.phpfreaks.com/topic/117347-adding-products/#findComment-603623 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.