Friday, May 1, 2015

CRUD With RAW PHP

1. db_connect.php


<?php
//connect to the database
$mysql_host = "localhost";

$mysql_user = "root";
$mysql_pass = "1991";
$mysql_db = "mtit_final";
if(!mysql_connect($mysql_host, $mysql_user, $mysql_pass) ||
!mysql_select_db($mysql_db)) {
die(mysql_error());
}
?>


2. Main.html

<!DOCTYPE html>
<html >

<head>
<title>Comments on MTIT</title>
</head>

<body>

<center>
<h4>
<form name="myForm" method="POST" action="Mdisplay.php">

<table>
<caption><h3>Comments on MTIT</h3></caption>

<tr>
<td>Registration No:</td>
<td><input type="text" name="regNo" /></td>
</tr>

<tr>
<td> Name : </td>
<td><input type="text" name="fullname" /></td>
</tr>

<tr>
<td> Year : </td>
<td><input type="radio" value="Year 3" name="year" /> Year 3
<input type="radio" value="Year 4" name="year" /> Year 4 </td>
</tr>

<tr>
<td> Faculty : </td>
<td>
<select name="faculty" />
<option> Faculty of Computing </option>
<option> Faculty of Engineering </option>
<option> Faculty of Business Management </option>
</select>
</td>
</tr>

<tr>
<td> Subjects</td>
<td>
<input type="checkbox" name="it" value="IT"> IT<br>
  <input type="checkbox" name="se" value="SE" checked> SE<br>
</td>
</tr>

<tr>
<td>Comments on MTIT : </td>
<td><textarea name="comment" rows="5" cols="40"></textarea></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="check" name="check" />
<input type="submit" value="View" name="View" />
<input type="reset" value="Reset" /></td>
</tr>

</table>
</form>
</h4>

</center>
</body>
</html>


3. Mdisplay.php


<?php
require 'db_connect.php';
?>
<?php

//to check more than one isset we need to use this
if(isset($_POST["fullname"]) && ($_POST["regNo"]) && ($_POST["comment"]) && ($_POST["year"]))
{

$name = $_POST["fullname"];
$regNumber = $_POST["regNo"];
$comment = $_POST["comment"];
$year=$_POST["year"]; //get the selected radio button option
$faculty=$_POST["faculty"]; //get the seleted combo box item

}
else
{
$error = "One or more fields are not filled";
echo $error;
return;
}

if(isset($_POST["check"]))
{

if(isset($_POST["it"]))
{
echo 'IT='.$_POST["it"];
}
if(isset($_POST["se"]))
{
echo 'SE='.$_POST["se"];
}

echo 'year'.$year;
echo '<br/>';
echo 'faculty'.$faculty;

    return;
}



if(isset($_POST["insert"]))
{

echo "inser pressed";

$insertString = "INSERT INTO ITA_Comments(name,comment,regno)
VALUES('$regNumber','$name','$comment')";

if(!mysql_query($insertString)) {
die('Error : '.mysql_error());
}

else {
echo '<br/>';
echo '1 record added...';
}

}

else if (isset($_POST["update"]))
{
echo "update pressed";
$updateString = "UPDATE ITA_Comments SET name='$name',
comment='$comment' WHERE regno='$regNumber'";

if(!mysql_query($updateString)) {

die('Error : '.mysql_error());
}
else {

echo '<br/>';
echo mysql_affected_rows().' record updated...';
}
}

else if (isset($_POST["delete"]))
{
echo "delete pressed";
$updateString = "DELETE FROM ITA_Comments WHERE regno='$regNumber'";

if(!mysql_query($updateString)) {
die('Error : '.mysql_error());
}

else {
echo '<br/>';
echo mysql_affected_rows().'1 record deleted...';
}
}

echo '<br/>';
echo '<br/>';

$selectString = "SELECT * FROM ITA_Comments";
$comments = mysql_query($selectString);

while($row = mysql_fetch_array($comments)) { ?>
<tr>
<td> <?php echo $row['regno']; ?> </td>
<td> <?php echo $row['name']; ?> </td>
<td> <?php echo $row['comment']; ?> </td>
</tr>

<?php
echo '<br/>';
}
?>

4. SQl QUERY

CREATE TABLE ITA_Comments(
id integer PRIMARY KEY auto_increment,
name varchar( 100 ) ,
comment varchar( 1000 ) ,
regno varchar( 20 )

)

No comments:

Post a Comment