Jump to content

PDO update sql form not working


webdeveloper123
Go to solution Solved by Barand,

Recommended Posts

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

Link to comment
Share on other sites

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

        image.png.4dca52180f499d926e7f7c248dd120da.png

With $format='Y-m-d' I get

        image.png.bb94a2ad29387ace162fe220c1f33449.png

Link to comment
Share on other sites

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

 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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"

Link to comment
Share on other sites

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

 

Link to comment
Share on other sites

/* 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 by Strider64
Link to comment
Share on other sites

@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() ).

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

 

Link to comment
Share on other sites

  • Solution

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...

                    image.png.b4e3bc494ac39d437038bc16a1473792.png

All the clues are there but you seem to be ignoring them

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.