PHP MySQL select 字段并增加值

PHP MySQL select field and increment the value

我想获取指定字段的值(它是 INT),然后递增此值,然后在 mysql 中更新此值。我试过了,但没用。我在 MySQL.

完全是绿色的
            $attempts = mysql_fetch_array(mysql_query("SELECT attempts FROM employees WHERE lastname='$lastname'"));
            mysql_query("UPDATE employees SET attetmps='$attemtps++' WHERE lastname='$lastname'");

我建议: mysql_query("UPDATE employees SET attempts = attempts + 1 WHERE lastname = '".$lastname."'");

如果你想使用 mysql_fetch_array 你应该知道它 returns 一个数组而不是一个值

所以试试这个

<?php
$query = mysql_query("SELECT attempts FROM employees WHERE lastname='$lastname'");
$row = mysql_fetch_array($query, MYSQL_ASSOC);
$attempts = $row['attempts'] + 1;
mysql_query("UPDATE employees SET attempts='$attempts' WHERE lastname='$lastname'");
?>

或者使用一个查询..你不需要做两个查询

mysql_query("
    UPDATE employees
    SET attempts = attempts + 1
    WHERE lastname = '".$lastname."'
");
UPDATE employees SET attempts = attempts + 1 WHERE lastname = '$lastname'