0

I'm trying to update multiple cells in a MySQL database based on a previous update date. I am having an issue with unchanged, trailing cells updating to 0.

Database table looks like this (default = 0 for Day#; N = last update date):

id--Run--Day1--Day2--Day3--Day4

1----N------1-----2-----3----4

The code below does the following:

  1. Retrieves the database entry under column= Run, id=1
  2. Subtracts that date from today's date.
  3. Uses that difference to move database for Day1->Day4 to the left.

Example database table when the difference is 2 days:

id--Run--Day1--Day2--Day3--Day4

1----N------3-----4-----3----4

My issue is that I need it to change all trailing Days to 0. So in this example, Day3 & Day4 should both be 0. I've been trying out another foreach() statement within but can't get the logic behind it. Would someone please point me in the right direction?

$DaysColumnRange2 = range (1, 4);
foreach ($DaysColumnRange2 as $DaysColumnRangeLoop2){
    $SubtractedDaysColumns2 = $DaysColumnRangeLoop2 - $diff2format;
    $MoveToNewDay2 = ${$Day.$SubtractedDaysColumns2};
    $OriginalOldDay = $diff2format + $SubtractedDaysColumns2;
    $sql2 = "UPDATE users SET Day$MoveToNewDay2='$OriginalOldDay' WHERE id='$id'";
    if ($conn->query($sql2) === TRUE) {
        echo "RECORDS UPDATED SUCCESFULLY";
    } else {
        echo "Error updating record: " . $conn->error;
            }
}

I'll update this first post with my attempts as I continue to work on it.

(this is not a great way of doing it, but this is what I was able to put together since Cron jobs wasn't reliable & I haven't yet figured out how MySQL Triggers work)

**

------------------UPDATE--------------

**

This section is to clarify my question.

Let's say this is my database right now:

id--Run--Day1--Day2--Day3--Day4

1----N------1-----2-----3----4

I run the code below where $diff2format = 2:

//>Database credentials + login here

//Retrieve database entry for Run
$id = 1;
$todaysdateupdate = date("Y-m-d");
$lastupdatequeryresult = mysql_query("SELECT Run FROM users WHERE id='$id'");
$lastupdaterow = mysql_fetch_assoc($lastupdatequeryresult);

//Compare Run date to today's date.
$date3=date_create($lastupdaterow['Run']);
$date4 = date_create(date("Y-m-d"));
$diff2=date_diff($date3,$date4);
$diff2format = $diff2->format("%a");

//Day1, 2, 3, etc...
$result1 = mysql_query("SELECT Day1 FROM users WHERE id='$id'");
$row1 = mysql_fetch_assoc($result1);


//Hard coded "Day#" variables
$Day = "Day";
$Day1 = $row1['Day1'];
$Day2 = $row2['Day2'];
$Day3 = $row3['Day3'];
$Day4 = $row4['Day4']; //etc

//MY QUESTION STARTS HERE****************
$DaysColumnRange2 = range (1, 4);
foreach ($DaysColumnRange2 as $DaysColumnRangeLoop2){
    $SubtractedDaysColumns2 = $DaysColumnRangeLoop2 - $diff2format;
    $MoveToNewDay2 = ${$Day.$SubtractedDaysColumns2};
    $OriginalOldDay = $diff2format + $SubtractedDaysColumns2;
    $sql2 = "UPDATE users SET Day$MoveToNewDay2='$OriginalOldDay' WHERE id='$id'";
    if ($conn->query($sql2) === TRUE) {
        echo "RECORDS UPDATED SUCCESFULLY";
    } else {
        echo "Error updating record: " . $conn->error;
            }
}

The output for the database is below. Basically it copied Day3 moved it 2 times to the left, then copied Day4 and also moved 2 times to the left. Nothing changed with Day3 or Day4.

id--Run--Day1--Day2--Day3--Day4

1----N------3-----4-----3----4

But I need it to output this instead:

id--Run--Day1--Day2--Day3--Day4

1----N------3-----4-----0----0
Fr33
  • 61
  • 6
  • 1
    Please, please, please, don't use *mysql_* functions they are [Deprecated](https://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php) – Jorge Campos Jun 13 '17 at 23:42
  • I've been out of the programming scene for a while. I'll look in the manual to connect another way for that. I'm just lost with the trailing 0 logic for now :( – Fr33 Jun 13 '17 at 23:44
  • wait so all of these are depreciated? http://php.net/manual/en/ref.mysql.php – Fr33 Jun 13 '17 at 23:45
  • Yes, they are... I didn't quite understand your problem, maybe is because of my english... Can you add some more sample data and the desired output for this sample, also with different diferences (I understand better seeing the data) – Jorge Campos Jun 13 '17 at 23:54
  • 1
    Use [MySqli_*](http://php.net/manual/en/book.mysqli.php) or [PDO](http://php.net/manual/en/book.pdo.php) instead – Jorge Campos Jun 13 '17 at 23:57
  • I updated the original question. I hope that makes more sense. Basically I'm trying to make Day3 & Day4 = 0. And thank you, I will work on switching it around to MySqli_* or PDO. – Fr33 Jun 14 '17 at 00:05
  • Where is $id sourced from? If the user has any input on the value of $id then you are vulnerable to SQL injection. – Enstage Jun 14 '17 at 00:11
  • For now it's just hard coded in $id = 1. I will change it an array function to include the entire table later. See my updated first post below "-----Update----" for the full script. – Fr33 Jun 14 '17 at 00:13

0 Answers0