1

Here's what I have. This code grabs the data from all of the users and subtracts 1 from user_days then updates every user's user_days row.

$result = mysqli_query($con,"SELECT * FROM users");

while($row = mysqli_fetch_array($result))
  {
  $minusone = $row['user_days']-1;
  mysqli_query($con,"UPDATE users SET user_days=$minusone");
  echo "<br />";
  echo $row['user_days'];
  }

The problem I'm having is this: Instead of subtracting 1 from each user and updating each users field, it's updating each user's field with the value from the first user.

example: before updating user 1 has 30 days user 2 has 60 days

after updating user 1 has 29 days user 2 has 29 days (instead of 59 days)

Any help is appreciated and I hope this question is easy to understand.

Just to clarify, I DO want to update every field. I just don't want the updates to be duplicated from the first result.

Thanks for all of the answers, this has given me a lot of help.

1
  • 3
    your SQL query updates all records. you should specify some condition into it, for example: update users set user_days=$minusone where id= . $row[id] or whatever. Moreover, you do not need to do that in the loop. Try something like update users set user_days=user_days-1; standalone. Commented Mar 11, 2013 at 14:58

7 Answers 7

3

Why don't you just run UPDATE users SET user_days = user_days-1 WHERE id=XXXXX? And then select the whole thing?

Sign up to request clarification or add additional context in comments.

1 Comment

If it's being done for all users, why execute n times for each individual user, and not simply run it once for all users?
0

When you update your record, you need to specify the user id for the record of interest, otherwise you current query updates all the rows in your table.

1 Comment

Or even simpler, use UPDATE users SET user_days=user_days-1 before doing your select
0

You should point which record to UPDATE

mysqli_query($con,"UPDATE users SET user_days=$minusone WHERE id=XXXXX");

Comments

0

The problem is with the UPDATE statement. Without a WHERE clause it will apply the SET clause to every row in the database. Assuming you have a unique id column named id in the users table, you could modify your code like this:

while($row = mysqli_fetch_array($result))
{
  $minusone = $row['user_days']-1;
  $user_id = $row['id'];
  mysqli_query($con,"UPDATE users SET user_days=$minusone WHERE id=$user_id");
  echo "<br />";
  echo $row['user_days'];
}

Comments

0

Use the following Query:

mysqli_query($con,"UPDATE users SET user_days=$minusone WHERE user_ID = '".$row['user_ID']."'");

1 Comment

PHP Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING
0

You're updating all the users with the same $minusone value. You need a WHERE clause in your update statement, like this:

$result = mysqli_query($con,"SELECT * FROM users");

while($row = mysqli_fetch_array($result))
  {
  $minusone = $row['user_days']-1;
  $id_user = $row['id_user'];
  mysqli_query($con,"UPDATE users SET user_days=$minusone WHERE id_user = $id_user");
  echo "<br />";
  echo $row['user_days'];
  }

Another way of doing what you want would be:

...
$result = mysqli_query($con,"UPDATE users SET user_days = user_days - 1");
...

This assuming you want to substracts 1 to all user_days.

Comments

0

As already pointed out by other answers, the best way to do this is to replace your entire code block with one line.

mysqli_query($con, "UPDATE users SET user_days=user_days-1");

Then you can SELECT and display the information as needed.

Without knowing exactly what you are using user_days for, I'm thinking there may be a better approach to what you are trying to do. I am assuming this is some kind of subscription service, and this code will be run once per day to decrement the number of days to allow them to access the service.

A better approach would be to have a subscriptionExpires field in your database, which would hold a datetime value. Using your approach, if the job that runs this fails, every user will get an extra day. What if a web spider or a user finds your script, your users accounts will expire early. If you use an actual date for when the account expires, there's no guessing if the current value is correct.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.