Jump to content

Auto insert current date to MySQL


Strimer

Recommended Posts

I just want to add some things to DB and one of it is current date which I want to be added automaticly according to the current daate.  I am using something like below but it is not working

 

 

<?php

//something unimportant

$c_date = date ("Y-m-d", $phptime);
$name = $_POST["name"];
$value = $_POST["value"];


//something unimportant
    
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");    

$result = mysql_query ("INSERT INTO $tbl_name (DATE, NAME, VALUE) VALUES ($c_date, $name', '$value')");
}

mysql_close();
        
if ($result) {
    echo "Zaznam ".$c_date." - ".$name." - ".$value." vložený!!!";
    echo "<BR>";    
} else {
    echo "ERROR";
}

?>

 

 

Thank yo

Link to comment
https://forums.phpfreaks.com/topic/275715-auto-insert-current-date-to-mysql/
Share on other sites

The second argument to the date function is a UNIX timestamp.  So that date returns that time, rather than the current.

There is a couple of options you can run with.

 

1. You can specify the date like you are doing.

2. You can store a UTC date in MySQL using the timestamp column type, and specify it to default to CURRENT_TIMESTAMP, you will not have to send anything, and it will add the timestamp.

 

Like Davidannis says, date is a reserved keyword in MySQL, so you will need to enclose it in backticks(`).

 

Edit: UNIX on the brain...

@OP, there is a syntax error (maybe two or more) in your sql query.

 

Instead using a date function in php, in that particular example you could use date-time functions in mysql.

 

Also, why did you close mysql connection and then make a test for the correct result? 

 

Well, you need to obtain basic knowledge before to start using the script even like this.

Thank you for your answers...

 

I have tried

 

$result = mysql_query ("INSERT INTO $tbl_name (`DATE`, NAME, VALUE) VALUES ('$c_date', $name', '$value')");

 

but it is not working. When I try to enter NAME or VALUE, everything is OK, but the date causing th eproblem..

 

thank you for help

My guess is that the content of $c_date  is mangled because I don't know where or how $phptime was set. I now understand that

 

The second argument to the date function is a UNIX timestamp.  So that date returns that time, rather than the current.

but since the OP said

I want to be added automaticly according to the current daate.

it is not really needed.

 

That said, I think that the advice to find the mysql error is a good.

Jessica, If I understand correctly, you are saying create a table with a column called myfieldname, YEAR(4) and then

INSERT into `mytablename` (myfieldname) VALUES (NOW())

What difference does it make? Is it a security issue or an efficiency issue or something else entirely?

FWIW, in an effort to avoid all possible productive work I looked at how long it took to set the year in a database using 3 methods, php outside of the loop, php every time the data was written and using the db's native function. The native function is a tiny bit more efficient unless you are going to make multiple writes tot he DB (you can't pull the native MySQL function out of the loop).

 

The code:

<?php
require_once 'config.inc.php';
require_once (NON_WEB_DIR.'setup.php');;
$time = -microtime(true);

for ($i=0; $i < 4000; ++$i) {
    $c=date('Y');
    $query = "INSERT into test (`id`,`year`) VALUES ('','$c')";
    $result= mysqli_query($link, $query);
}
$time += microtime(true);
echo "time: ",sprintf('%f', $time),PHP_EOL;
$c=date('Y');
echo '<br />time set outside of loop';
$time = -microtime(true);
for ($i=0; $i < 4000; ++$i) {
    $query = "INSERT into test (`id`,`year`) VALUES ('','$c')";
    $result= mysqli_query($link, $query);
}
$time += microtime(true);
echo "time: ",sprintf('%f', $time),PHP_EOL;
echo "<br />time set by DB";
$time = -microtime(true);
for ($i=0; $i < 4000; ++$i) {
    $query = "INSERT into test (`id`,`year`) VALUES ('',NOW())";
    $result= mysqli_query($link, $query);
}
$time += microtime(true);
echo "time: ",sprintf('%f', $time),PHP_EOL;
?>

the results:

time: 0.272242
time set outside of looptime: 0.242826
time set by DBtime: 0.268353

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.