如何使用更新将+1增加到数据库中的值

时间:2015-10-09 10:12:48

标签: mysql

这里我试图将数据库中的值增加+ 1.例如 在数据库中,当我调用下面的函数时,我在名称owners的一列中有值3。我想将列增加+1,即列值现在应该变为4

function updateOwners($product_id){
    // Create connection
    $conn = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME);
    if ($conn->connect_error) 
    {
        die("Connection failed: " . $conn->connect_error);
    } 
    $sql = "UPDATE product_list set product_current_co_owners =  where product_id ='$product_id'";
    $result = $conn->query($sql);
    /*if ($result->num_rows > 0) {
        // output data of each row
        while($row = $result->fetch_assoc()) {
            echo '<b>Total co-owners:</b><br><span>'.$row["product_co_owners"].'</span>';
        }
    } else {
        echo "No data available";
    }*/
    $conn->close();
}

我该怎么做?

$sql = "UPDATE product_list set product_current_co_owners =# here i want add +1 to previous value  where product_id ='$product_id'";

3 个答案:

答案 0 :(得分:1)

试试这个,但你的列必须是整数类型

UPDATE product_list set product_current_co_owners =product_current_co_owners+1  where product_id ='$product_id'

答案 1 :(得分:1)

只需将+1添加到上一个值,例如

UPDATE product_list 
set product_current_co_owners = product_current_co_owners + 1 
where product_id ='$product_id';

答案 2 :(得分:0)

$sql = "UPDATE product_list set product_current_co_owners =  owners + 1 where product_id ='$product_id'";