1

I am a newbie to mysql. I am making a table named inventory with 4 columns- id,name, price,quantity. Now if name already exists, I am to add the quantity to existing one else create a new row. (name is defined as UNIQUE). I am doing this on php. $name, $quant,$price contain the values.

mysqli_query($con,"INSERT INTO inventory (Name, Quantity, Price) VALUES ($name,$quant,$price)
ON DUPLICATE KEY UPDATE
   Quantity    =  Quantity + $quant ,
  Price = VALUES(Price) ");

I am unable to understand why it's not working correctly. Sometimes Quantity updates correctly, most of the times it doesn't. Rest everything working perfectly. Just the problem with Quantity. Thanks for any help

EDIT: I defined the table on phpmyadmin. Here are the details:
1 id int(11) AUTO_INCREMENT PRIMARY

2 Name varchar(15) latin1_swedish_ci UNIQUE
3 Quantity int(11)
4 Price int(11)

EDIT: It was a cache problem . SOLVED

16
  • Post the table definition please. (SHOW CREATE TABLE inventory) Commented Mar 27, 2016 at 7:38
  • Add mysqli_error() function in your query to show the error. Commented Mar 27, 2016 at 7:40
  • 1
    Sorry, it works fine for me :-) At the end i get "Quant=46" (10+5+6+10+15) Commented Mar 27, 2016 at 8:21
  • 1
    Might be a cache problem - happens to me again and again :-) - deactivate the cache while developing Commented Mar 27, 2016 at 8:29
  • 1
    And also check for negative numbers..i was able to set $quant as -1000 and your code happily accepted that ;) Commented Mar 27, 2016 at 9:37

3 Answers 3

1

Is the $quant variable being updated correctly from your GET variable? Also please try the following query which should do the same as you are trying to do:

  mysqli_query($con,"INSERT INTO inventory (Name, Quantity, Price) VALUES ($name,$quant,$price)
ON DUPLICATE KEY UPDATE
   Quantity    =  Quantity + VALUES(Quantity),
  Price = VALUES(Price) ");
Sign up to request clarification or add additional context in comments.

Comments

0

problem with delaing with NULL values i guess Try as follow

mysqli_query($con,"INSERT INTO inventory (Name, Quantity, Price) VALUES ($name,$quant,$price)
ON DUPLICATE KEY UPDATE
Quantity    =  IF((Quantity IS NULL), $quant,Quantity + $quant) ,
Price = VALUES(Price) ");

3 Comments

Quantity = NULL won't work. Use Quantity IS NULL instead.
@PaulSpiegel Done Boss :)
Problem is not with NULL values. It picks up the first entry correctly. It's just adding consecutive entries randomly.
0

Please remember that if $name, $quant, and $price are coming from users then this method is subject to SQL injection attacks and should not be used. Directly taking any user input without sanitizing it first is critical mistake that can lead security vulnerabilities.

We now have tools like PDO statements which prepare your SQL for entry into a databases. Please consider using a similar tool which prepares your statements when getting anything from a user.

Users are not to be trusted. Notice the binding of values below:

$query = $mysqli->prepare('
    SELECT * FROM users
    WHERE username = ?
    AND email = ?
    AND last_login > ?');

$query->bind_param('sss', 'test', $mail, time() - 3600);
$query->execute();

The above code is from this article and it discusses the differences between using straight PDO and the mysqli version:

http://code.tutsplus.com/tutorials/pdo-vs-mysqli-which-should-you-use--net-24059

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.