Jump to content

Adding Products


jj20051

Recommended Posts

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>";

 

 

Link to comment
https://forums.phpfreaks.com/topic/117347-adding-products/
Share on other sites

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>";

?>

Link to comment
https://forums.phpfreaks.com/topic/117347-adding-products/#findComment-603602
Share on other sites

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());
}

Link to comment
https://forums.phpfreaks.com/topic/117347-adding-products/#findComment-603619
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/117347-adding-products/#findComment-603623
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.