0

I have a little problem here. I am new to either PHP and JS, so. I have a PHP code with MySQL database inside of it. In JS file I have written the code, which checks if the user's entered date is earlier or not than today's date. Now it is adding all the users to the database, but I need to add just the users which has entered earlier than today's date (add just the users with TRUE if statement). How should I do that? Thanks for any help.

Here is my part of the JS code:

if (x.getTime() < today.getTime()) {
    alert ("Employees has been succesfully added to the database");
}
else
{
    alert ("You have entered date which is later than todays date. Re-enter it");
}
}

Here is my PHP code:

<?php
try {
$db = new PDO( 'mysql:host=127.0.0.1;dbname=employees;charset=utf8','root','' );
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

if (isset($_POST['Submit'])) {
$name = $_POST['name'];
$surname = $_POST['surname'];
$employmentDate = $_POST['employmentDate'];

$insert = $db->prepare("INSERT INTO employee (name,surname,employment_date) VALUES (:name,:surname,:employmentDate)");

$insert->bindParam(':name',$name);
$insert->bindParam(':surname',$surname);
$insert->bindParam(':employmentDate',$employmentDate);
$insert->execute();
}
}
catch (PDOException $e) {
echo "There is something wrong with the database".$e->getMessage();
die();
}
?>
user7435747
  • 417
  • 2
  • 5
  • 11
  • You should not trust the date coming from the client, you have no idea how it was generated, or what time and date the host is set to. What are *x* and *today*? I guess they're Dates, but how are they generated? – RobG Feb 09 '17 at 20:38
  • Can you include a little more? I assume your php code is where the data is actually getting inserted into the DB right? What if you put that check in the php code and only called the sql insert if the date was valid. – mparis Feb 09 '17 at 20:39
  • I've added my PHP code – user7435747 Feb 09 '17 at 20:41

1 Answers1

0
var today, someday, text;
today = new Date();
someday = new Date();
someday.setFullYear(2100, 0, 14);

if (someday > today) {
    alert("Today is before January 14, 2100.");
return;
 }
   ...Database insert Code here

Edited to answer your question i believe. You need to only call the database add if your in the right scope block.

Check out W3 schools: http://www.w3schools.com/js/js_date_methods.asp

If you want to make life easier, use moment.js https://momentjs.com/

Dys1
  • 90
  • 8
  • I did that, my JS code just checks if the user's entered date in a form (whch is in php file) is no later than today and either if its true or not it prints the result. I think you did'nt understood my quetion. – user7435747 Feb 09 '17 at 20:56
  • I don't think there's anything here that isn't in answers to [*Compare two dates with JavaScript*](http://stackoverflow.com/questions/492994/compare-two-dates-with-javascript). – RobG Feb 09 '17 at 20:58
  • You need to either exit if the date validation fails or put the db insert in the scope that is correct. Im guessing you have your database code below the IF block, thus its always firing – Dys1 Feb 09 '17 at 21:25