1

I just want to know how to subtract 1 from the number that appears in $row[posts_remaining]

In other words...


<?php
$con = mysql_connect($db_server_name,$db_username,$db_password);
if (!$con) {
    //do something
}
mysql_select_db("" . $db_database_name . "", $con);
$result = mysql_query("SELECT * FROM users
WHERE fb_id='$user_id'");
while($row = mysql_fetch_array($result)) {
    $posts_remaining = $row['posts_remaining']
    // this is where I want to subtract 1 and then update "posts_remaining" with the new number
}

mysql_close($con);
?>

This will give me my result where row posts_remaining = {THE NUMBER}

But I want to update that returned number by subtracting one from it and then SETting the new number in back where the old number was.

I hope Im not making this confusing. It's hard to explain.

Additionally... should I have the row "posts_remaining" set as something other than TEXT in order to do this.... like Char(50) or something or is it ok to leave it as TEXT?

automatix
  • 14,018
  • 26
  • 105
  • 230
user2284703
  • 367
  • 3
  • 15
  • 2
    IF you're using a database column to store numbers, then DON'T MAKE IT A TEXT FIELD – Mark Baker Oct 09 '13 at 12:05
  • 1
    Ok that still didn't tell me WHAT to make it or answer my initial question. But ok. – user2284703 Oct 09 '13 at 12:07
  • @user2284703 I think it quite clearly suggests that if you're dealing with integers, the INT types are probably a good way to go... (note: there are multiple that you may want to look at, TINYINT, SMALLINT, MEDIUMINT, INT, BIGINT - depending on how large you invisage the numbers in the column being) – MDEV Oct 09 '13 at 12:12
  • 1
    The `mysql_*` functions are **no longer maintained** and shouldn't be used in any new codebase. It is being phased out in favor of newer APIs. Instead you should use [**prepared statements**](https://www.youtube.com/watch?v=nLinqtCfhKY) with either [PDO](http://php.net/pdo) or [MySQLi](http://php.net/mysqli). – tereško Oct 09 '13 at 12:14

3 Answers3

3

First you have to modify the type of the field from text to integer Than for update records try this query

update users
set
posts_remaining= posts_remaining - 1
WHERE fb_id='your_user_id'
Moyed Ansari
  • 8,436
  • 2
  • 36
  • 57
  • I see where I went wrong. Youre code was right. I just had the wrong variable for the user id. Was using $user_id when I was supposed to be using $f_userid. – user2284703 Oct 09 '13 at 12:30
0

You can do it using database query itself:

mysql_query("Update users set posts_remaining = posts_remaining -1 where fb_id='$user_id'");
Mayank Sharma
  • 844
  • 7
  • 21
0

The column type has to be an integer, not string, you'll have to edit that. You can use

UPDATE users SET posts_remaining = posts_remaining -1 where fb_id = '$user_id'

You are also susceptible to SQL injection attacks, and you should look into prepared statements to reduce the impact of these attacks (https://www.w3schools.com/php/php_mysql_prepared_statements.asp)

Timothy
  • 3
  • 3