Struggling with simple database update - php

This is a cronjob to check where a users subscription has expired. There are 4 different types of subscription.
However I'm completely lost as to why it doesn't update the database when I run the command.
Below is the script:
$sub = mysql_query("SELECT turboexpires,platinumexpires,goldexpires,silverexpires FROM $tab[user] WHERE id='$id'");
$expire = mysql_fetch_array($sub);
echo "".$expire['0']."";
echo "".$time."";
if($expire[0] <= $time){ mysql_query("UPDATE $tab[user] SET turbo='no' AND turboexpires='0' WHERE id='$id'"); }
if($expire[1] <= '$time'){ mysql_query("UPDATE $tab[user] SET platinum='no', platinumexpires='0' WHERE id='$id'"); }
if($expire[2] <= '$time'){ mysql_query("UPDATE $tab[user] SET gold='no', goldexpires='0' WHERE id='$id'"); }
if($expire[3] <= '$time'){ mysql_query("UPDATE $tab[user] SET silver='no', silverexpires='0' WHERE id='$id'"); }
I put the echo's in to check that they were working and they are.
$expire[0] comes back with a unix timestamp, in this case a past one.
$time comes back with the current unix timestamp.
I also tried removing the '' around $time as shown above with no luck.
However, when I then proceed with the command nothing happens in the database.
Hopefully an easy problem, I've just been looking at it for so long I'm completely lost!
Thanks for any help!
P.S yes I know I need to upgrade to mysqli, it's on the todo list haha!

Related

change status after 1 hour and after 24 hours

I have installed a cron job to run automatically. It checks for the jobs in the database and after one hour it should change from private to public status and after 24 hours it should change from public to expired. The cron job is running smoothly but the following code is not bringing the desired outcome. The database data doesn't change and when I run it myself, nothing is being shown as a message.
How can I twist it so that it achieves that?
My code:
// Check and do something: private to public
$now=date("Y-m-d H:i:s");
$timeBefore=strtotime($now-3600);
$check=mysqli_query($con,"SELECT * FROM ibirakas WHERE status='private' AND added<'$timeBefore'") or die(mysql_error());
while($exe_check=mysqli_fetch_assoc($check)){
$id=$exe_check['kiraka_id'];
$query=mysql_query("UPDATE ibirakas SET status='public' WHERE id='$id'") or die(mysql_error());
if($query==true){
echo "CRON_1_DONE";
} else {
echo "CRON_1_FAIL";
}
}
//check and do something: public to expired
$now=date("Y-m-d H:i:s");
$timeBefore=strtotime($now-86400);
$check=mysqli_query($con,"SELECT * FROM ibirakas WHERE status='public' AND added<'$timeBefore'") or die(mysql_error());
while($exe_check=mysqli_fetch_assoc($check)){
$id=$exe_check['kiraka_id'];
$query=mysql_query("UPDATE ibirakas SET status='expired' WHERE id='$id'") or die(mysql_error());
if($query==true){
echo "CRON_2_DONE";
} else {
echo "CRON_2_FAIL";
}
}
I have included connect.php file and also i have opened the php open and close tags(which are not included here)
Your help will be appreciated. Thank you
I think you can make it much easier without so much PHP and work more with MySQL functions. There are a lot functions like TIMEDIFF so you could check if the difference from NOW() to the saved date is bigger then 90 minutes. Here is a good question how to get minutes from your diff.
Difference in minutes from two time fields in MySQL
Then you don't need so select and update all your rows. You can directly update all that rows that match your conditions.
I have been able to find a relevant answer to this question. Since I wanted to use MySQL in the first place, this simple solution did it.
<?php
include("connect.php");
date_default_timezone_set("Africa/Kigali");
// Change from private to public
$query=mysqli_query($con,"SELECT * FROM ibirakas WHERE TIME_TO_SEC(TIMEDIFF(now(),`added`))>3600 AND status='private'");
while($row=mysqli_fetch_assoc($query)){
$change=mysqli_query($con,"UPDATE ibirakas SET status='public'");
}
// Change from public to expired
$query=mysqli_query($con,"SELECT * FROM ibirakas WHERE TIME_TO_SEC(TIMEDIFF(now(),`added`))>86400 AND status='public'");
while($row=mysqli_fetch_assoc($query)){
$change=mysqli_query($con,"UPDATE ibirakas SET status='expired'");
}
?>
Thanks

MySQL table not updating through PHP cron job at specified time

