Note that there are some explanatory texts on larger screens.

plurals
  1. POEntire PHP Code Running Twice
    primarykey
    data
    text
    <p>My website is a community; we have forums and user profiles, and everyone has what are called "merits" which are sort of like the "Reputation" here on StackOverflow. I have a code which runs on a timer that I made. Two scripts run off of this timer; one script runs once per day (which works flawlessly), and the other one runs once per week.</p> <p>The timer, itself, works fine, but when it comes time to run the weekly code, a couple of really strange things happen: 1) The weekly code runs perfectly the first time, but for some reason, the entire code gets run a second time, and some logging errors occur. 2) When this program runs the second time, it forgets the last time everyone has logged in (the system is supposed to reduce a user's merit status by one if they have been inactive for two consecutive weeks), therefore reducing everyone's merits no matter how active they've been.</p> <p>I've done my homework, and I thought it was an error with my browser (Firefox loads every page twice, depending on what plugins you have), but this isn't that; every user has a merit log, which records each and every transaction affecting their merit status. Everyone has two transactions by the system, and the second one is exactly one second after the first. Everyone has the same two timestamps. What this means, is that the code runs through, gives everyone what they had coming, and then starts over from the beginning, exactly one second later. This time, however, it gives everyone a Demerit (a subtraction from their merits), unconditionally.</p> <p>None of this should happen because the first thing my weekly timer does is check to see if it's the right day to do it, and the second thing it does (if it <em>is</em> the right day) is update the timer to next week, so it won't run twice in a single day.</p> <p>Here it is:</p> <pre><code>&lt;?php mysql_connect("connect","username","password"); mysql_select_db("seriamus"); $feduby = mysql_query("SELECT day,week FROM timer WHERE name='timer'"); $timer = mysql_fetch_array($feduby); //Daily Timer if($timer[0]==date("M j, Y")) { $tomorrow = strtotime("+1 day"); mysql_query("UPDATE timer SET day='" . date('M j, Y', $tomorrow) . "' WHERE name='timer'"); mysql_connect("connect","username","password") or die(mysql_error()); mysql_select_db("agluserdatabase"); $getsuspendinfo = mysql_query("SELECT gamertag,rank,sdate,srank FROM users"); while($suspo = mysql_fetch_array($getsuspendinfo)) { if($suspo[1]=="Suspended") { if($suspo[2]==date("M j, Y")) { mysql_query("UPDATE users SET rank='" . $suspo[3] . "', srank='', sdate='' WHERE gamertag='" . $suspo[0] . "'"); } } } } //Weekly Timer if($timer[1]==date("M j, Y")) { $inaweek = strtotime("+7 days"); mysql_query("UPDATE timer SET week='" . date('M j, Y', $inaweek) . "' WHERE name='timer'"); mysql_connect("connect","username","password") or die(mysql_error()); mysql_select_db("agluserdatabase"); $getmeritinfo = mysql_query("SELECT merits,logins,lastseen,demerit,gamertag,rank,userid FROM users"); while($meritinfo = mysql_fetch_array($getmeritinfo)) { if($meritinfo[3]==0) { if($meritinfo[1]&gt;=3) { if($meritinfo[5]!="Suspended"&amp;&amp;$meritinfo[5]!="Banned") { $newmerits = $meritinfo[0] + 1; mysql_query("UPDATE users SET merits='" . $newmerits . "' WHERE gamertag='" . $meritinfo[4] . "'"); mysql_query("INSERT INTO meritlog" . $meritinfo[6] . " VALUES ('System', 'Merit', 1, 'Active for a week without getting a demerit', '" . date('M j, Y g:i:s') . "')"); } } else if ($meritinfo[1]==0) { $two_weeks_ago = strtotime('-14 days', strtotime(date("M j, Y"))); $last_seen = strtotime($meritinfo[2], strtotime(date("M j, Y"))); if($last_seen &lt;= $two_weeks_ago) { if($meritinfo[5]!="Suspended"&amp;&amp;$meritinfo[5]!="Banned") { $newmerits = $meritinfo[0] - 1; mysql_query("UPDATE users SET merits='" . $newmerits . "' WHERE gamertag='" . $meritinfo[4] . "'"); mysql_query("INSERT INTO meritlog" . $meritinfo[6] . " VALUES('System', 'Demerit', 1, '2+ weeks of inactivity', '" . date('M j, Y g:i:s') . "')"); if($newmerits &lt;= -10) { mysql_query("UPDATE users SET merits = 0 WHERE gamertag = '" . $meritinfo[4] . "'"); mysql_query("UPDATE users SET lastpromotion = '" . date('M j, Y') . "' WHERE gamertag = '" . $meritinfo[4] . "'"); if($meritinfo[5]=="Praetorian") { mysql_query("UPDATE users SET rank = 'Centurion' WHERE gamertag = '" . $meritinfo[4] . "'"); } else if($meritinfo[5]=="Centurion") { mysql_query("UPDATE users SET rank = 'Triarius' WHERE gamertag = '" . $meritinfo[4] . "'"); } else if($meritinfo[5]=="Triarius") { mysql_query("UPDATE users SET rank = 'Decanus' WHERE gamertag = '" . $meritinfo[4] . "'"); } else if($meritinfo[5]=="Decanus") { mysql_query("UPDATE users SET rank = 'Prime Legionary' WHERE gamertag = '" . $meritinfo[4] . "'"); } else if($meritinfo[5]=="Prime Legionary") { mysql_query("UPDATE users SET rank = 'Legionary' WHERE gamertag = '" . $meritinfo[4] . "'"); } else if($meritinfo[5]=="Legionary") { mysql_query("UPDATE users SET rank = 'Banned' WHERE gamertag = '" . $meritinfo[4] . "'"); } } } } } } mysql_query("UPDATE users SET logins='0', demerit='0' WHERE gamertag='" . $meritinfo[4] . "'"); } } ?&gt; </code></pre> <p>Within this code, there's also some things relating to the users' rank, or how many demerits they've been given that week, or whether they've been suspended or banned, etc. They shouldn't be important, but I included them anyways so you can get the entire code.</p> <p>Let me know if you need me to elaborate on what the whole code is specifically set up to do.</p> <p>Thank you, in advance, for taking the time to read all this and (hopefully) help me out with it! Any help at all would be appreciated, even if it's some efficiency method that's completely irrelevant to the problem -- any help at all would be great! :D</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload