Jump to content

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.