I am working on an Android App which has a MySQL database which contains a table that has all the login details of each user.In that table I have column named "Status" which is initially assigned to 0 for every user.Everyday when the user logs in to his account and submits the data(clicks the submit button) the status is changed to 1.And every night at 12 AM the status should be reset to 0.Also, if the user doesnot submit the data until 10 AM for a day, a row should be automatically inserted to the MySQL table with a column value as "No Records" for the particular user whose status is still 0.So,I have a created a PHP file which runs as a cron job in cpanel. The PHP file checks the time every hour. And the current time 12 AM and 10 AM, it should do the required changes.But I am not getting the required result with my PHP script.The script is given below:
<?php
require "connection.php";
$get_time = "select date_format(NOW(), '%H:%i A') as current_daily_date from dual;";
$convert_time = mysqli_query($connect,$get_time);
$php_time = mysqli_fetch_assoc($convert_time);
$final_time = $php_time["current_daily_date"];
$dateTime = new DateTime('now', new DateTimeZone('Asia/Kolkata'));
$d = $dateTime->format("H:i A ");
$e = $dateTime->format("d/m/y");
echo $d;
$s = '00:00';
$g = strtotime($s);
$p = date('H:i A',$g);
echo $p;
$c = '10:00';
$q = strtotime($c);
$r = date('H:i A',$q);
echo $r;
if($d == $p) {
$run = "UPDATE login SET status = 0;";
$connect->query($run);
echo "Status updated";
}
else if($d == $r) {
$second_run = "INSERT into attendance (name,date,attendance) VALUES ((SELECT name from login where status = 0 ), '$e' , 'No Records');";
$connect->query($second_run);
echo "Marked as No Records";
}
else {
echo "Not the correct time to change the status.";
}
$connect->close();
?>
I have set my email for the cron job so that I get the output to my mail. But even at 12 AM ,the table did not update.
The output was:
00:00 AM 00:00 AM10:00 AMNot the correct time to change the status.
I don't know where I am going wrong.Can anyone please check and let me know what is the issue?
The format of the INSERT SELECT is incorrect:
$second_run = "INSERT into attendance (name,date,attendance) SELECT name, ****, **** from login where status = 0";
You don't need the VALUES bit and I'm not sure what the end segment was at all. You also are trying to insert 3 values whilst only providing one, I've put ****'s in there where you need to put your other values.
You should also turn on error reporting in your script if you didn't get any errors from it as this would have been an error.
error_reporting(E_ALL);
ini_set('display_errors', 1);

Error in script sending automatic birthday wish to a registered user in php

I have written the script of sending automatic e-mail for a given date. I have already set up the cron job to execute this once a day for a given time. But it is not working. Can anyone show me the error here. I'm really new to this.
What I'm really supposed to do is to send an automatic email to a user on their birthday
<?php
$host="mysql117.000webhost.com/";//hostname
$username="abc";//mysql_username
$password="123";//mysql_password
$dbname="abc";//Database name
$tbl_name="customer_info";//table name
$date = date("2005-09-23"); //here my date format in my DB is 2010-09-30
$link = mysqli_connect('$host','$username','$password','$dbname');
if($link && mysqli_select_db('$dbname', $link))
{
$grabBday = "SELECT b_day,DATE_FORMAT(b_day,'2015-%m-%d') FROM customer_info where b_day = '2002-09-24'";
//here it will take the name of the person whose bday is on a particular date,I just hard coded this date to check if this is working
if($rs = mysqli_query($link, $grabBday))
{
while(mysqli_fetch_array($rs))
{
mail('abc92#yahoo.com', 'HAPPY BIRTHDAY', 'Many Happy Returns of the day');
}
}
} ?>
I am seeing logical difference as you are searching where b_day=$date where $date='2015-09-23' but dob can be '2002-09-23' or '1997-09-23' etc. so you will not get desired output, so you can change your query as per below if everything other than it is fine.
SELECT * FROM
tbl_name
WHERE CONCAT(YEAR(CURDATE()),DATE_FORMAT(b_day,'-%m-%d')) = $date

PHP MySQL Deactivating premium membership after a certain amount of time?

