Wednesday 21 December 2011

Set up LAMP Server at home

1. Follow Ubuntu's Help to Install LAMP Server

    https://help.ubuntu.com/community/ApacheMySQLPHP

2. Create a simple PHP file in  /var/www/
  • Open a Terminal.
  • sudo gedit /var/www/info.php
  • Type your Password. Press Enter.
  • Paste the Following Code into opened File(/var/www/info.php).

<?php
echo "Hey There , This is Working."
phpinfo();
?>
  •   Save the File.
3. Start your Apache Server.
  •  sudo service apache2 restart
4.Open the PHP page in Browser.
  • http://localhost/info.php
  • http://127.0.0.1/info.php
  • http://<ur IP address>/info.php
5.If <You can see the below page> Continue Else Go To Step 1.
  • http://localhost/info.php
6.Create a sample MySQL Database .
  • Open a Terminal
  • admin@mooshtoo:~$ mysql -u root -p
  • Enter password:
  • Copy the Following Commands in the Terminal:

create database record;
create table record.student(
rno int,
name varchar(40),
phy int,
chem int,
math int
);

7.Create a Form in HTML.
  • sudo gedit  /var/www/StudentForm.html
  • Enter Password:
  • Paste the Following Code in StudentForm.html and Save.

<HTML>
<Head>
      <title>Insert Student Record</title>
      <style type="text/css">
                  table{
                  border:1;
                  border-collapse:collapse;
                  font: normal 12px 'Lucida Grande',Verdana,sans-serif;
                  }
                  td{
                  color:#663333;font-family:verdana;
                  border-bottom: 1px solid #666;
                  padding-left:10px;
                  background-color:#F0F8FF;
                  }
                  #sub{ text-align:center;}
      </style>
</Head>
<body>
      <h2> Student Record Form </h2>
      <form action="dbinsert.php" method="POST" id="insert">
                  <table>
                              <tr>
                                       <td>Roll Number</td>
                                       <td><input type="text" size=40 name="rno"></td>
                              </tr>
                              <tr>
                                       <td>Name</td>
                                       <td><input type="text" size=40 name="name"></td>
                              </tr>
                              <tr>                                          
<td >Physics</td>
                                          <td><textarea  name="phy" cols=3 rows=1></textarea> </td>
                              </tr>
                              <tr>
                                          <td>Chemistry</td>
                                          <td><textarea  name="chem"  cols=3 rows=1></textarea> </td>
                              </tr>
                              <tr>
                                          <td>Mathematics</td>
                                          <td><textarea  name="math"  cols=3 rows=1></textarea> </td>
                              </tr>
                              <tr>
                                          <td colspan=2 id="sub"><input type="submit" name="submit" value="submit" ></td>
                              </tr>
                  </Table>
      </form>
</BODY>
</Html>


8. Now Write a PHP script to process the Student Form.
  • Create a new file  /var/www/dbinsert.php
  • Paste the following code in it.
<?php

    $con = mysql_connect("localhost","root","<Your password>");
    if (!$con)
    {
        die('Could not connect: ' . mysql_error());
    }
    mysql_select_db("record", $con);
    $result = mysql_query("SELECT * FROM record.student");
    //Get data in local variable
    $v_rno=$_POST['rno'];
    $v_name=$_POST['name'];
    $v_phy=$_POST['phy'];
    $v_chem=$_POST['chem'];
    $v_math=$_POST['math'];
    // check for null values
    if ($v_rno==""  or $v_name=="")
    echo "All fields must be entered, hit back button and re-enter information";
    else{
    $query="insert into record.student      values($v_rno,'$v_name',$v_phy,$v_chem,$v_math)";
    mysql_query($query)  or die(mysql_error());
    echo "Your message has been received";
        }
?>
<br><br>
<!-- For Navigation -->
<a href='insertform.html'>Insert Another Record</a>
&nbsp; &nbsp;
<a href='dbresult.php'>Student Record</a>
9.Write another PHP script to Display Student Records.
  • Create a new file  /var/www/dbresult.php
  • Paste the following code in it.
<?php
$con = mysql_connect("localhost","root","<Your password>");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }
mysql_select_db("record", $con);
$result = mysql_query("SELECT * FROM record.student");
?>
<table border='1'>
<tr>
<th>Roll No.</th>
<th>Name</th>
<th>Physics</th>
<th>Chemistry</th>
<th>Mathematics</th>
</tr>
<?php
while($row = mysql_fetch_array($result))
{
  echo "<tr>";
  echo "<td>" . $row['rno'] . "</td>";
  echo "<td>" . $row['name'] . "</td>";
  echo "<td>" . $row['phy'] . "</td>";
  echo "<td>" . $row['chem'] . "</td>";
  echo "<td>" . $row['math'] . "</td>";
  echo "</tr>";

}
    mysql_close($con);
?>
        </table>
<br><br>
<!-- For Navigation -->

<a href='insertform.html' alt='New%20Student%20Record' >Insert Another Record</a>
10. Now test the following links in your browser.
http://127.0.0.1/insertform.html
http://127.0.0.1/dbinsert.php
http://127.0.0.1/dbresult.php