在我的表格中,我检查值是否存在,如果它存在,我需要增加qty
,但它不起作用。我做错了什么?
$servername = "localhost";
$username = "username";
$password = "psw";
$dbname = "database";
$myemail ='my@gmail.com';
$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);
}
答案 0 :(得分:4)
我想你要为qty添加+1:
$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";
警告,这只适用于应该是唯一的值。所以当我看到你的桌子时,我不确定它会起作用......