2

I am working a system in PHP. In my MySQL have a table borrow. Inside borrow is borrow_status a varchar and borrow_remain which is an int value. When the user click the submit button. The query will run something like

 INSERT INTO `borrow`( `borrow_status`,`borrow_remain`) VALUES ('Borrowing',3)

How can I decrement the borrow_remain value per 24 hours then UPDATE the borrow_status to Finish when it reaches zero?

EDIT: I also have a book table, that have a book_quantity, my only concern is that the book_quantity must increment if the borrow_status is updated to Finish.

WTFZane
  • 592
  • 1
  • 4
  • 25
  • Why not have `borrow_expiry_date` instead of `borrow_remain`? – eggyal Mar 03 '15 at 12:01
  • Oh I see, thank you for the concern. It is borrow remaining days. :) Which I think is identical to `borrow_expiry_date` – WTFZane Mar 03 '15 at 13:36
  • The difference is that if you store the "expiry date" rather than "remaining days", there's no need to increment/decrement anything: you'd simply test whether the current date is greater than or less than the expiry date. – eggyal Mar 03 '15 at 13:46
  • I thought of that also, and here is my thoughts about that. Would it be hard setting a value in PHP 3 days after submitting the form? Please, I want to hear your side. And give me the logical explanation. – WTFZane Mar 03 '15 at 13:56
  • I'm sorry, but I don't understand the question: what is the value that you wish to set "3 days after submitting the form"? – eggyal Mar 03 '15 at 13:59
  • I assume that `borrow_expiry_date` is date data type. How can I query it 3 days ahead from the date it is queried? submitted? (I can't pull the right english sorry) – WTFZane Mar 03 '15 at 14:14
  • 1
    Yes, `borrow_expiry_date` would be a `DATE` data type. You could query with `WHERE borrow_expiry_date = CURRENT_DATE + INTERVAL 3 DAY`. – eggyal Mar 03 '15 at 14:28

4 Answers4

3

You can implement the above scenario following the three steps given below :

1) First you have to enable Mysql event scheduler.

SET GLOBAL event_scheduler = 1;

2) Create a procedure in mysql that will do the borrow status update related functionalities.

    delimiter //
    CREATE PROCEDURE UBS()
    BEGIN
        UPDATE borrow set borrow_remain = borrow_remain - 1 
        WHERE borrow_status='borrowing';
        UPDATE borrow SET borrow_status = 'finished'
        WHERE borrow_remain=0;

    END //
delimiter ;

3) Create an event which will be scheduled in 24 hours interval.

CREATE
EVENT UBSEvent
ON SCHEDULE EVERY 24 HOUR STARTS '2015-03-04 00:00:00'
ON COMPLETION PRESERVE
ENABLE
DO
CALL UBS();

Note :

  1. UBS is the procedure name.[ UBS = Update Borrow Status (just a relevant abbreviation ]
  2. The event will start from tomorrow (4 March 2015) at 12:00 AM

Have a nice day!

EDIT: To increment book_count in book table whenever a borrow_status in borrow table gets updated to 'finished' you need a trigger.

Suppose you have a book table where the count of the book is denoted by book_count; Then the following trigger will do what you want.

DELIMITER $$
CREATE TRIGGER IncBookCount AFTER UPDATE ON borrow
FOR EACH ROW
BEGIN
     IF NEW.borrow_status = 'finished' THEN
            UPDATE book SET book_count = book_count+1;
     END IF;
END $$
DELIMITER;

SUGGESTION : You need to improve your database design. Otherwise you might get more troubles in designing a complex architecture in future. Stay well!

1000111
  • 13,169
  • 2
  • 28
  • 37
  • Will the event still usable for future inputs? and it gives me error creating the procedure. `near '' at line 4 ` it says that it is a incorrect syntax. My MySQL version is 5.5.36 – WTFZane Mar 03 '15 at 14:11
  • If your above stated scenario is kept unchanged then surely it will work. By the way, I've posted my answer after testing it by myself. So probably you have a problem with your delimiters. Would you please take a snapshot of that error and comment here? You may have a look at this link related to defining stored procedures in MySQL. http://dev.mysql.com/doc/refman/5.0/en/stored-programs-defining.html – 1000111 Mar 03 '15 at 14:46
  • Hi! I've just got the case why you are getting error. And this is exactly what I've comment about earlier. I've edited my post. Please try now.Hopefully this time you won't get the error. – 1000111 Mar 03 '15 at 14:56
  • Hey yes, the stored proc run! It is the delimeter-something. I'm gonna read about that later. And I forgot to ask something in my question. Please see my question after 5 minutes. – WTFZane Mar 03 '15 at 15:06
  • You have now my respect. Thank you for the answer and suggestion. I will keep that in mind. Stay well too. :) – WTFZane Mar 03 '15 at 15:51
  • Thanks for the compliment. Glad to hear that it helped! But you need to change :) :p – 1000111 Mar 03 '15 at 15:53
  • Actually, I didn't understand well the database design you are telling. Is it my way of connecting the tables? or the naming convention? – WTFZane Mar 03 '15 at 15:55
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/72161/discussion-between-subrata-dey-and-wtfzane). – 1000111 Mar 03 '15 at 15:56
  • One suggestion from me - it's probably better to store dates. Instead of `borrow_remain` it would be better to store `borrow_until` as a date in future, and every day you can compare that date with current time, and you'll know how many days left. It's more in case your script won't run or accidentally on one day will be run twice. – Paweł Tomkiel Mar 03 '15 at 19:38
1

Have you look at Events in mySQL? You can create an event and set it to run on a particular schedule. That might be the ticket for your problem!

Professor Abronsius
  • 33,063
  • 5
  • 32
  • 46
  • Where can i find that Events? So ok, let's say I've solve that problem, but how can i update when it reaches to zero? – WTFZane Mar 03 '15 at 11:47
0

You could also set a cronjob that executes a piece of code to decrement "borrow_remain" and updates "borrow_status" accordingly. If your hosting has cPanel it's pretty easy: https://documentation.cpanel.net/display/ALD/Cron+Jobs

Valeriu Ciuca
  • 2,084
  • 11
  • 14
0

You need to have mysql version 5.5 to use event. May be your desired event looks like this:

CREATE EVENT update_borrow
ON SCHEDULE EVERY 24 HOUR
STARTS CURRENT_TIMESTAMP
DO
BEGIN
UPDATE borrow SET borrow_remain = borrow_remain - 1 WHERE borrow_remain > 0;
UPDATE borrow SET borrow_status = 'FINISH' WHERE borrow_remain = 0;
END
Mamun Sardar
  • 2,679
  • 4
  • 36
  • 44