Click here to Skip to main content
15,941,444 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
I need to compare the dates in the form that the later dates should not be earlier than the first one and this is works when I use Javascript. However, I need to compare another field(num_file_ptj) from the form with the database if its already existed and I'm kind of clueless on this since the form already have onsubmit function.

What I have tried:

I comment out the onsubmit function on the form and use PHP script but it seems the data wont enter the database
Posted
Updated 8-Jun-24 22:00pm
v2

1 solution

If that column can only ever have one record with a certain value in it, make the column unique in your table. When you execute the insert statement, you will get an error code if the value is a duplicate.

I can't tell you what that error will be because I have no idea what database you are using.
 
Share this answer
 
Comments
Member 16279690 9-Jun-24 4:24am    
I am using MySQL database. Not sure if MySQL can execute error if the value is duplicate. If I put the duplicate value on the unique column on the form, after clicking the submit button, the page return blank. I'm still newbie on this
Pete O'Hanlon 9-Jun-24 4:33am    
You get error code 1062 back. Check the error immediately after you make the insert statement and you should be good to go. You need something like

if (mysqli_error() == 1062){ tell the user there was a duplicate }

Note, you should use define a constant rather than use the magic number of 1062.
Member 16279690 9-Jun-24 4:52am    
Seems like didn't work from my end :( Not sure if I was getting error code 1062 cause when I inspect the browser, it only shows have 1 issue
The script is like this:
// Finally add to registration table
$stmt = mysqli_prepare($connection, "INSERT INTO tblapplication (no_file,no_file_md,no_file_ptj,client_name,client_phone,client_email,ptj_id,status) VALUES (?,?,?,?,?,
(SELECT tblptj.ptj_id FROM tblptj WHERE tblptj.reg_date=? AND tblptj.pass_date=? AND tblptj.ljt_pay=? AND tblptj.ljt_certificate=? AND tblptj.qt_reg=? AND tblptj.qt_pass=?),?);");

mysqli_stmt_bind_param($stmt, 'sssssssssssss', $_POST["no_file"],$_POST["no_file_md"],$_POST["no_file_ptj"],$_POST["client_name"],$_POST["client_phone"],$_POST["client_email"]
,$_POST["reg_date"],$_POST["pass_date"],$_POST["ljt_pay"],$_POST["ljt_certificate"],$_POST["qt_reg"],$_POST["qt_pass"]
,$_POST["status"]);
/* execute prepared statement */
mysqli_stmt_execute($stmt);

/* close statement and connection */
mysqli_stmt_close($stmt);

if (mysqli_error() == 1062){
$msg = "
  Please check your email!
";
}

The form is still using onsubmit function which check the dates. For email or any unique column, not sure how to execute the error
Pete O'Hanlon 9-Jun-24 5:41am    
Your error handling is attempting to check the result of closing the connection, not executing the insert.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900