Jump to content

Equal To Update


jj20051

Recommended Posts

I Have Created A Script To Add Products To A Database but I Have A Problem. Each Product Has A Barcode. Now What I Need It To Do Is Check and See If The Barcode Already Exists In The Database. If It Does I Want It To Add 1 To The Quantity Of That Product. If It Doesn't Exist I Have Made the Following To Add A New Listing:

 

mysql_query("INSERT INTO products (id, name, barcode, price, quantity) VALUES('$id', '$name', '$barcode', '$price', '$quantity') ")

or die(mysql_error());

 

 

I Know It Will Involve An If () Query and an Else Query

Link to comment
https://forums.phpfreaks.com/topic/114486-equal-to-update/
Share on other sites

if(mysql_result(mysql_query("SELECT * FROM `products` WHERE `barcode`='{$barcode}'"),0)){
//Yes, it exists, lets add one to quantity.
mysql_query("UPDATE `products` SET `quantity`=(`quantity` + 1) 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());
}

 

Should do the trick...

Link to comment
https://forums.phpfreaks.com/topic/114486-equal-to-update/#findComment-588710
Share on other sites

Thank You So Much It Works Perfectly! I Do Have Two More Requests If You Would Like To Help.

 

1. I Need To Some How Take 15% Off All Price Values In A Table. The Table Name Is checkout .

 

2. i have made a script to copy one of the values in a table to another table as shown bellow. but the problem is when they copy the id stays with them. is there any way to have it create a new id?

 

mysql_query("INSERT INTO checkout (id,name,barcode,price,quantity) SELECT id,name,barcode,price,quantity FROM products where barcode='$barcode1'")

or die(mysql_error());

Link to comment
https://forums.phpfreaks.com/topic/114486-equal-to-update/#findComment-588716
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.