Monday, May 4, 2015

PHP Basics

Basic PHP Lab
Student.html

<html>
<head>
<title> Student Registration Form </title>
</head>
<body>
<center>
<h4>
<form name = "Student Form" method= "POST" action= "display.php">
<table>
<caption><h3>Student Registration Form</h3></caption>
<tr>
<td>Registration number</td>
<td><input type = "text" name="regNo"/></td>
</tr>
<tr>
<td>Student name</td>
<td><input type = "text" name="StdName"/></td>
</tr>
<tr>
<td>Student email</td>
<td><input type = "text" name="email"/></td>
</tr>
<tr>
<td>Student year:</td>
<td><input type = "radio" value="yr3" name="year"/>year 3
<input type = "radio" value="yr4" name="year"/>year 4
</td>
</tr>
<tr>
<td><select name="course">
<option value = "IT">IT</option>
<option value = "BM">BM</option>
<option value = "Engineering">Engineering</option>
</select>
</td>
</tr>
<tr>
<td colspan = "2"><input type="submit" value="insert" name="insert"/>
<input type="submit" value="update" name="update"/>
<input type="submit" value="delete" name="delete"/>
<input type="submit" value="view" name="view"/>
<input type="submit" value="reset" name="reset"/>
</td>
</tr>
</table>
</form>
</h4>
</center>
</body>
</html>
</table>
</h4>
</center>
</body>
</html>



Display.php

<?php
$mysql_host = "localhost";
$mysql_user = "root";
$mysql_pass = "";
$mysql_db = "Student";
if(!mysql_connect($mysql_host, $mysql_user, $mysql_pass) || !mysql_select_db($mysql_db)) {
die(mysql_error());
}

?>

<?php

if(isset($_POST["regNo"]) && isset($_POST["StdName"]) && isset($_POST["email"]) && isset($_POST["year"])
&& isset($_POST["course"]))
                {
                $regno=$_POST["regNo"];
                $name= $_POST["StdName"];
                $email=$_POST["email"];
                $year =$_POST["year"];
                $course=$_POST["course"];
                }
else
                {
                $error = "One or more fields are not filled";
                echo $error;
                }

if(isset($_POST["insert"]))
{
                $insertString="insert into StudentInfo values ('$regno','$name','$email','$year','$course')";
                if(!mysql_query($insertString))
                {
                die('Error : '.mysql_error());
                }
                else
                {
                echo '<br/>';
                echo '1 record added...';
                }
}

else if (isset($_POST["update"]))
{             
                $updateString = "update StudentInfo set name='$name',email='$email',year='$year',course='$course'
                where RegNo='$regno'";
               
                if(!mysql_query($updateString))
                {
                die('Error : '.mysql_error());
                }
                else
                {
                echo '<br/>';
                echo '1 record updated...';
                }
}

else if (isset($_POST["delete"]))
{
                $deleteString = "delete from StudentInfo where RegNo='$regno'";

                if(!mysql_query($deleteString))
                {
                die('Error : '.mysql_error());
                }
                else
                {
                echo '<br/>';
                echo '1 record deleted...';
                }
}

else if (isset($_POST["view"]))
{
$selectString = "select * from StudentInfo";
$comments = mysql_query($selectString);
while ($row = mysql_fetch_array($comments))
                { ?>
                <br>
                <table>
                <tr>
               
                <td><?php echo $row['RegNo']; ?> </td>
                <td><?php echo $row['name']; ?> </td>
                <td><?php echo $row['year']; ?> </td>
                </tr>
                </table>
                </br>
                <?php
                }
}

?>


                                               


No comments:

Post a Comment