PHP Program to find the even-odd number with form and database
PHP Program to find the even-odd number with form and database
In this tutorial, we will learn about the followings;
- PHP Program to find the even-odd number
- PHP Program to find the even-odd number with form
- PHP Program to find the even-odd number with a database
PHP Program to find the even-odd number
1 2 3 4 5 6 7 8 9 10 11 12 13 | <?php $a=4; if($a%2==0) { echo $a ." is Even number"; } else { echo $a . " is odd number"; } echo "<title>Even | Odd</title>"; ?> |
PHP Program to find the even-odd number with form
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | <html> <body> <form method="post" action=""> <input type="text" name="num" placeholder="Enter Any Number"> <input type="submit" name="sub" value="calculate"> </form> </body> </html> <?php if(isset($_POST['sub' ])) { $NUM=$_POST['num']; if($NUM%2==0) { echo "<h2>".$NUM. " is even number </h2>"; } else { echo "<h2>".$NUM. " is Odd number </h2>"; } } ?> |
PHP Program to find the even-odd number with a database
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | <html> <body> <form method="post" action=""> <input type="text" name="num"> <input type="submit" name="sub" value="calculate"> </form> </body> </html> <?php include 'connection.php'; if(isset($_POST['sub'])) { $NUM=$_POST['num']; if($NUM%2==0) { mysql_query("insert into even(number) values('$NUM')"); $sel=mysql_query("select * from even"); echo $NUM . " is even"; } else { mysql_query("insert into odd(number) values('$NUM')"); $sel=mysql_query("select * from Odd"); echo $NUM. " is odd"; } } ?> |
now creates the connection.php file to make a database connection.
connection.php
1 2 3 4 5 6 7 8 | <?php $conn = mysql_connect('localhost', 'root', ''); if (!$conn) { die('Could not connect: ' . mysql_error()); } mysql_select_db("cal", $conn); ?> |