How to update table if date has passed? - php

On my page I want to display some dates. A script needs to check if the date is in the past or in the future. If it's in the future, the date needs to be displayed. If it's in the past, I want a query to change the value of this date to "closed", so it won't be displayed any more.
What I have tried:
$deDatum = strtotime($row_originele_datum['datum']); //the date that has to be checked
$vandaag = strtotime($datum_vandaag); //today's date
$dataID = $rows_data['id']; //the message ID
if ($deDatum < $vandaag) {
$result_sluit_datum = mysqli_query($mysqli, "UPDATE belangrijkeDatum SET status = 'gesloten' WHERE id=$dataID");
}
but this seems to do nothing at all. No changes were made after running the page. What am I missing?

I think you need to change
$result_sluit_datum = mysqli_query($mysqli, "UPDATE belangrijkeDatum SET status = 'gesloten' WHERE id=$dataID");
to
$result_sluit_datum = mysqli_query($mysqli, "UPDATE belangrijkeDatum SET status = 'gesloten' WHERE id='".$dataID."'");
Essentially you are passing $dataID to your query instead of the value of $dataID.

Related

Update a value in sql server with php that can be used one time

I want to execute my update statement just once, and prevent updating it a second time. My query is as follows:
$sql0 = "UPDATE PRODUCTS.dbo.ITEMPRICE
SET Price = Price + '".$item_price."'
WHERE UserID = '".$session_id."'";
$q0 = odbc_exec($gcon, $sql0);
But every time, when I open the page, the value gets updated again and again.
Add one more column to your table: IS_UPDATED int (0 - is not updated yet, 1 - is updated already). Set it's default value to 0.
Then your update statement will be:
$sql0 = "update PRODUCTS.dbo.ITEMPRICE
set Price = Price + '".$item_price."'
, IS_UPDATED = 1
where UserID = '".$session_id."'
and IS_UPDATED = 0";
Your solution works, but now i wanna try something like this
$sql01 = "SELECT Price, Code FROM PRODUCTS.dbo.ITEMPRICE WHERE UserID = '".$session_id."'";
$q01 = odbc_exec($gcon, $sql01);
IF (Code value NOT EXISTS)
{
$sql0 = "UPDATE PRODUCTS.dbo.ITEMPRICE SET Price = Price + '".$item_price."' WHERE UserID = '".$session_id."'";
$q0 = odbc_exec($gcon, $sql0);
}
Just wanna know what to put in if statemen to check if that code already exists in database.

How to limit how often a user can update a mysql value to a database

I have a field on my website, which updates a value in my mysql database. I want to make it so the user can only update the value every 3 days. How would I go about doing this?
Here is the code that I have written so far:
<?php
if(isset($_POST['Update'])){
$UpdateHWID = $_POST['HWID'];
$sql = $con->query("UPDATE users SET HWID = '{$UpdateHWID}' where UserID = $User");
echo "HWID Updated Successfully!";
}
?>
Use a last updated field in mysql (date and time of last update), and check it before making the update. If satisfies your condition then commit the update and also update that time field, if not show error to the user.
Create new row into db (last_update) type= data
//return to sql last_update (select db ...)
$current_Data = date ('Y-m-d');
$current_Data_time = strtotime ($current_Data); //convert data to time
$last_update_p3 = strtotime ("+3day", strtotime($last_update));
$last_update_p3 = strtotime ($last_update_p3);//convert data to time
if($current_Data_time <=$last_update_p3)
{
$sql = $con->query("UPDATE users SET HWID = '{$UpdateHWID}' , last_update='{$current_Data}' where UserID = $User");
//update last data with current date
}
else
{
//It has not gone three days
}
According to Pinx0, if you add a new column to your users table which contains the date of the last update, then you can create a condition. For example:
ALTER TABLE `users`
ADD `lastUpdated` DATE NOT NULL;
Now you can add a condition to your existing query something like this:
UPDATE `users`
SET `HWID` = '{$UpdateHWID}',
`lastUpdated` = NOW()
WHERE `UserID` = $User AND 2 < DATEDIFF(CURDATE(),`lastUpdated`);
You can easily do that, using the DATEDIFF method.
It will be my first comment. so, Let me know if I have written
something incorrectly here.
$currentDate = date('Y-m-d');
$query = "SELECT DATEDIFF($currentDate,*Last_update_date_row_name*) as diff FROM *TABLE_NAME* WHERE user_id=Current_User_Id";
$result = mysqli_query($conn,$query);
if($result!=false){
//compare the diff with your desire number
// then you can hide or disable the button or show error
}

SQL UPDATE query fails to update some of the time - PHP Booking System

