0

別のレコードが挿入される前に、テーブルの最新のレコードの日時フィールドを確認できるようにしたいと考えています。レコードが挿入されたときに、これを使用して日時フィールドに入力します。

$date = date("Y-m-d H:i:s");

今のところ、他のフィールドの検証がありますが、日時フィールドにも検証を追加したいと考えています。テーブルの最新のレコードをチェックし、日時フィールドと $date = date("Ymd H:i:s");の違いをチェックする必要があります。差が 5 秒以下の場合は、テーブルに新しいレコードを挿入できるようになります。次のようなエラー メッセージが表示されます。

    }else if(trim($checktime) == '') {
    $error = 'Please wait 5 seconds before insert'; 
   } if($error == '') { //Run the code

上記の例で $checktime はどのようになりますか?

4

2 に答える 2

2

最初にデータベースに最新の日時を問い合わせます。次のようなものがうまくいくはずです: SQL:

SELECT MAX(`mydate`) AS mydate FROM `tablename`;
or
SELECT `mydate` FROM `tablename` ORDER BY `mydate` DESC LIMIT 1;
or if you have a primary key auto-increment column called id this may be fastest:
SELECT `mydate` FROM `tablename` ORDER BY `id` DESC LIMIT 1;

<?php
$last_insert_time = strtotime(); # Use the result from the database query as the parameter
$now = time();

# If the difference between the last inserted entry and the current time is less
# than five seconds perform an error action.
if (($now - $last_insert_time) < 5) {
    $error = 'Please wait 5 seconds before insert';
}
于 2013-01-10T11:15:34.617 に答える
0

このように使用できます

1)このクエリを使用して、最初に最終記録日の値を取得します

SELECT dateVal FROM tablename ORDER BY id DESC LIMIT 0, 1;

2)これを使用して2つの日付の違いを見つけます

$timeFirst  = strtotime($dateVal);
$timeSecond = strtotime(date('Y-m-d H:i:s'));
$checktime= $timeSecond - $timeFirst;
于 2013-01-10T11:10:10.253 に答える