HI , First at all i'm a newbie and sorry if this a dumb question.

i want to create a form that only can be submitted once per day by each user, each user has thier own id. My question is how to create a process that will check if the user already submitted and only can be summited on the next day .

This is code that i have tried and now working

<?php

include("../../config/config.php");

        $id=$_POST['id_student'];
        $id_kedai=$_POST['id_kedai'];
        $now_date_time=date('Y/m/d');


        $quer1="SELECT * FROM activity_log where id_user='$id' AND date_create_log = '$now_date_time'";
         $quer2=mysqli_query($db_conn_e,$quer1);
           while ($row= mysqli_fetch_array($quer2)>0){
        if($row["date_create_log"] ==  $now_date_time=date('Y/m/d')){
    echo "<script langauge=\"javascript\">alert(\"Data Already Inserted Today\");
            </script>";
}

       else
       {  


        $que1="INSERT INTO activity_log (id_user,id_kedai)  VALUES ('$id','$id_kedai')";
        $que2= mysqli_query($db_conn_e,$que1); 


      if($que2){
        echo    "<script langauge=\"javascript\">alert(\"Success\");
                window.location='../submited.php?id=$id'
            </script>";

      }
}

           }      
?>

the date_create_log is set as current_timestamp in database.

Sorry for asking.

Dani AI

Generated

— the idea is simple but the implementation needs two fixes: compare dates in a format the database understands (or use DB date functions), and enforce “one-per-day” at the database level so concurrent requests can’t sneak through. is right that tracking the user/session helps UX, but client-side/session checks must be backed by a server-side constraint.

A reliable pattern is to add a DATE column you can index, then make a unique key on (id_user, that_date). With MySQL >= 5.7 a stored generated column works well:

ALTER TABLE activity_log
  ADD COLUMN created_date DATE GENERATED ALWAYS AS (DATE(date_create_log)) STORED,
  ADD UNIQUE KEY uniq_user_day (id_user, created_date);

With that in place you can attempt an insert and let the DB enforce uniqueness. Using prepared statements and checking the result keeps things safe:

$id = (int)$_POST['id_student'];
$id_kedai = (int)$_POST['id_kedai'];

$stmt = $mysqli->prepare("INSERT IGNORE INTO activity_log (id_user, id_kedai) VALUES (?, ?)");
$stmt->bind_param("ii", $id, $id_kedai);
$stmt->execute();

if ($stmt->affected_rows === 1) {
  // inserted — success
} else {
  // not inserted — already submitted today
}

Notes and pitfalls: comparing a TIMESTAMP to a PHP string like date('Y/m/d') often fails because formats differ; prefer DATE(date_create_log) = CURDATE() in queries or rely on the generated DATE column above. Keep server and DB timezones aligned (or use UTC). Avoid check-then-insert without a DB constraint — it is vulnerable to race conditions. Use prepared statements to prevent SQL injection. For more on generated columns see the MySQL docs: generated columns, and for PHP prepared statements see mysqli::prepare.

You may use session or user ID and check if it is same, then you can disable the submit!

commented: thanks for giving me idea +0
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.