Using MySQLi Bind_Param With Date and Time Columns

Using Mysqli bind_param with date and time columns?

Like any other string

$stmt = $mysqli->prepare('insert into foo (dt) values (?)');
$dt = '2009-04-30 10:09:00';
$stmt->bind_param('s', $dt);
$stmt->execute();

Mysqli - Correct parameter for date

You can use "s" this could be used for date, datetime.
Its treated as just any other string.

$stmt->bind_param('s', $date);

Inserting date in PHP prepared statements

You should use the YYYY-MM-DD format:

$dat = date("Y-m-d");

How to insert the record with MySQL NOW() function in MySQLi with bind_param?

It's not a parameter of the query, in that you don't have to supply a value to MySQL.

$insert = $mysqli->prepare("INSERT INTO posts (post_name, publish_date) VALUES (?, NOW())");

how to insert date using php into mysql query?


WHERE DATE(`tec_sales`.date) = $date

Use single quotes around date value, otherwise it is evaluated as arithmetic operation - 2019-07-23 = 2012 - 23 = 1989.

Correct condition:

WHERE DATE(`tec_sales`.date) = '$date'

There is no risk of sql injection, because input value is parsed by strtotime.

MySQLi Prepared Statement to compare current time with stored datetime?

You can use this to check if one has submitted the form in the last 24 hours.

SELECT * FROM IPlock WHERE `ip`=? AND `submitTime` > NOW() - INTERVAL 24 HOUR;

If IP has not submitted in the past 24 hours, no rows will be returned.

EDIT:

In addition to that, there's no need to use PHP date() function, you can do your insert like

INSERT INTO IPlock (`ip`, `submitTime`) VALUES ('127.0.0.1', NOW());

Note that CURRENT_TIMESTAMP will work just as well.



Related Topics



Leave a reply



Submit