Autoupdate every day on database [closed] - php

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I'm having local MAMP server with phpmyadmin with three tables, users, booking and rooms. In table booking I have something like bookingFrom bookingTo columns that represents booking time. In rooms table I have something like isBooked aswell which is depending on time mostly.
I'd like to make something in my phpmyadmin that every day database will update itself and check if the room is occupied and if the bookingTo date will be before the day that currently is the database will automatically change isBooked from true to false.
My goal isn't the ready script to get, but the way to do it or any useful links as long as it is my training website for school project :)

you can't do such as think with phpmyadmin. You can try mysql scheduler if your version supports it.
Something like that should work...
CREATE EVENT myevent
ON SCHEDULE EVERY 1 DAY
DO
CALL check_book_status();
Please don't forget to create check_book_status function that updates field that you need.
Also, here more information...
http://dev.mysql.com/doc/refman/5.7/en/create-event.html

It might be possible to do this in the database directly, I'm not aware of any method that does this, but I'm sure it's possible (maybe not in mysql, but in some db-engine). However, can't you just do a simple PHP-run of an UPDATE-query at the start of the day (or, whenever), or even just run a cron-script at a specific time (half an hour after checkout, or something like that) and have it check the "end-date" in the database, and if it's before the current date/time, change the "occupied"-status?

You can make it without update status. Suffice calculate status when you show this info:
SELECT
IF(NOW() > bookingFrom AND NOW() < bookingTo, 1, 0) AS isBooked
/* other fields */
FROM booking
...

Related

Send auto email on delay for each MySQL entry [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I currently have a MySQL database that sends an auto email each time a new record is entered. I need to also send a reminder email to the email address in each row 5 days after it is inserted. What is the best way to do this? Can I use a MySQL trigger or a cron job? I will likely need step by step instructions, if it isn't through PHP. I've done quite a bit of searching but can't find exactly what I'm looking for.
A cron job is what you need. The php file doing the cron needs to query the table for rows where the entry date is equal to 5 days ago. The mysql query is:
SELECT `email` FROM `entries` WHERE date(`entrytimestamp`) = date_sub(curdate(), INVERVAL 5 DAY);
Hopefully this gets you started in the right direction. You'll need to make sure that the php file cannot be called outside of cron, that cron only happens once a day, or maybe keep a separate table logging the emails where you would add a "NOT IN email_log" condition to make sure that you don't double up on emails to someone.

How can I time something in PHP? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I want to do on a script I`m working on that every month a part of the site resets.
Or a better example, Something like a Subscription, when you want to buy something and you need to renew it every month. How can I know its been a month?
In PHP you can't do regular cron jobs, and I will discourage you seriously from doing it with real cron jobs if you don't know what you're doing.
You can only register when you last executed that event and then check if it's been a month since then. This is a really simple sample cron:
<?php
$lastexecution = /*logic to know when you last executed.*/;
/* It's either a database or a file or something similar.
* I usually use a database table that contains the records when I
* last executed a cron
*/
if (time() > $lastexecution + (30 * 24 * 3600)) {
/*CRON LOGIC*/
}
You also should look into flock() or some similar locking mechanism to prevent the cron being triggered by two different users simultaneously.
Note: In your case, with a subscription, you could add a expires field to your database that would contain the date and time when the user's subscription needs to be renewed. If that date is in the past, you tell them

SQL table - Data management [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I am creating a website where users can easily calculate the calories they eat and see the repartition in term of fat, carbo, etc.
I want the users to be able to retrieve data from previous days.
I then need to store the data sent by my users everyday (basically, they input how much of each food they have eaten everyday and I am making the calculation then store the results).
The question if the following: what would be the best way to store the data? I have to store the data for each user for each day. I can't think of a simple solution (I think creating a new table for each new day would not be great, would it?).
I'm using PHP and MySQL for now.
Thanks for the help!
It seems that you are a step ahead of your self with the daily breakdown question.
First, you need to decide what you need to store, e.g. fields and normalise the way they are stored.
For example, you would have the following tables:
Users:
Id
..
EatItems:
UserId
ProductId
Calories
Fat
DateTime
Once you have these tables up and running, you can build reporting layer on top of that to breakdown consumption by user / date or anything else you might be interested in.
You could have a table that holds the input/calculated data/date which relates to a user/account.
When the user views previous day's, select the data that relates to that user.
I wouldn't create a table for each day. One table would suffice.
However, I would suggest attempting something and posting the code for specific issues you have if you run into before posting here.

Editable list using php mysql [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I'm a beginner to this whole mySQL thing and I am working on my first website. I understand how to setup the database I would need for this, but am unsure how I would implement it. I want to allow logged-in members to add input into a list such as the following:
First Name | Last Name | Timestamp | Poster
I want this to be saved to the database. Ideally the member will be able to input into the first two columns and the "timestamp" and "poster" will be automatically filled out. I'm not necessarily looking for a full explanation but maybe someone to point me in the right direction.
Thanks a bunch!
For the timestamp field, you can use a default value, although it is usually best to have your application handling such things (say using an ORM).
To default to the current time, in the mysql console, type:
ALTER TABLE myTable CHANGE COLUMN myColumn DEFAULT TIMESTAMP;
For the user, this will need to be inserted by your application unless you only have 1 user. You should really look at using some sort of Framework to handle these tasks.

Background update in database using php [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
We have a database table with created datetime and status flag. We would like to update the status flag to the next status if the created date time has elapsed by 30 minutes without any user intervention. How can we achieve this in php.
Create a php file that does this status change in the database and program to execute it every 30 minutes with cron (Linux) or Task Scheduler (Windows).
You have to think carefully about how you design and use your database. Sometimes things are made overly complicated when they don't need to. For instance, in this case you could use a 'datetime' in your table indicating the start time. Any PHP script can now check whether or not the start time started 30 minutes ago, only when this information is actually needed. No need for a flag, cron jobs, etc.

Categories