Wednesday, September 21, 2011

Running PHP Programs

PHP + MySQL Program

Question - To create simple applications like login forms after setting up a LAMP Stack.

Here however we are designing a PHP application that obtains the information from a student and stores it in the MySQL database.

(This post assumes that you have created a LAMP Stack. If not Click Here to refer to my previous post) 


Steps Involved


1.       Go to the terminal and login as root.

2.       Move to the /var/www/html folder by entering the following command:
[root@fosslab fosslab]# cd /var/www/html

3.       Create a filenamed ‘phpdata.php’ using vi editor.

[root@fosslab html]# vi phpdata.php

Now inside enter the following code

phpdata.php

<html>

<head>

                <title>Student Registration!</title>

</head>

<body bgcolor="lightgreen">

<b><center>    <h2>Student Registration Form</h2>

                    <form action="handle_register.php" method="post">

                    <table cellpadding="10" cellspacing="10">          

                    <tr>

                    <td><label for="lbl_name">Name</td>

                    <td><input type="text" name="name" ></td>

                    </tr>

                    <tr>

                    <td><label for="lbl_email">Email Id</td>

                    <td><input type="text" name="email"></td>

                    </tr>

                    <tr>

                    <td><label for="lbl_mobile">Mobile Number</td>

                    <td><input type="text" name="mobile"></td>

                    </tr>

                    <tr>

                    <td><label for="lbl_college">College</td>

                    <td><input type="text" name="college"></td>

                    </tr>

                    <tr>

                    <td><label for="lbl_degree">Degree</td>

                    <td><input type="text" name="degree"></td>

                    </tr>

                    <tr>

                    <td><label for="lbl_branch">Branch</td>

                    <td><input type="text" name="branch"></td>

                    </tr>

                    <tr>

                    <td><label for="lbl_year">Year</td>

                    <td><input type="text" name="year"></td>

                    </tr>

                    <tr>

                    </table>    

                    <input type="submit" value="Register" name="Submit">

                                    </form></center></b>                          

</body>

</html>

4.       Now save the file and create another one named ‘handle_register.php’.
[root@fosslab html]# vi handle_register.php

Now enter the following code:

handle_register.php


<?php

if($_POST['name']=="" || $_POST['email']=="" || $_POST['mobile']=="" || $_POST['college']=="" || $_POST['degree']=="" || $_POST['branch']=="" || $_POST['year']=="")

{

echo "<h3>Please go back and fill in all the details.</h1>";

die();

}

else

{

$name = $_POST['name'];

$email = $_POST['email'];

$mobile = $_POST['mobile'];

$college = $_POST['college'];

$degree = $_POST['degree'];

$branch = $_POST['branch'];

$year = $_POST['year'];

$connect = mysql_connect("localhost","root","admin123") or die( "Unable to select database");

mysql_select_db("register");

$query = "SELECT * from students where Email='$email'";

$results = mysql_query($query);

$query = "INSERT INTO students (Name,Email,Mobile,College,Degree,Branch,Year) VALUES ('$name','$email','$mobile','$college','$degree','$branch','$year')";

$results = mysql_query($query);

echo "<h2>Thank you " . $name . " for registering.</h2>";

echo "<br><br>List of Students Enrolled:<br><br>";

echo "<table border='2' cellpadding='3'><tr><td>Name</td><td>Email</td><td>Mobile</td><td>College</td><td>Degree</td><td>Branch</td><td>Year</td></tr>";

$query = "SELECT * from students";

$results = mysql_query($query);

while($info = mysql_fetch_array($results))

{

echo "<tr><td>".$info['Name']."</td><td>".$info['Email']."</td><td>".$info['Mobile']."</td><td>".$info['College']."</td><td>".$info['Degree']."</td><td>".$info['Branch']."</td><td>".$info['Year']."</td></tr>";

}

echo "</table>";

}

mysql_close();

?>

Now you have completed your coding part. Next step is to create database in MySQL. We use PhpMyAdmin for it.

Steps for creating database in PhpMyAdmin

1.       In your browser type http://localhost/phpmyadmin

2.       Click the Databases tab. In the Create New Databse textbox type register and click create. 

3.       Next in the create new table frame enter name as ‘students’ and number of fields as ‘7’. Click Go. Now fill in the following

Field
Type
Name
Text
Email
Text
Mobile
Text
College
Text
Degree
Text
Branch
Text
Year
Text
4.        
      Now finally click Save button below.
5.       Now that’s it!! You have created the database.

Executing the Program

1.       Go to the browser and type http://localhost/phpdata.php.

2.       Fill in the details (all details) and click Register.

3.       Now in the next page a Thank you message and list of entries in database will come indicating the success of the program.

Download the Source Code