Jump to content

Date entry in form


sfia

Recommended Posts

	$value0 = $_POST['date1'];
    $value1 = $_POST['date2'];
    $value2 = $_POST['date3'];

$sql = "INSERT INTO Datetable (startdate,enddate, total)
VALUES ('$value0','$value1', '$value2')" ;

$result = mysqli_query($sql);

Hi,

I was wondering is it possible to not insert values in MySQL if form entry is left blank? Right now if I dont enter any values in form for my dates than MySQL entry shows 0000-00-00 but I need it not to show anything.

 

image.png.e833e1283f01d2537b527e440998aa50.png

Link to comment
Share on other sites

Yes. You would accomplish that by writing code to prevent it from inserting values into MySQL if they are left blank.

Also, you need to switch to prepared statements now. You can continue using mysqli if you wish, but much of the PHP community prefers PDO because it is a little easier to use.

Link to comment
Share on other sites

    $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
	$form=$_POST;
	$startdate =$form[ 'date1' ];
	$enddate=$form[ 'date2' ];
	$total=$form[ 'date3' ];

    $sql = "INSERT INTO Datetable (startdate, enddate, total) 
	VALUES (:startdate, :enddate, :total)";
$query= $conn->prepare( $sql );
$query->execute( array( ':startdate'=>$startdate, ':enddate'=>$enddate, ':total'=>$total ) );

 

I converted mysqli as suggested, and now using PDO and it seems to be working, but I'm completely stumped on how can i create a code to prevent empty values in form for date to pass to database as 0000-00-00. 

Could someone please help me with that? Or just point to similar thread I can read up on how to make that code?

Also what is your opinion on PDO being slower and not as robust as mysqli?

Thank you.

 

Link to comment
Share on other sites

9 hours ago, sfia said:

Like something like this? IDK that doesnt seem to work 

Is validDate() defined in your script? Note that PHP has a built-in date checker. More information can be found here:
https://www.php.net/manual/en/function.checkdate.php

If you scroll down to the "User Contributed Notes" section, there is a user-defined function validateDate() that you could experiment with.

Link to comment
Share on other sites

I followed that link and used validateDate function below and now my script works!. thank you. Appreciate the help. Just trying to learn some of PHP.

function validateDate($date, $format = 'Y-m-d')
{
    $d = DateTime::createFromFormat($format, $date);
    return $d && $d->format($format) == $date;
}

 

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.