KAREN
KAREN

Reputation: 386

How to insert current date in database mysql

hello i am using insert query with a different pattern so i just want to insert the current date in database but i can't find a place to put $date in insert query

$date = date('Y-m-d H:i:s', strtotime(str_replace('-', '/', $date)));    

$sql="INSERT INTO `delivery`(product_id,pr_name,pr_price,pr_size,user_id)
         SELECT  `product_id`,`pr_name`,`pr_price`,`pr_size`, `userid`  
         FROM `shopping_cart` WHERE `userid` = $uid";

Upvotes: 3

Views: 6105

Answers (6)

S R Sharma
S R Sharma

Reputation: 31

First of you have to check whether you have column for current date or not in your database table. If not then you have to add a column to store current date and use that column in your query.

You can use timestamp /datetime / now() function to store current date(choose one according to your requirement)

$sql="INSERT INTO `delivery` (product_id,pr_name,pr_price,pr_size,user_id, curr_date)
 SELECT  `product_id`,`pr_name`,`pr_price`,`pr_size`,
         `userid`, timestamp
 FROM `shopping_cart` WHERE `userid` = $uid";

Upvotes: 1

Zafar Malik
Zafar Malik

Reputation: 6854

If you want to insert your own date then use as per below-

$date = date('Y-m-d H:i:s', strtotime(str_replace('-', '/', $date)));    

$sql="INSERT INTO `delivery`(product_id,pr_name,pr_price,pr_size,user_id, your_date_column)
         SELECT  `product_id`,`pr_name`,`pr_price`,`pr_size`,
                 `userid`, '$date' 
         FROM `shopping_cart` WHERE `userid` = $uid";

If you want to put current date then use as per below-

$sql="INSERT INTO `delivery`(product_id,pr_name,pr_price,pr_size,user_id, your_date_column)
         SELECT  `product_id`,`pr_name`,`pr_price`,`pr_size`,
                 `userid`, CURDATE()  
         FROM `shopping_cart` WHERE `userid` = $uid";

Note: If you want to insert with time then use now().

Upvotes: 2

mynawaz
mynawaz

Reputation: 1594

You should not pass date or datetime from PHP if you want the date/datetime at the time of yours query's execution, instead you can use built-in MySQL functions to do so

To insert current date & time use NOW()

$sql="INSERT INTO `delivery`(product_id,pr_name,pr_price,pr_size,user_id, dt)
    SELECT  `product_id`,`pr_name`,`pr_price`,`pr_size`,
        `userid`, NOW()  
    FROM `shopping_cart` WHERE `userid` = $uid";

To insert current date only use CURRENT_DATE()

$sql="INSERT INTO `delivery`(product_id,pr_name,pr_price,pr_size,user_id, dt)
    SELECT  `product_id`,`pr_name`,`pr_price`,`pr_size`,
        `userid`, CURRENT_DATE()  
    FROM `shopping_cart` WHERE `userid` = $uid";

These queries assume you have a column named dt in your delivery table. Please change column name if different in your schema, or add a date or datetime column in your table if you do not have any yet

Upvotes: 3

RiggsFolly
RiggsFolly

Reputation: 94682

You dont need to do the date capture in PHP, as long as you want the date to be the date as at time of execution of the query. You can use the MySQL NOW() function, just add it to the inner query in the correct column to match the outer queries columns.

$sql="INSERT INTO `delivery` (product_id,pr_name,pr_price,pr_size,user_id, PR_DATE)
     SELECT  `product_id`,`pr_name`,`pr_price`,`pr_size`, `userid`, NOW()
     FROM `shopping_cart` WHERE `userid` = $uid";

Upvotes: 3

senthil
senthil

Reputation: 185

Use now() in your query to get the current date.

Upvotes: 1

Prakash
Prakash

Reputation: 76

I cant understand your clearly. Normally, we insert current date like

  • curdate()
  • sysdate()

Upvotes: 1

Related Questions