change status after 1 hour and after 24 hours - php

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

Related

Calculating Days on every session login

<?php
session_start();
#mysql_connect('localhost','','') or die('Please Check Username or Password');
#mysql_select_db('') or die('error connetcing database');
$qry="SELECT DATEDIFF(DueDate, CURDATE()) AS XYZ,ID FROM fresh_orders";
echo $qry;
$rs=mysql_query($qry);
$qry1="";
while($row=mysql_fetch_array($rs))
{
echo $row[0];
echo $row[1];
$qry1="update fresh_orders set DDays='".$row[0]."' where ID=".$row[1]."";
mysql_query($qry1);
}
?>
This formula is not working for me.
I want to calculate the difference between payment Due Date and Current Date and update the remaining amount of days in DDays (Due Days) on every session login on individual user's login from Admin(my) side.
Each client have different credit days so need it to affect properly on each id together. Is there any way I can calculate the difference of each ID in one time login?
update fresh_orders set DDays=DATEDIFF(DueDate, CURDATE())

Struggling with simple database update

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!

unlink() function don't work without any error

i have problem with my code and unlink function
here my code
if(isset($_GET['delimg'])){
$id= $_GET['delimg'];
$sql = "delete from images_img where id='$id'";
$res=mysqli_query($con,$sql);
$getname="select * from images_img";
$res2=mysqli_query($con,$getname);
$image=mysqli_fetch_array($res2);
$image1=$image['image_url'];
$image2=$image['image_url_big'];
unlink('../../images/photo'.$image1);
unlink('../../images/photo'.$image2);
}
when i run my delete.php in the database the images deleted just fine
but the unlink function don't work and don't delete anything from the path and no error shows !
my thought that the select way return empty value ! as i use
this $image=mysqli_fetch_array($res2); to select the images name
so any error with my code please ?
UPDATE ::
i'm sure that the delete excuted before selecting the data for name to delete >>
so how i would arraying the order ?
UPDATE 2 :::
this is the upload code
$nameimg=$_FILES['image']['name'];
$tmp=$_FILES['image']['tmp_name'];
$type=$_FILES['image']['type'];
$size=$_FILES['image']['size'];
$dir="/images/photo/";
if($_POST['upload']){
if(!empty($nameimg)){
if(in_array($type,array('image/png','image/jpg','image/gif','image/jpeg'))){
if(filesize($tmp) <= 20242880){
move_uploaded_file($tmp,$dir.$nameimg) ;
$done ="done";
}
else {$errorsize= "it's bigger than the allowed size";}
}
else {
$errortype= "the file not image,please choose image to upload";
}
}
if(empty($nameimg)){$errorchoose= "no file choosen,please choose file";}
}
echo "<meta http-equiv='refresh' content='5;url=../../admin.php#ajax/add_img.php'>";
?>
check with file exists function like below also enable php errors
error_reporting(E_ALL);
ini_set('display_errors', 1);
$imgPath = ''../../images/photo'.$image1';
if (file_exists($imgPath)) {
unlink($imgPath);
} else {
echo "not exists";
}
ooh it's worked finally !!!
the all problem .. all time we have spend it was for nothing !
from first time and my first code it was work just fine and every code i have given by you guys it was worked .. but just we all [except "Parasad"] forgot [/] after "photo" !!!!!!! to be like this
unlink('../../images/photo/'.$image1)
not like this
unlink('../../images/photo'.$image1)
really i make you guys work hard to try slove my problem and i take your time
so million huge thanks to all of you specially [WordpressCoder] .. thanks a lot
Copy this code and tell me whether it is working or not?
if(isset($_GET['delimg'])){
$id= $_GET['delimg'];
$getname="select * from images_img where id='$id'";
$res2=mysqli_query($con,$getname);
$image=mysqli_fetch_array($res2);
$image1=$image['image_url'];
$image2=$image['image_url_big'];
$sql = "delete from images_img where id='$id'";
$res=mysqli_query($con,$sql);
unlink('../../images/photo/'.$image1);
unlink('../../images/photo/'.$image2);
}
if(isset($_GET['delimg'])){
$id= $_GET['delimg'];
$sql = "delete from images_img where id='$id'";
$res=mysqli_query($con,$sql);
$getname="select * from images_hair";
$res2=mysqli_query($con,$getname);
$image=mysqli_fetch_array($res2);
$image1=$image['image_url'];
$image2=$image['image_url_big'];
unlink('../../images/photo/'.$image1);//check your url
unlink('../../images/photo/'.$image2);//check your url
}

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.

Quiz Application showing Negative Score

I've a fb quiz application which is working fine if we check correct question and then go for next question but its showing negative score when i change options multiple times
I'm calling ajax onchange on selecting option to update score page and storing score in session
in this codes it works fine if we select correct answer and then wrong so it decrease but when selecting wrong and then we got negative score because of
$_SESSION['score']--;
How to solve this problem ???
// check if this option is correct for current question
$check_anwer_query = mysql_query("select * from tbl_option where ques_id=$ques_id and opt_id=$opt_id") or die(mysql_error());
if(mysql_num_rows($check_anwer_query))
{
$rs = mysql_fetch_array($check_anwer_query);
if($rs['opt_value']==1)
{
$_SESSION['score']++;
//$_SESSION[$ques_id]='1';
}
else if($_SESSION['lastq']==$ques_id) // check if option is changed,
{
$_SESSION['score']--;
}
if(isset($_SESSION['user']))
{
mysql_query("delete from tmp_quiz where ques_id=$ques_id and uid='".$_SESSION['user']."'") or die(mysql_error());
if($rs['opt_value']==1)
{
mysql_query("insert into tmp_quiz(ques_id,opt_id,uid,is_correct) values($ques_id,$opt_id,'".$_SESSION['user']."',1)") or die(mysql_error());
}
else
{
mysql_query("insert into tmp_quiz(ques_id,opt_id,uid,is_correct) values($ques_id,$opt_id,'".$_SESSION['user']."',0)") or die(mysql_error());
}
}
else
{
mysql_query("delete from tmp_quiz where ques_id=$ques_id and tmp_id='".$_SESSION['tmp_id']."'") or die(mysql_error());
mysql_query("insert into tmp_quiz(ques_id,opt_id,tmp_id) values($ques_id,$opt_id,'".$_SESSION['tmp_id']."')") or die(mysql_error());
}
}
$_SESSION['lastq'] = $ques_id; // assign current question as last question answred
?>
//Logic for Score Start
$rs = mysql_fetch_array($check_anwer_query);
if($rs['opt_value']==1)
{
$_SESSION['score']++;
$_SESSION["test_".$ques_id]='1'; // if Answer is Correct Set Session
}
elseif($_SESSION['lastq']==$ques_id) // check if option is changed
{
if(isset($_SESSION["test_".$ques_id])) { // if Last Ansewer is same as current question and last answer is yes then substract score
$_SESSION['score']--;
unset($_SESSION["test_".$ques_id]); } // Unset That Session
}
elseif(isset($_SESSION['test_'.$_SESSION['lastq']])) // If Last Question Session is still exists Then Unset That Question Session
{
unset($_SESSION['test_'.$_SESSION['lastq']]);
}
//Logic for Score End
You should not do relative calculations, since that requires you to be certain that the state shared between the UI and your session is 100% in sync and that no user actions arrive out of order (which I guess is where your problem is).
Instead, save all chosen answers in the session. When wanting to display the user's score, run through all chosen answers and calculate a number based on that. Do this every time you want to display the user's score, so the score is always up to date and accurate.

Categories