Site icon T4Tutorials.com

PHP Program To Find The Leap year with form and database

PHP Program To Find The Leap year with form and database

In this tutorial, we will learn about the followings.

  1. PHP Program To Find The Leap year.
  2. PHP Program To Find The Leap year with a form.
  3. PHP Program To Find The Leap year with the database.

PHP Program To Find The Leap year.

In this example, we will take the inputs from the user and store them in variables.

<?php
$value;
$value = 2012;
echo "Enter a year <br>";
echo "$value <br>";
if($value % 4 == 0)
{
    echo "$value is a leap year";
}
else
{
	echo "$value is Not a leap year";
}
?>

PHP Program To Find The Leap year with a form.

In this example, programmer stores the values in the variables. However, if you interested to see the program about taking values in the form, please see example 2.

<html>
<head></head>
<body>
<form method="post">
Enter a year:<input type = "text" name="year" /><br>
<input  type="submit" id="btnsubmit" name="btnsubmit" value="submit" style ="align:center;margin-left: 135px;margin-top: 5px;"/>
</form>	
</body>
</html>

<?php
/*include 'db1.php';*/

 if(isset($_POST['btnsubmit']))
 {
	$value = 0; 
$value = $_POST['year'];
 
 // echo $value;

if($value % 4 == 0)
{
    echo $value. " is a leap year";
}
else
{
	echo $value. " is Not a leap year";
}
 }
?>

PHP Program To Find The Leap year with the database.

In this example, we will take the inputs from the user and temporarily store them in variables and then finally stores them permanently in the database.

<html>
<head></head>
<body>
<form method="post">
Enter a year:<input type = "text" name="year" /><br>
<input  type="submit" id="btnsubmit" name="btnsubmit" value="Year" style ="align:center;margin-left: 135px;margin-top: 5px;"/>
</form>	
</body>
</html>

<?php
include 'database.php';

if(isset($_POST['btnsubmit']))
{
	
$value=$_POST['year'];
 $temp=$value;
 // echo $value;

if($value % 4 == 0)
{
    echo "$value is a leap year";
    $v1=" is a leap year";
}
else
{
	echo "$value is Not a leap year";
	$v1=" is Not a leap year";
}


$sql="INSERT INTO leapyear (Years,Result) 
      VALUES ('$temp','$v1') ";

      if(mysqli_query($conn,$sql))
      {
      //	echo "record successfully";
      }
      }
?>

database.php

<?php
$servername="localhost";
$username="root";
$password="";
$dbname="LeapYear";
//create a connection 
$conn= new mysqli($servername,$username,$password,$dbname);

//check connection
if($conn->connect_error)
{
	die ("connection  failed:" . $conn->connect_error);
	
}
//connected  successfully
else
{
	//echo "connected successfully.";
}
?>

Database of Program To Find The Leap year with the database.

Figure: Leap Year program in PHP with the database.

 

Exit mobile version