I've built a simple booking system using SQL, php, and Javascript/Jquery. I'm also using AJAX. The idea is that the user is presented with a list of available timeslots and when the user selects one that chosen timeslot should automatically disappear (the available timeslots reload again via AJAX) and so become unavailable to further users. Each timeslot is output as an html td element containing a link which stores the timeslot 'id' (as set in the database) as an attribute of that hyperlink. When the timeslot is clicked, the timeslot id is sent to the php function below using AJAX. Timeslots are displayed according to whether their visibility column is zero or one as stored in the database.
I am testing this with quite a large group of users and I've noticed that every so often (maybe once every twenty or thirty bookings) the SQL UPDATE query responsible for updating the timeslot availability value to zero does not seem to be doing what it should, and the value remains at one, thus resulting in an occasional double booking.
Otherwise, everything works absolutely fine - I've tested the application with my colleagues as much as I can and I'm unable to replicate the problem, double bookings obviously aren't an option though. Might anyone have an idea of what could be going wrong?
Thanks in advance for any suggestions/help.
public function makebooking($user, $timeslotUID, $timeslot, $edits) {
$username = $user->username;
$user_fn = $user->displayname;
$plgID = $user->plg; //The 'PLG' is the person who the user is booking an appointment with
$user = $edits['user'];
$currentDate = $edits['date'];
$json = new JSON();
$json->user = stripslashes($user);
$json->currentDate = $currentDate;
//Problem seems to occur here..
$q1 = " UPDATE plg_timeslots
SET visible = 0
WHERE id= '$timeslotUID';" ;
$res1 = $this->dbcon->query($q1);
//If the user already has a booking they are rearranging
$currentBooking = $this->getBookings($username, 'STU');
if($currentBooking) {
//Reinstate previous timeslot
$q2 = "UPDATE plg_timeslots
SET visible = 1
WHERE id= '$timeslotUID';" ;
$res2 = $this->dbcon->query($q2);
//Update current booking
$q3 = "UPDATE bookings
SET timeslot_id = '$timeslotUID',
timeslot = '$timeslot',
edited_by = '$user',
edited_when = '$currentDate'
WHERE user_id = '$username' ;";
$res3 = $this->dbcon->query($q3);
} else { //If no booking is present, make a new booking
$q4 = "INSERT INTO bookings (plg_id, user_id, timeslot_id, timeslot,
edited_by, user_fn, edited_when)
VALUES ('$plgId', '$username' ,'$timeslotUID',
'$timeslot', '$user', '$user_fn',
'$currentDate');";
$res4 = $this->dbcon->query($q4);
}
return $json;
}

extend mysql date

I am trying to code so that when a certain date is reached, then that is displayed in the report. What I would like to do is to find a way so that when a date is reached, ie, today, then keep the entry in the report until a user deletes it. So instead of it just showing today and not beyond, I need to find a way to show when a date is reached, it is displayed until deleted.
mysql_select_db($database_conn, $conn);
$query = "SELECT * FROM boxes WHERE customer = '$_SESSION[kt_idcode_usr]' AND destroy_date = DATE(NOW()) AND status = 1";
$result = mysql_query($query) or die(mysql_error());
SELECT * FROM boxes WHERE customer = '$_SESSION[kt_idcode_usr]' AND
destroy_date <= DATE(NOW()) AND status = 1";
this will return, when a date is reached, it is displayed until deleted.
if row is deleted no record would be there.

Setting status of other rows after INSERT

Hey, I have a field called STATUS and it is either 1 to show or 0 to hide. My code is below. I am using an edit in place editor with jQuery. Everytime you update it creates a new ROW which I want, but I want only the new one to have STATUS = 1 and the others to 0. Any ideas on how I would do that?
<?php
include "../../inc/config.inc.php";
$temp = explode("_", $_REQUEST['element_id'] );
$field = $temp[0];
$id = $temp[1];
$textboxval = stripslashes(mysql_real_escape_string(preg_replace('/[\$]/',"",$_REQUEST["update_value"])));
$query = "INSERT INTO notes ($field,status,date,c_id) VALUES ('$textboxval','1',NOW(),'$id')";
mysql_query($query);
echo($_REQUEST['update_value']);
?>
I am not sure exactly what you mean - do you want to make all the entries except the new one have status = 0? If so, just issue an update before the insert:
UPDATE notes SET status = 0
However, I should also note that you have a potential SQL injection to worry about. By stripping slashes after applying "mysql real escape string", you are potentially allowing someone to put text in your SQL statement that will execute an arbitrary SQL statement.
Something like this, sorry for the post before, I mis read it the first time then went back:
<?php
include "../../inc/config.inc.php";
$temp = explode("_", $_REQUEST['element_id'] );
$field = $temp[0];
$id = $temp[1];
$textboxval = mysql_real_escape_stringstripslashes((preg_replace('/[\$]/',"",$_REQUEST["update_value"])));
// set older entries to 0 - to not show but show in history
$hide_notes = "UPDATE notes SET status = 0";
mysql_query($hide_notes);
// add new entry with status of 1 to show only latest note
$query = "INSERT INTO notes ($field,status,date,c_id) VALUES ('$textboxval','1',NOW(),'$id')";
mysql_query($query);
echo($_REQUEST['update_value']);
?>
i just ran in to a problem I didn't of the set up of my table doesn't allow me to show more than one client a time and i will be having numerous clients, my bad on planning ha
You really want to get the ID of the newly generated row and then trigger an UPDATE where you all rows where the ID is not the new row, e.g.
UPDATE notes SET status = 0 WHERE id != $newly_generated_id
If the ID column in your table is using AUTO_INCREMENT you can get its ID via "SELECT LAST_INSERT_ID()" and then use the return value in that statement in your UPDATE statement.
Pseudo code:
$insert = mysql_query("INSERT INTO ...");
$last_id = mysql_query("SELECT LAST_INSERT_ID()");
$update = mysql_quqery("UPDATE notes SET status = 0 WHERE id != $last_id");
The only caveat to this approach is where you might have a brief moment in time where 2 rows have status=1 (the time between your INSERT and the UPDATE). I would wrap all of this in a transaction to make the whole unit more atomic.

Categories