PHP Create, Read, Update and Delete (CRUD) Application Tutorial


Index.php :

<body>
 <center>
 <form name="myform" action="" method="get">
 Name : <input type="text" name="name" /><br />
Mobile : <input type="text" name="mbi" /><br />
 Qulification (Mulitiple Selection) <select name="quli[]" multiple="multiple"> <br />
 <option>select</option>
 <option>Mca</option>
 <option>Bsc</option>
 <option>Bcom</option>
 <option>M</option>
 </select><br />
 Gender <input type="radio" name="gr"  value="male"/> Male <input type="radio" name="gr"  value="female"/> Female<br />
 <input type="submit" name="submit" value="Insert" />
 </form>
 </center>
 </body>
 </html>
<?php if(isset($_REQUEST['submit'])) {?>
 <?php include_once('values_store.php');?>
 <?php  }?>

Demo:

demo1

Insert:

values_store.php

<?php
 $con = mysql_connect("localhost","root","admin123");
 if(!$con)
 {
 die("Error");
 }
 mysql_select_db("test");
 $name = $_REQUEST['name'];
 $mob = $_REQUEST['mbi'];
 $quli = $_REQUEST['quli'];
 $gende =$_REQUEST['gr'];
//echo count($quli);
 if(is_array($_REQUEST['quli']))
{
 $a = serialize($_REQUEST['quli']);
$sql  = "insert into addressbook values('','$name','$mob','$a','$gende')";
 }
$result = mysql_query($sql);
 if($result)
 {
 //echo "ok ";
}else {die("Error".mysql_error());}
include_once('resultsvalues.php');

Result:

resultsvalues.php’

<?php 

$con = mysql_connect("localhost","root","admin123");
if(!$con)

{
    die("error");
    
}

mysql_select_db("test");
echo "<center>";
echo "<table border='1'>";

$sql = "select * from addressbook";
$results = mysql_query($sql);
while($row = mysql_fetch_array($results))
{
    echo "<tr><td>";$id = $row['id']; echo $id; echo "</td>";
    echo "<td>";echo $row['name'];echo "</td>";
        echo "<td>";echo $row['mobilenum'];echo "</td>";
            echo "<td>";$qulif = $row['qulifications'];
            
              $myquls = unserialize($qulif);
              for($i=0; $i < count($myquls); $i++){
              echo $myquls[$i].',';
             
              }
            
            
            
            echo "</td>";
            
            
                echo "<td>";echo $row['gender'];echo "</td>";
                echo "<td>"; echo "<a href='Update_front.php?id=$id'>";echo "Edit";echo "</td>";
                echo "<td>"; echo "<a href='Delete.php?did=$id'>";echo "Delete";echo "</td></tr>";
    
}

echo "</table>";
echo "</center>";

Demo :

demo2

Update(Edit) :

Update_front.php

<?php 

$con = mysql_connect("localhost","root","admin123");
if(!$con)

{
    die("error");
    
}

mysql_select_db("test");
$id = $_REQUEST['id'];

$sql = "select * from addressbook where id='$id'";
$res = mysql_query($sql);
echo "<form name='upd' action='update.php' method='get'>";
while($row = mysql_fetch_array($res))
{
    ?>
    <center>
Name : <input type="text" name="name" value="<?php echo  $row['name'];?>" /><br />

mobile : <input type="text" name="mbi" value="<?php  echo $row['mobilenum'];?>"/><br />
Qulification  <select name="quli[]" multiple="multiple"> <br />
<option>select</option>
 <?php 
 $qulif = $row['qulifications'];
 $myquls = unserialize($qulif);
              for($i=0; $i < count($myquls); $i++){
             echo $myquls[$i].',';
             
              
            ?>
<?php if($myquls[$i] == "Mca") {?><option selected>Mca</option> <?php }?>
<?php if($myquls[$i] == "Bsc") {?><option selected>Bsc</option> <?php }?>
<option>Bcom</option>
<option>M</option>

<?php }?>
</select><br />
Gender <?php if($row['gender']=='male'){?><input type="radio" name="gr"  value="male" checked/> Male <input type="radio" name="gr"  value="female"/> Female<br /> <?php } else {?><input type="radio" name="gr"  value="male" checked/> Male <input type="radio" name="gr"  value="female" checked/> Female<?php }?>
<input type="hidden" name="id" value="<?php echo $id;?>">
<input type="submit" name="submit" value="update" />
    
   </form> 
   </center>
<?php 
    
}

Update.php

<?php $con = mysql_connect("localhost","root","admin123");
if(!$con)

{
    die("error");
    
}

mysql_select_db("test");

$name = $_REQUEST['name'];
$mob = $_REQUEST['mbi'];
$quli = $_REQUEST['quli'];
$gende =$_REQUEST['gr'];
$ids = $_REQUEST['id'];
if(is_array($quli)){
    $b = serialize($quli);

$sql = "update  addressbook set name='$name', mobilenum = '$mob', qulifications ='$b', gender = '$gende' where id='$ids' ";

}

$upd = mysql_query($sql);
if($upd){
    
    include_once('resultsvalues.php');
} else { die("erro".mysql_error());}

Delete:

Delete.php

<?php $con = mysql_connect("localhost","root","admin123");
if(!$con)

{
    die("error");
    
}

mysql_select_db("test");
$did =  $_REQUEST['did'];
$sql = "delete from addressbook where id = '$did'";
$delte = mysql_query($sql);
if($delte)

{
    
    include_once('resultsvalues.php');
}else {echo "error in delete";}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s