Connection with Database in PHP


Before storing the php form data in MySql it is essential to establish connection with PhpMyAdmin and MySql database. Php has an inbuilt function 'mysql_connect( )', which is used to make connection with PhpMyAdmin and the function 'mysql_select_db( )', is used to make the connection with database.

SYNTAX OF USING MYSQL_SELECT( ) AND MYSQL_SELECT_DB( )

@mysql_select('host name', 'username', 'password');
@mysql_select_db('database name');

mysql_connect( ) function takes three parameters. First parameter is host name, Second parameter is username and the third parameter is password.

Mysql_select_db( ) function take only one parameter that is database name.

<?php
@mysql_connect('localhost','root','') or die('check username and password');
@mysql_select_db('students') or die('not connected');
?>
@ sign is used to hide the warning message. If you don't use @ sing  error message will appear in browser if any error occurred in the connection with PhpMyAdmin and database. To hide this warning message @ sign is used before the statement.

Die() statement is used to show the user defined error message when any error occurred in the connection with PhpMyAdmin or with database. it is advisable not to use die() statement when you are working on live project because no one wants to show any error message to his/her users.

Below is the complete advisable code to make connection with PhpMyAdmin and with Database
<?php
@mysql_connect('localhost','root','');
@mysql_select_db('your database name');
?>
After writing the above code in the notepad save it with .php extension. When you execute the above code in the browser nothing will be displayed if you get anything displayed in the browser it shows there is any mistake occurred in the code.

You can also write the above code in this way
<?php
$host = 'localhost';
$username = 'root';
$password = '';
$dbname = 'your database name';
@mysql_connect($host,$username,$password);
@mysql_select_db($dbname);
?>

Note :- The above code is only applicable for wampserver. For your domain username and password may be different.


How to collect form data in php


Php form is a document that contain blank fields, that the user can fill the date or user can select the data. This data will be collected by 'get' or 'post' method and stored in the database. Php form is simply HTML form with no special tag.

SYNTAX OF PHP FORM
<form action = " " method = " " >
</form >

Example :-
text.php
<!DOCTYPE html>
<html>
<head><title>expertcodingmaster.com</title>
</head>
<body>
<center>
<div>
<table border="1" style="width:30em;">
<form action="data.php" method="post">   // you can also use 'get' method
<tr><td>Name</td><td><input type="text" name="name"></td></tr>
<tr><td>Email</td><td><input type="email" name="email"></td></td>
<tr><td>Comment</td><td><textarea name="comment" rows="3" cols="20"></textarea></td></td>
<tr align="center"><td colspan="2"><input type="submit" name="submit"></td></tr>
</form>
</div>
<center>
</body>
</html>
The above example containing a simple HTML form with two input fields and one text area for comment and a submit button to collect the filled form data. To collect form data HTTP POST method is used you can also use HTTP GET method to collect the form data. Write the above code in notepad and save it as text.php.

This form data is collected on data.php which look likes this
data.php
<?php
$_POST['submit'];     // if you used 'post' method then use $_POST and if you used 'get' method  then use $_GET method.
{
 $name = $_POST['name'];
 $email = $_POST['email'];
 $comment = $_POST['comment'];

 echo "Your name is :-" . " " . $name . "<br>";
 echo "your email is :-" . " " . $email . "<br>";
 echo "your comment is :-" . " " . $comment;
}
?>
For the real programming scenario don't perform the echo function. Here echo function is performed to show you how the code is working.

Output of the above code
Your name is :- mukesh
your email is :- mukeshsingh.singh66@gmail.com
your comment is :- expertcodingmaster.com contain complete programming solution 

The above complete example shows the collection of form date in different page but you can also collect the form date in the same page. See the below example how to collect the form data in the same page. For this we only need to write the data.php code in the same page that in on the text.php.
And to collect the form data php inbuilt function 'isset' is used. And making the form action attribute empty.

Example 2:-
text.php
<?php
if(isset($_POST['submit']))   // isset function come in action when the submit button is clicked
{
 echo "Your name is :- " . " " . $name = $_POST['name']. "<br>";
 echo "your email is :- " . " " . $email = $_POST['email'] . "<br>";
 echo "your comment is :- " . " " . $comment = $_POST['comment'];
}
?>
<!DOCTYPE html>
<html>
<head><title>expertcodingmaster.com</title>
</head>
<body>
<center>
<div>
<table border="1" style="width:30em;">
<form action="" method="post">
<tr><td>Name</td><td><input type="text" name="name"></td></tr>
<tr><td>Email</td><td><input type="email" name="email"></td></td>
<tr><td>Comment</td><td><textarea name="comment" rows="3" cols="20"></textarea></td></td>
<tr align="center"><td colspan="2"><input type="submit" name="submit"></td></tr>
</form>
</div>
<center>
</body>
</html>

Difference between 'GET METHOD' and 'POST METHOD' :-

GET METHOD :-
1. GET Method is visible to everyone. It will be displayed in the browsers address bar.
2. GET Method is has limits on the amount of information send.
3. GET is less secure compared to POST because data sent is part of the URL. So it's saved in browser history and server logs in plain text.

POST MTHOD :-
1. POST Method variables are not displayed in the URL.
2. POST Method has no limitation.
3. POST is a little safer than GET because the parameters are not stored in browser history or in web server logs.





Time and Date function in Php


Time and Date function is used to display date and time in your webpage or to check the last view of the user.

Syntax for using Time and Date function

date_default_timezone_set("your time zone");
date('d-m-y h:i:s A');

Example :-
<!DOCTYPE html>
<html>
<head><title>expertcodingmaster.com</title>
</head>
<body>
<?php
/* below are the example of different format to display Date and Time which can be used according to your requirement  */ 
date_default_timezone_set("Asia/Kolkata");    // set your own time zone 
echo date('d-m-y h:i:s a') . "<br>";
echo date('D-M-Y h:i:s A') . "<br>";
echo date('D-M-Y H:i:s A') . "<br>";
echo date('D-M-Y h:I:s A') . "<br>";
echo date('D-M-Y h:i:S A') . "<br>";
echo date('D-M-Y h:i:s A') . "<br>";
echo  date('d-m-y') . "<br>";
echo  date('h:i:s a') . "<br>";
?>
</body>
</html>

Output of the above code
31-01-17 12:16:27 am

Tue-Jan-2017 12:16:27 AM


Tue-Jan-2017 00:16:27 AM


Tue-Jan-2017 12:0:27 AM


Tue-Jan-2017 12:16:st AM


Tue-Jan-2017 12:16:27 AM


31-01-17


12:16:27 am




While loop in Php


While loops are used to execute a block of code again and again until a specified condition is true.
In while loop at first initialisation is done. In initialisation we assign any value to a variable.

Syntax of while loop

while(condition)
{
         block of code
}

Example :-
<!DOCTYPE html>
<html>
<head><title>expertcodingmaster.com</title>
</head>
<body>
<?php
/*  suppose we want to print 1-20  */
$i = 1;      // initialisation of varable
while($i<=20)
{
echo $i . "<br>";
$i++;
}
?>
</body>
</html>

Output of the above code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20