tags:

views:

47

answers:

3

I want to automatically add date in order_date field when a customer checkouts my online shop.

What is the best way to do it?

There are other fields like, delivery data and payment date in the same table.

Is it good idea to add a hidden field so that when a cutomer submit, the date will be added?

But I am not sure how to do it.

Can anyone suggests me the better way to do it please?

+5  A: 

No, you don't need a hidden form field. You can do this directly in MySQL.

Assuming that your order_date field is a DATE. DATETIME or TIMESTAMP, then in your SQL that inserts the order record, simply put NOW() as the value for order_date:

INSERT INTO orders (x,y,z,order_date) VALUES ('x','y','z',NOW());
zombat
I agree. Putting the NOW() function in your SQL will make this more secure and reliable. By using a hidden form field, malicious users can change this value to anything, potentially exploiting your online store. Never trust user input :p
Colin O'Dell
Besides, don't trust the client-side date.
Seva Alekseyev
A: 

In the update statement that finalizes your order record, do something like this:

update order set ....., order_date = now() where  ...

You don't want to rely on client-side date anyway. It could be off, and think of the timezones. You want a single source of dates in your system, and that should be either the database or the server-side code layer (PHP or what have you).

Seva Alekseyev
A: 

When a customer places an order you can use a TIMESTAMP column to track when the order was placed / the order was saved the database. Something like:

ALTER TABLE sales_order ADD COLUMN date_placed TIMESTAMP DEFAULT CURRENT_TIMESTAMP;

The DEFAULT CURRENT_TIMESTAMP will be filled with the current time when the row is created, but not on subsequent updates. See the docs for more:

http://dev.mysql.com/doc/refman/5.0/en/timestamp.html

With a DEFAULT CURRENT_TIMESTAMP clause and no ON UPDATE clause, the column has the current timestamp for its default value but is not automatically updated.

pygorex1
Um, the OP wants to store the checkout date, not the creation date.
Seva Alekseyev
It's a bit ambiguous - the OP was for recording order_date on checkout - i.e., when an order is placed.
pygorex1