I have just recently worked out how to use PayPal IPN to initiate a premium membership when users pay a certain amount for it. Once the user clicks upgrade account, they are directed to PayPal to pay, then the IPN kicks in to update the database to show that the user is premium. I was hoping to have this last for a certain amount of time before their account is returned to normal, yet not sure how to do that? For example, they upgrade, 2 years later, a MySQL update is triggered to return their account to average. Here is the IPN script I am using:
<?php
include 'core/init.php';
//DONT CHANGE THESE VARIABLES
$payment_status = $_POST['payment_status'];
$payment_amount = $_POST['mc_gross'];
$payment_currency = $_POST['mc_currency'];
$receiver_email = $_POST['receiver_email'];
$username = $_POST['custom'];
if($payment_status =="Completed")
{
$purchase_check = mysql_query("SELECT premium FROM users WHERE username='$username'");
while($row = mysql_fetch_assoc($purchase_check))
{
$paid = $row['premium'];
}
if($paid=="0")
{
if($receiver_email=="aidan6141#hotmail.co.uk")
{
if($payment_amount=="6.99" && $payment_currency=="GBP")
{
$update_premium = mysql_query("UPDATE users SET premium='1' WHERE username='$username'");
}
}
}
}
else
{
echo "Access Denied!";
}
?>
I am thinking using my BASIC idea of PHP that I might add a value of "upgrade_date" to the database as a timestamp and then add into my code:
mysql_query("UPDATE users SET upgrade_date=CURDATE() WHERE username='$username'");
So updating the date when they upgrade shouldn't be an issue, the issue is how can I recall that information so in 2 years after the time stamp, they are issued back to an average member? I was also hoping to show a countdown date or something so they know how long they have left on their account?
Any help would be appreciated, thanks!
Why don't use unix timestamp instead of using boolean value in the database?
you can just say that the field premium in your database will save the expiration of premium membership
and then you can do the check to see if the current time stamp is greater than the value saved in the field premium then it's expired ... if it is Null or 0 he never been premium, and if it's less than the saved value then his membership is valid and then you can calculate the difference between current timestamp and the saved one to see how much longer it still has to expire.
Why not have in your DB
upgraded = tinyint: to store a 0 for not upgraded and a 1 for upgraded
upgraded_date = datetime: to store the date this was done
Then set-up a cron job to run (maybe a few times a day or just check as the user logs in) to check if the time has expired, if it has reset 'upgraded' to 0
you maybe need an else condition to check the years.
like that:
while($row = mysql_fetch_assoc($purchase_check))
{
$paid = $row['premium'];
$timestamp_start = $row['upgrade_date'];
$timestamp_end = date("Y/m/d");
$difference = abs($timestamp_end - $timestamp_start);
$years = floor($difference / (365*60*60*24));
}
if($paid=="0")
{
if($receiver_email=="aidan6141#hotmail.co.uk")
{
if($payment_amount=="6.99" && $payment_currency=="GBP")
{
$update_premium = mysql_query("UPDATE users SET premium='1' WHERE username='$username'");
}
}
}else if($years == 2){ do update here and set premium to 0}
else{do what you like here}
}
else
{
echo "Access Denied!";
}

php mysql - trying to add time limit to how offten user can post

I'm new to using php, but here my problem:
I'm working on a website where users can post things to the server.
I'm trying to add a 5 minute limit to how often a user can post. Every time a user posts it updates a field on their row in the database with CURRENT_TIMESTAMP()
What I would like to do is something that checks how long ago they posted and if it is less than 5 minutes ago it disallows them to post yet.
I figure it would look something like this:
<?php
$query = "SELECT * FROM Users_table WHERE Username = '$username'";
$result = mysql_query($query);
if (($result['last_post']-now())<=5minutes) {
echo "Please wait 5 minutes before you can post again.";
} else {
code to let user post
}
?>
The problem i'm having is i don't know how time works very well and don't know how long 5 minutes would look like.
Any help is much appreciated.
Instead of using a database, you can use sessions too:
session_start();
if (isset($_SESSION['last_post']) && $_SESSION['last_post'] + 300 >= time()) {
echo "sorry, please wait for 5 minutes before next post";
} else {
// do your post here
$_SESSION['last_post'] = time();
}
It will add a session variable that keeps track of the last post within this session; every time you post the value gets updated.
When the last time + 5 minutes is in the future, then it hasn't been 5 minutes yet since the last post.
You can check that directly on SQL, then use PHP just to verify if the query returned anything:
$query = "
SELECT * FROM Users_table
WHERE Username = '$username'
AND DATE_ADD(last_post, INTERVAL 5 MINUTE) > NOW()
";
$result = mysql_query($query);
if(mysql_num_rows($result) > 0) {
echo "Please wait 5 minutes before you can post again.";
} else {
//code to let user post
}
SELECT * FROM Users_table WHERE Username = ? AND DATE_ADD(last_post, INTERVAL 5 MINUTE) > NOW()
Then check the result is empty or not.

Categories