Magento Dev
Magento Dev

Reputation: 29

update table if exist

In my table I check values is there or not if it's there I need to increase qty but it's not working. What mistake I done?

$servername = "localhost";
$username = "username";
$password = "psw";
$dbname = "database";

$myemail ='[email protected]';
$image = '1';
$user = 'user';
$device = '1';
$product = '2';
$qty = '100';
$status = '1';
$orderno = '2';

$conn = mysqli_connect($servername, $username, $password, $dbname);

// Check connection
$sql = "INSERT INTO tablemname(email,image_url,user_id,device_id,product_id,qty,status,order_no) VALUES ('$myemail','$image','$user','$device','$product','$qty','$status','$orderno') ON DUPLICATE KEY UPDATE qty=1";

if (mysqli_query($conn, $sql)) {
  echo "New record created successfully";
} else {
  echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}

enter image description here

Upvotes: 0

Views: 67

Answers (1)

Daniel E.
Daniel E.

Reputation: 2480

I think you want to add +1 to qty :

$sql = "INSERT INTO tablemname(email,image_url,user_id,device_id,product_id,qty,status,order_no)
VALUES ('$myemail','$image','$user','$device','$product','$qty','$status','$orderno') 
ON DUPLICATE KEY UPDATE qty= qty+1";

A warning, this work only on a value that should be unique. So when i see your table, I am not sure it will work...

Upvotes: 4

Related Questions