PHP - Subtracting a number from mysql table values

Bradley

This may seem like a bit of an odd request, but it's important that I get this working. It's for a game i'm making. So basically, my users have their own accounts on my website. I have different plans that they can select and each plan costs a different amount every month.

What I want to do is have 4 seperate PHP crons that automatically subtract a certain amount from their accounts each month based on the plan they have selected. For example, if a user has the "Ultimate" plan, and currently has $1,049.99 in their account, I want it to subtract 49.99 from the "balance" row in the database, and then update the table to 1000.00 (as it has subtracted the plan amount).

At registration, the database is loaded with the value "chargeday" which is basically the day of the month that the user registered. I want the cron to run every day (which I know how to do) and only run this code if the current day equals the chargeday listed on the database. (I've been running this code with the if ($chargeday == date("d")) { and } lines removed.

I've tried the following but I can't get it to work the way I want:

<?php
  $servername = "localhost";
  $username = "myusername";
  $password = "mypassword";
  $dbname = "advenaio_myaccount";

$chargeday = date("d");
   if ($chargeday == date("d")) {

$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 

$sql = "SELECT email, plan, balance FROM advaccounts WHERE plan='ultimate'";
$result = $conn->query($sql);

$ultimateplan = "49.99";
$currentbalance = $row['balance'];
$newbalance = $currentbalance-$ultimateplan;

if ($result->num_rows > 0) {
    // output data of each row
    while($row = $result->fetch_assoc()) {
        echo "plan: " . $row["plan"]. "<br>balance: " . $row['balance'] . "<br>new balance: " . $newbalance . "<br>email address: " . $row['email'] .  "<br><br> ";
    }
} else {
    echo "0 results";
}

$sql = "UPDATE advaccounts SET balance=$newbalance WHERE plan='ultimate'";

if ($conn->query($sql) === TRUE) {
    echo "Record updated successfully";
} else {
    echo "Error updating record: " . $conn->error;
}

$conn->close();
?>

I'm sorry if my question doesn't make sense, I just really need this to work and I'm sort of desperate for it to work. Here is the structure of the database:

id_user | name | email | domain | username | password | plan | balance | joindate | chargeday | substatus | cic

Jignesh Patel

you can use below sql to update the balance directly using one sql. No need to write php logic.

$sql = "UPDATE advaccounts SET balance=balance-49.99 WHERE plan='ultimate' and chargeday = '" . date('d'). "'";  

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Subtracting multiple values from same number

From Dev

PHP & MySQL: Get all values from a table?

From Dev

Using values returned from a mysql table in PHP

From Dev

data.table: Subtracting levels means from values

From Dev

MySQL - Select number of occurrences of values from another table (including zeros)

From Dev

Pull unique values from mysql table and order by number of occurrences

From Dev

How to Get VALUES from another table into another table in PHP/MySQL

From Dev

Updating table in php MYSQL with Values from another table

From Dev

Inserting dynamic table values from PHP array to MySQL table

From Dev

Subtracting values via MYSQL query

From Dev

Subtracting values via MYSQL query

From Dev

SELECT * FROM table WHERE column = ??? in PHP with unknown number of values for?

From Dev

SELECT * FROM table WHERE column = ??? in PHP with unknown number of values for?

From Dev

Subtracting 2000 from Range of values

From Dev

Adding/Subtracting values from a checkbox

From Dev

Php, Mysql sort results based on number of records from another table

From Dev

PHP MYSQL finding row number/rank from table with order by clause

From Dev

how to fetch a number of rows...from table in mysql using php

From Dev

MySQL subtracting value from one table by some rows from second table

From Dev

MySQL subtracting value from one table by some rows from second table

From Dev

Mysql subtracting payments from invoices

From Dev

php count and add each values from MySQL table row

From Dev

Adding total values from multiple table in MySQL with php

From Dev

how to retrieve array values from mysql table using php?

From Dev

Adding total values from multiple table in MySQL with php

From Dev

Adding multiple positive values and subtracting it with matching number

From Dev

Subtracting Number of Days from a Date in PL/SQL

From Dev

subtracting square wave from csv values

From Dev

Subtracting average values from columns in pandas DataFrame

Related Related

  1. 1

    Subtracting multiple values from same number

  2. 2

    PHP & MySQL: Get all values from a table?

  3. 3

    Using values returned from a mysql table in PHP

  4. 4

    data.table: Subtracting levels means from values

  5. 5

    MySQL - Select number of occurrences of values from another table (including zeros)

  6. 6

    Pull unique values from mysql table and order by number of occurrences

  7. 7

    How to Get VALUES from another table into another table in PHP/MySQL

  8. 8

    Updating table in php MYSQL with Values from another table

  9. 9

    Inserting dynamic table values from PHP array to MySQL table

  10. 10

    Subtracting values via MYSQL query

  11. 11

    Subtracting values via MYSQL query

  12. 12

    SELECT * FROM table WHERE column = ??? in PHP with unknown number of values for?

  13. 13

    SELECT * FROM table WHERE column = ??? in PHP with unknown number of values for?

  14. 14

    Subtracting 2000 from Range of values

  15. 15

    Adding/Subtracting values from a checkbox

  16. 16

    Php, Mysql sort results based on number of records from another table

  17. 17

    PHP MYSQL finding row number/rank from table with order by clause

  18. 18

    how to fetch a number of rows...from table in mysql using php

  19. 19

    MySQL subtracting value from one table by some rows from second table

  20. 20

    MySQL subtracting value from one table by some rows from second table

  21. 21

    Mysql subtracting payments from invoices

  22. 22

    php count and add each values from MySQL table row

  23. 23

    Adding total values from multiple table in MySQL with php

  24. 24

    how to retrieve array values from mysql table using php?

  25. 25

    Adding total values from multiple table in MySQL with php

  26. 26

    Adding multiple positive values and subtracting it with matching number

  27. 27

    Subtracting Number of Days from a Date in PL/SQL

  28. 28

    subtracting square wave from csv values

  29. 29

    Subtracting average values from columns in pandas DataFrame

HotTag

Archive