webdeveloper123 Posted July 29, 2022 Author Share Posted July 29, 2022 2 minutes ago, Barand said: However it will be sent in the POST data as Y-m-d, therefore your expected input format is "Y-m-d". ok I'm getting a little confused. I just changed it to 'd/m/Y' and the error is gone. So should it be d-m-Y or Y-m-d? Quote Link to comment https://forums.phpfreaks.com/topic/315097-pdo-update-sql-form-not-working/page/2/#findComment-1598748 Share on other sites More sharing options...
Barand Posted July 29, 2022 Share Posted July 29, 2022 The input, as stated, displays d/m/Y but printing the posted data shows it is sent in Y-m-d format So the input format you are validating is Y-m-d Quote Link to comment https://forums.phpfreaks.com/topic/315097-pdo-update-sql-form-not-working/page/2/#findComment-1598749 Share on other sites More sharing options...
webdeveloper123 Posted July 29, 2022 Author Share Posted July 29, 2022 ok so the storage format and the defining input format should be the same? Quote Link to comment https://forums.phpfreaks.com/topic/315097-pdo-update-sql-form-not-working/page/2/#findComment-1598750 Share on other sites More sharing options...
webdeveloper123 Posted July 29, 2022 Author Share Posted July 29, 2022 I printed both my $customers and $errors array and customers looks fine but this is my errors array Array ( [first_name] => [last_name] => [address] => [town] => [county] => [post_code] => [fav_food] => [birthdate] => Invalid date [email] => [terms] => ) It's still containing Invalid Date error message, which is probably why the record is not updating Quote Link to comment https://forums.phpfreaks.com/topic/315097-pdo-update-sql-form-not-working/page/2/#findComment-1598752 Share on other sites More sharing options...
Barand Posted July 29, 2022 Share Posted July 29, 2022 No - they are independent of one another. They can be be the same but do not have to be. your users may be typing 29/07/22 into a text input so your expected format is "d/m/y" but your output format for the db field would still be Y-m-d, requiring conversion of the input . My test code... <?php if ($_SERVER['REQUEST_METHOD']=='POST') { echo '<pre>' . print_r($_POST, 1) . '</pre>'; echo check_date($_POST['birthdate']) ? 'Date is valid' : 'Date is NOT valid'; } function check_date($input, $format='d/m/Y') { $date = DateTime::createFromFormat($format, $input); return ($date && $date->format($format) === $input); } ?> <form method='POST'> <input type='date' name='birthdate'> <input type='submit'> </form> With input format defined as "d/m/Y" (which you said "worked") my output is With $format='Y-m-d' I get Quote Link to comment https://forums.phpfreaks.com/topic/315097-pdo-update-sql-form-not-working/page/2/#findComment-1598753 Share on other sites More sharing options...
webdeveloper123 Posted July 29, 2022 Author Share Posted July 29, 2022 Hey barand I ran your code and got this error: Fatal error: Uncaught TypeError: print_r() expects parameter 2 to be bool, int given in /var/www/vhosts/ btw, my function format is 'Y/m/d' Quote Link to comment https://forums.phpfreaks.com/topic/315097-pdo-update-sql-form-not-working/page/2/#findComment-1598754 Share on other sites More sharing options...
Barand Posted July 29, 2022 Share Posted July 29, 2022 Then change the "1" to "true" if you're using strict variable typing (There is a manual!) 2 minutes ago, webdeveloper123 said: btw, my function format is 'Y/m/d' Not what you said earlier ... 54 minutes ago, webdeveloper123 said: I just changed it to 'd/m/Y' and the error is gone Quote Link to comment https://forums.phpfreaks.com/topic/315097-pdo-update-sql-form-not-working/page/2/#findComment-1598755 Share on other sites More sharing options...
webdeveloper123 Posted July 29, 2022 Author Share Posted July 29, 2022 2 minutes ago, Barand said: (There is a manual!) lol 2 minutes ago, Barand said: Not what you said earlier ... 1 hour ago, webdeveloper123 said: Yes I said I changed it to 'd/m/Y' and the error had gone (on screen error) then I was asking should it be 'd/m/Y' or 'Y/m/d' and from your reply you said Y/m/d so I changed it to Y/m/d Quote Link to comment https://forums.phpfreaks.com/topic/315097-pdo-update-sql-form-not-working/page/2/#findComment-1598756 Share on other sites More sharing options...
webdeveloper123 Posted July 29, 2022 Author Share Posted July 29, 2022 I get a very neat looking array output and in that there is: [birthday] => 1991-02-27 Then I get: Notice: Undefined index: birthdate in /var/www/vhosts on the echo check_date line and then I get: Date is NOT valid Quote Link to comment https://forums.phpfreaks.com/topic/315097-pdo-update-sql-form-not-working/page/2/#findComment-1598758 Share on other sites More sharing options...
Barand Posted July 29, 2022 Share Posted July 29, 2022 IBM has an excellent corporate motto - "THINK!" Quote Link to comment https://forums.phpfreaks.com/topic/315097-pdo-update-sql-form-not-working/page/2/#findComment-1598759 Share on other sites More sharing options...
webdeveloper123 Posted July 29, 2022 Author Share Posted July 29, 2022 oh yes my mistake. I corrected it and it still says "Invalid date" in my errors array and it still says : Date is NOT valid Quote Link to comment https://forums.phpfreaks.com/topic/315097-pdo-update-sql-form-not-working/page/2/#findComment-1598760 Share on other sites More sharing options...
Barand Posted July 29, 2022 Share Posted July 29, 2022 Quote Link to comment https://forums.phpfreaks.com/topic/315097-pdo-update-sql-form-not-working/page/2/#findComment-1598761 Share on other sites More sharing options...
webdeveloper123 Posted July 29, 2022 Author Share Posted July 29, 2022 sorry I must have not made it clear I changed it to Y-m-d Quote Link to comment https://forums.phpfreaks.com/topic/315097-pdo-update-sql-form-not-working/page/2/#findComment-1598762 Share on other sites More sharing options...
Barand Posted July 29, 2022 Share Posted July 29, 2022 20 minutes ago, webdeveloper123 said: I corrected it and it still says "Invalid date" in my errors array and it still says : Date is NOT valid What were you expecting as a reply to that statement? I have no way of knowing what exact code gave you that result and with what input value. I ain't psychic. Quote Link to comment https://forums.phpfreaks.com/topic/315097-pdo-update-sql-form-not-working/page/2/#findComment-1598764 Share on other sites More sharing options...
webdeveloper123 Posted July 29, 2022 Author Share Posted July 29, 2022 Ok So I have this code: if (!check_date($customers['birthdate']) ) { $errors['birthdate'] = 'Invalid date'; } else { $errors['birthdate'] = ''; } Which is giving me "Invalid Date" in my errors array And the test code you gave me higher up the thread which is: echo check_date($_POST['birthdate']) ? 'Date is valid' : 'Date is NOT valid'; I get "Date is NOT valid" Quote Link to comment https://forums.phpfreaks.com/topic/315097-pdo-update-sql-form-not-working/page/2/#findComment-1598765 Share on other sites More sharing options...
Barand Posted July 29, 2022 Share Posted July 29, 2022 19 minutes ago, Barand said: and with what input value plus I can't see the current state of your function code which is a fairly important component, don't you think?. This getting to be too much like hard work. Quote Link to comment https://forums.phpfreaks.com/topic/315097-pdo-update-sql-form-not-working/page/2/#findComment-1598767 Share on other sites More sharing options...
webdeveloper123 Posted July 29, 2022 Author Share Posted July 29, 2022 1 minute ago, Barand said: 24 minutes ago, Barand said: and with what input value sorry barand missed that part this is my function: function check_date($input, $format='Y/m/d') { $date = DateTime::createFromFormat($format, $input); return ($date && $date->format($format) === $input); } and the input value was: 27/02/1991 Quote Link to comment https://forums.phpfreaks.com/topic/315097-pdo-update-sql-form-not-working/page/2/#findComment-1598768 Share on other sites More sharing options...
Strider64 Posted July 29, 2022 Share Posted July 29, 2022 (edited) /* Check for Validity */ function checkDate($input) { return strtotime($input); } $format='Y/m/d' // Format to Display $valid = checkDate($input); // Check to see if user input is valid if ($valid && isset($input) && strlen($input) === 10) { $date = DateTime::createFromFormat($format, $input); } A different way in doing this Edited July 29, 2022 by Strider64 Quote Link to comment https://forums.phpfreaks.com/topic/315097-pdo-update-sql-form-not-working/page/2/#findComment-1598769 Share on other sites More sharing options...
Barand Posted July 29, 2022 Share Posted July 29, 2022 @Strider64 According to your code, 29.07.2022 and 07/29/2022 both give a value of true in $valid but you will end up writing an empty date value to the db if you use $date as neither comply with the expected $format.. Also. checkdate is an existing function. (PHP Fatal error: Cannot redeclare checkDate() ). Quote Link to comment https://forums.phpfreaks.com/topic/315097-pdo-update-sql-form-not-working/page/2/#findComment-1598773 Share on other sites More sharing options...
Barand Posted July 29, 2022 Share Posted July 29, 2022 1 hour ago, webdeveloper123 said: and the input value was: 27/02/1991 What do $customers['birthdate'] and $_POST['birthdate'] contain when it says invalid? They are what the function is testing. Quote Link to comment https://forums.phpfreaks.com/topic/315097-pdo-update-sql-form-not-working/page/2/#findComment-1598774 Share on other sites More sharing options...
webdeveloper123 Posted July 30, 2022 Author Share Posted July 30, 2022 16 hours ago, Barand said: What do $customers['birthdate'] and $_POST['birthdate'] contain when it says invalid? They are what the function is testing. $_POST['birthdate'] holds the value:[birthday] => 1991-02-27 and $customers['birthdate'] holds the value: [birthdate] => 1991-02-27 Quote Link to comment https://forums.phpfreaks.com/topic/315097-pdo-update-sql-form-not-working/page/2/#findComment-1598788 Share on other sites More sharing options...
Barand Posted July 30, 2022 Share Posted July 30, 2022 18 hours ago, webdeveloper123 said: sorry barand missed that part this is my function: function check_date($input, $format='Y/m/d') { $date = DateTime::createFromFormat($format, $input); return ($date && $date->format($format) === $input); } But the format of your dates is "Y-m-d"... 24 minutes ago, webdeveloper123 said: $customers['birthdate'] holds the value: [birthdate] => 1991-02-27 Quote Link to comment https://forums.phpfreaks.com/topic/315097-pdo-update-sql-form-not-working/page/2/#findComment-1598789 Share on other sites More sharing options...
webdeveloper123 Posted July 30, 2022 Author Share Posted July 30, 2022 (edited) Yes that's what you said to put it on, Y-m-d. And then there is the locale setting when using the html date element, which in the forms shows UK date format - d-m-Y Edited July 30, 2022 by webdeveloper123 Quote Link to comment https://forums.phpfreaks.com/topic/315097-pdo-update-sql-form-not-working/page/2/#findComment-1598790 Share on other sites More sharing options...
Solution Barand Posted July 30, 2022 Solution Share Posted July 30, 2022 If you know it's Y-m-d why have you specified Y/m/d ??? 18 hours ago, webdeveloper123 said: function check_date($input, $format='Y/m/d') It's the <input type='date'> that is using the locale to display dd/mm/yyyy. It posts it to your code as yyyy-mm-dd as the print_r($_POST) shows... All the clues are there but you seem to be ignoring them Quote Link to comment https://forums.phpfreaks.com/topic/315097-pdo-update-sql-form-not-working/page/2/#findComment-1598791 Share on other sites More sharing options...
webdeveloper123 Posted July 30, 2022 Author Share Posted July 30, 2022 I thought something was up with the hyphens and slashes but I wasn't sure. I left it as slashes because that's what it had on the locale setting on the form. It's showing up as Date is valid. Thanks so much Barand Quote Link to comment https://forums.phpfreaks.com/topic/315097-pdo-update-sql-form-not-working/page/2/#findComment-1598792 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.