Patryk Brejdak
Patryk Brejdak

Reputation: 1601

PHP MySQL select field and increment the value

I want to get the value of specified field(it's INT) then increment this value and then update this in mysql. I tried this but it doesn't work. I'am completely green in MySQL.

            $attempts = mysql_fetch_array(mysql_query("SELECT attempts FROM employees WHERE lastname='$lastname'"));
            mysql_query("UPDATE employees SET attetmps='$attemtps++' WHERE lastname='$lastname'");

Upvotes: 0

Views: 744

Answers (3)

Mohammad Alabed
Mohammad Alabed

Reputation: 809

if you want to use mysql_fetch_array you should know it returns an array and not one value

so try this

<?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'");
?>

or use one query.. you do not need to do two queries

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

Upvotes: 3

paddyfields
paddyfields

Reputation: 1539

UPDATE employees SET attempts = attempts + 1 WHERE lastname = '$lastname'

Upvotes: 2

ion
ion

Reputation: 540

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

Upvotes: 3

Related Questions