I have a query that I want to update a column with the current date time. The column I want to update is of the type datetime.
How can I get it and set it?
else{ //If the last update is NULL, It must be players first page load. So set datetime equal to NOW
$query = "UPDATE `stats` SET `last_ap_update` = WHERE `member_id` = {$_SESSION['SESS_MEMBER_ID']}";
$queryresult = mysql_query($insertqry);
}
Using NOW() in the query would provide this.
else{ //If the last update is NULL, It must be players first page load. So set datetime equal to NOW
$query = "UPDATE `stats` SET `last_ap_update` = NOW() WHERE `member_id` = {$_SESSION['SESS_MEMBER_ID']}";
$queryresult = mysql_query($insertqry);
}
But if this gets updated anytime the table updates, I would suggest changing the column to TIMESTAMP and this will automatically update to the current time for you anytime that record changes.
else{ //If the last update is NULL, It must be players first page load. So set datetime equal to NOW
$query = "UPDATE `stats` SET `last_ap_update` = '".gmdate("Y-m-d H:i:s")."' WHERE `member_id` = {$_SESSION['SESS_MEMBER_ID']}";
$queryresult = mysql_query($insertqry);
}
You can use any format you want instead of gmdate("Y-m-d H:i:s")
You can use the mysql function NOW() for this, or you can pase the $_SERVER['REQUEST_TIME'] in there so the query gets cached by mysql.
Related
This is my database
This is my php code which is used to fetch the data from the table
while($row = mysqli_fetch_array($result))
{
$user_id = $row['id'];
$sql = "SELECT id, ScheduleDate, StartTime,Endtime, Hours,Employeeid
FROM empdet WHERE Employeeid ='".$user_id."' ";
$result = $con->query($sql);
if ($result->num_rows > 0)
{
// output data of each row
while($row = $result->fetch_assoc())
{
$id=$row["id"].
$date=$row["ScheduleDate"];
$start=$row["StartTime"];
$end=$row["Endtime"];
$hour=$row["Hours"];
$Employeeid=$row["Employeeid"];
list($year,$month,$day) = split("-",$date);
$data[] = array("year"=>$year,
"month"=>$month,
"day"=>$day,
"StartTime"=>$start,
"Endtime"=>$end,
"Hours"=>$hour );
}
$response = $data;
}
else
{
$data=1;
$response = $data;
}
My question is how to find whether the database is modified/changed/updated?
In the users table, if I change the start time from 2:00pm to 3:00 pm of "2018-12-01" it should notify me in echo as the "2018-12-01" has been modified.
Can anyone suggest how to do this with PHP?
int mysql_affected_rows ([ resource $link_identifier = NULL ] )
Returns the number of affected rows on success, and -1 if the last query failed.
see http://php.net/manual/en/function.mysql-affected-rows.php
You could make use of the timestamp column type in the database which, when set to use auto-update, would give an indication that the record has been updated.
alter table `empdet`
add column `timestamp` timestamp null default current_timestamp on update current_timestamp after `Employeeid`;
Then you could alter your query to determine if the record is updated
select `id`, `scheduledate`, `starttime`, `endtime`, `hours`, `employeeid`,
case
when `timestamp` is not null then
true
else
false
end as 'updated'
from `empdet` where `employeeid` ='".$user_id."';
However, looking at the original code there appears to be nested loops both trying to process the query result variable $result with the inner query re-assigning this variable. As the outer loop is providing an ID for the inner loop it would make more sense perhaps to do a join - but hard to be specific without seeing the previous code and knowing what the original query was.
you create a column with timestamp using this query
ALTER TABLE `table_name` ADD `time_stamp` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP AFTER `column_name`;
timestamp create auto time when data enter so you can get lastime of data and checkout max timestamp
I want to set a variable that will auto increase always by 1 in form submit and if exist to do +1 if already ia in db.
For example :
if the form is submitted almost the same time by two different users .i have tried with primary key Id but some times it gets increased by two.
My code for inserting is
<?php
require('db.php');
date_default_timezone_set('Europe/Athens');
include("auth.php"); //include auth.php file on all secure pages
mysql_query("SET NAMES 'utf8'");
$status = "";
$signintime = date("Y-m-d H:i:s");
$customid =$_REQUEST['customid'];
$seat =$_REQUEST['seat'];
$matchtype= $_REQUEST['matchtype'];
$ticketprice=$_REQUEST['ticketprice'];
$barcode= $_REQUEST['barcode'];
$expdate= $_REQUEST['expdate'];
$expdate = date('Y-m-d H:i:s', time()+36000);
$submittedby = $_SESSION["username"];
$ins_query="insert into kozani(`customid`,`signintime`,`seat`,`matchtype`,`ticketprice`,`barcode`,`expdate`,`submittedby`)values('$customid','$signintime','$seat','$matchtype','$ticketprice','$barcode','$expdate','$submittedby')
ON DUPLICATE KEY UPDATE `customid` = '$customid',`signintime`='$signintime',`seat`='$seat',`matchtype`='$matchtype',`ticketprice`='$ticketprice',`expdate`='$expdate',`barcode`= IFNULL (`barcode`,'$barcode')";
Where customid is the value of vouvher
Thank you.
you can use update , you need to adapt this code (php)
if( question )
UPDATE `table` SET `parameter` = `parameter` +1 WHERE `parameter` =".$value.";
else
UPDATE `table` SET `parameter` = `parameter` +2 WHERE `parameter` =".$value.";
i want to create a timestamp by which i can know which post is modified when and all. in mysql databse, i made a coloumn called lastmodified, with type as timestamp. now, when i am updating the data in db, i want to update the current timestamp in last modified. how to do so? also could anyone please tell me, if any function exits for comparing these timestamps.
$now = time();
$query = "update storydb set lastmodified = '$now' where story_id = '$story_id'";
mysqli_query($con, $query);
time() returns UNIX Timetamp in integer format e.g. 1223485636.
You want it in 2014-12-10 02:02:36
Use MySQL now() function instead of $now
$query = "update storydb set lastmodified = now() where story_id = '$story_id'";
now() is a MySQL function that returns current Time Stamp (including date).
No, its not unix timestamp that should be used in there, just a normal NOW() should suffice:
$query = "UPDATE storydb SET lastmodified = NOW() WHERE story_id = ?";
$update = $con->prepare($query);
$update->bind_param('s', $story_id);
$update->execute();
To insert current unix timestamp in data base
$time = time();
$qry = 'update db_name' set column_name = $time where condition;
mysql_query($qry);
I have a Project Management application I am building with PHP and MySQL and the function below is used to mass update all Tasks associated with a project, in some cases this could be 200-300 MySQL records being updated at once.
I just realized there is a flaw in my design here though. This function UPDATES my database columns status, date_modified, and date_completed
This is great most of the time but I just realized that often a user will forget to "start" a Task which sets the date_started column.
If the date_started column is never set, it will have a value of 0000-00-00 00:00:00 and with a date_started column set to 0000-00-00 00:00:00 and the date_completed column set to NOW...this breaks my Gantt chart functionality for these Tasks.
So I need to modify my SQL below so that when setting a status to completed it will somehow check to see if the date_stared column === 0000-00-00 00:00:00 and if it does, it will need to also update that column to NOW.
I believe this is possible using CASE in MySQL instead of having to query each and every record into PHP to do the check and then do a separate UPDATE when needed but my experience is very little in these more advanced SQL features.
Could someone help me with a solution?
public function completeAllTasks($projectId, $completed_date = true){
// Update Project Task STATUS and Date_MODIFIED
$sql = "UPDATE `project_tasks`
SET `status` = 'Completed',
`modified_user_id` = '$this->user_id',
`date_completed` = UTC_TIMESTAMP()
`date_modified` = UTC_TIMESTAMP()
WHERE `project_id` = '$projectId'
AND `status` != 'Completed'
AND `heading` != '1'";
return $this->db->query($sql);
}
UPDATE
I did some testing and this seems to do the trick...
`date_started` = (CASE
WHEN date_started = '0000-00-00 00:00:00'
THEN UTC_TIMESTAMP()
ELSE date_started
END)
$sql = "UPDATE `project_tasks`
SET `status` = 'Completed',
`modified_user_id` = '$this->user_id',
`date_completed` = UTC_TIMESTAMP(),
`date_modified` = UTC_TIMESTAMP(),
`date_started` = CASE
WHEN date_started = '0000-00-00 00:00:00' THEN NOW()
ELSE date_started
END
WHERE `project_id` = '$projectId'
AND `status` != 'Completed'
AND `heading` != '1'";
I'm writing a PHP/MySQL program. I need to add the current date each time a new record to the TABLE is added. I have a field called DATE_ADDED. I'm confused how to use CURDATE() function from MySQL to write this to the TABLE. Or should I from PHP use a date function to get today's date and pass it as a variable to be written to the TABLE? I don't need a TIMESTAMP, just YYYY-MM-DD.
Thanks!
You have to try with php like
$today = date("Y-m-d");
and then while adding data to your db give this value with the remaining
INSERT INTO table_name ( field1, field2,...DATE_ADDED )
VALUES
( value1, value2,...CURDATE());
You can set it as the default value for that column date_added in the table definition like so:
date_added TIMESTAMP DEFAULT CURRENT_TIMESTAMP
$regdate=date('Y-m-d');
$sql = "INSERT INTO table_name (fld_name1, fld_name2,fld_regdate) VALUES ('value1', 'value2', '$regdate')";
$rs = mysql_query($sql) or die('ERROR:' mysql_error());
$sql = 'INSERT INTO joke SET
joketext = :joketext,
jokedate = CURDATE()';
$s = $pdo->prepare($sql);
$s->bindValue(':joketext', $_POST['joketext']);
$s->execute();