Pages - পৃষ্ঠাসমূহ

Insert, update and delete using php and mysql

In this post we will learn a sample projects using php and mysql. Using these we create a simple data management system of insert update and delete more data. A large data managenment system requires more security to enter data and an authenciation system so that anybody can not access without permission. Now we will simply shsow the insert, update and delete data using php and mysql. In the previous post we show how to create a websites authenciation Create a site authenciation or login, register, logout using php and mysql
Hide Example Show Example

Final output of this sample projects

The final output of Insert update delete using php and mysql. The sample screenshots of this project.

data management system using php and mysql

Insert update and delete files structure

There are several files needed to completed a webpage data entry. We createinsert, update and delete pages. Some files of this sample projects are listed below
insert-delete-update/
 css/
  style.css
 admin/
  dbconfig.php
 img/
  add.png
  del.png
  update.png
 data.php
 insert.php
 delete.php
 update.php
 
And finaly include a sql file. 

create a admin database configure file

Now we will create a file where server information are located. If anybody want to create their insert delete update projects they must re configure the file as their database configuration.

admin/dbconfig.php
<?php
 
 $server_name="127.0.0.1";   // server name
 $server_username="root";    // server username
 $server_password="";        // server password
 $server_database="taslim1"; // database name
 
 $connect=mysql_connect($server_name,$server_username,$server_password) or die ("Server error : Server information is wrong.");
 $data=mysql_select_db($server_database) or die ("Database error : Database not found in your server.");

?>

Create front or data page

Create front page of this samples where all information and links are located here to easily navigate. Here all data are shows and update insert and delete links are found here.

data.php
<?php include("admin/dbconfig.php"); ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html lang="en-US">
 <head>
  <title>A simple DATA Management system</title>
  <link rel="stylesheet" type="text/css" href="css/style.css">
   </head>
 <body>
  <div id="header">
   <h1>A simple DATA Management system</h1>
  </div>
  <div id="navbar">   
   <a href="data.php">Data</a> 
  </div>
  <div id="content">
   <table cellpadding="0" cellspacing="0">    
    <tr>     
     <td ><a href="insert.php" class="add"><img src='img/add.png'/> </a>
     <td><a href="insert.php" class="add"> Insert New Data</a>
    </tr>
   </table>
   <table class="data" cellpadding="0" cellspacing="0">
    <caption>User Data</caption>
    <tr>     
     <td width="30%">Email</td>
     <td width="15%">User</td>
     <td width="15%">Password</td>
     <td width="15%">State</td>
     <td width="15%">Country</td>
     
     <td width="5%" ><img src='img/update.png'/></td>
     <td width="5%" ><img src='img/del.png'/></td>
    </tr>
     <?php
      $select=mysql_query("select * from userinfo");
      while($row=mysql_fetch_array($select)){
       echo "<tr>        
        <td>".$row['email']."</td>
        <td>".$row['username']."</td>
        <td>".$row['password']."</td>
        <td>".$row['state']."</td>
        <td>".$row['country']."</td>
       
        <td><a href='update.php?id=".$row['id']."'><img src='img/update.png' alt='update' title='update'/></a></td>
        <td>";?>
        <a href="delete.php?id=<?php echo $row['id']; ?>"  onclick="return confirm('Are you sure you want to delete?')">
        <img src='img/del.png' alt='delete' title='delete'></a></td>
     <?php echo "
       </tr>";
      }
     ?> 
</table>     
    
    
  </div>
  <div id="footer">
   Copyright @ taslim. Anybody can use it for personal and commercial.
  </div>
  
 </body>
</html>

Create data add Page

Crete a insert file where the a user can add data. Here necessary inforamtion box are added. Because to add data many information are required. If anybody want to know more inforamtion they can add input box as their own way..

insert.php
<?php include("admin/dbconfig.php"); ?>

   <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html lang="en-US">
 <head>
  <title>A simple DATA Management system</title>
  <link rel="stylesheet" type="text/css" href="css/style.css">
  
 </head>
 <body>
  <div id="header">
   <h1>A simple DATA Management system</h1>
  </div>
  <div id="navbar">
   <a href="index.php">Home</a>   
   <a href="data.php">Data</a> 
  </div>
  <div id="content">
   <form action="" method="post">
   <table class="register" cellpadding="0" cellspacing="0">
    <caption>Register</caption>
    <tr>
     <td>Email*</td>
     <td>
     <?php $a=1; ?>
      <input type="text" name="email" value="<?php if(isset($_POST['email'])) echo $_POST['email']; ?>"/>
      <?php
       if(isset($_POST['register'])){
           $email_pattern = "/^[a-zA-Z0-9._-]+@[a-zA-Z0-9-]+\.[a-zA-Z.]{2,5}$/";
        $email = $_POST['email'];         
        if (preg_match($email_pattern,$email)) {}        
        else{
         echo "<br/><span class='error'>Email must be valid.</span>";
         $a=0;         
        }
       }
      ?>
     </td>
    </tr>
    <tr>
     <td>Username*</td>
     <td><input type="text" name="username" maxlength="12" value="<?php if(isset($_POST['username'])) echo $_POST['username']; ?>"/>
      <?php
       if(isset($_POST['register'])){
           $username_pattern = "/^[a-zA-Z]{4,12}$/";
        $username = $_POST['username'];         
        if (preg_match($username_pattern,$username)) {}        
        else{
         echo "<br/><span class='error'>Username only contains letters. others are prohibited.</span>";
         $a=0;         
        }
       }
      ?>
     </td>
    </tr>
    <tr>
     <td>Password*</td>
     <td><input type="password" name="password" maxlength="12" value=""/>
      <?php
       if(isset($_POST['register'])){
           $password_pattern = "/^[a-zA-Z0-9._-]{4,12}$/";
        $password = $_POST['password'];         
        if (preg_match($password_pattern,$password)) {}        
        else{
         echo "<br/><span class='error'>Password must be contain combination of 4 to 12 letters,digits,dot and hiphen.</span>";
         $a=0;         
        }
       }
      ?>
     </td>
    </tr>  
    <tr>
     <td>Password</td>
     <td><input type="password" name="password1" maxlength="12" value=""/>
      <?php
       if((isset($_POST['password1']))!=(isset($_POST['password1']))){
        echo "<br/><span class='error'>Password did not match.</span>";
        $a=0;         
       }
      ?>
     </td>
    </tr>     
    <tr>
     <td>Privience/city</td>
     <td><input type="text" name="state" maxlength="12" value="<?php if(isset($_POST['state'])) echo $_POST['state']; ?>"/></td>
    </tr>
    <tr>
     <td>Country</td>
     <td>
      <select name="country">
       <option selected="selected" value="<?php if(isset($_POST['country'])) echo $_POST['country']; ?>">
        <?php if(isset($_POST['country'])) echo $_POST['country']; else echo "Choose"; ?>
       </option>
       <option value="bangladesh">Bangladesh</option>
       <option value="pakistan">Pakistan</option>
       <option value="uk">UK</option>
       <option value="espan">Espan</option> 
       <option value="france">France</option>
       <option value="usa">USA</option>
       <option value="oters">Others</option>

      </select>
     </td>
    </tr>
    <tr>
     <td><input type="checkbox" name="agree"/>*</td>
     <td>Are you agree with our terms and policies.
      <?php
       if(isset($_POST['register'])){
        if((isset($_POST['agree']))!="selected"){
         echo "<br/><span class='error'>You must agree with our terms and policies.</span>";
         $a=0;         
        }
       }
      ?>
     </td>
    </tr>
    <tr>
     <td></td>
     <td><input type="submit" name="register" value="Register"/></td>
    </tr>
   </table>
   </form>
  </div>
  
  
<?php
 if(isset($_POST['register'])){
  $user=$_POST['username'];  
  $pass=$_POST['password'];
  $email=$_POST['email'];
  $state=$_POST['country'];
  $country=$_POST['state'];
  if($a!=0){
   $ins=mysql_query("insert into userinfo(state,country,email,username,password) values('$state','$country','$email','$user','$pass')");
   if($ins){
    echo "Your data inserted.";
   }
   else{
    echo "Error occured. try again.";
   }
  }
  else{
   echo "You must need to insert correct information.";
  }
  
  
 }

?>
  <div id="footer">
   Copyright @ taslim. Anybody can use it for personal and commercial.
  </div>
  
 </body>
</html>





Create the update Page

After creating insert page we need an update page. Where anybody can update their data as they want.

update.php
<?php include("admin/dbconfig.php"); ?>
<?php
 $id=$_GET['id'];
 
?>
   <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html lang="en-US">
 <head>
  <title>A simple DATA Management system</title>
  <link rel="stylesheet" type="text/css" href="css/style.css">
  
 </head>
 <body>
  <div id="header">
   <h1>A simple DATA Management system</h1>
  </div>
  <div id="navbar">
   <a href="index.php">Home</a>   
   <a href="data.php">Data</a> 
  </div>
  <div id="content">
  
   <form action="" method="post">
   <table class="register" cellpadding="0" cellspacing="0">
    <caption>Register</caption>
    <?php
     $select=mysql_query("select * from userinfo where id='$id' "); 
      while($row=mysql_fetch_array($select)){
      
      
    ?>
    <tr>
     <td>Email*</td>
     <td>
     <?php $a=1; ?>
      <input type="text" name="email" value="<?php if(isset($_POST['email'])) echo $_POST['email']; else echo  $row['email']; ?>"/>
      <?php
       if(isset($_POST['update'])){
           $email_pattern = "/^[a-zA-Z0-9._-]+@[a-zA-Z0-9-]+\.[a-zA-Z.]{2,5}$/";
        $email = $_POST['email'];         
        if (preg_match($email_pattern,$email)) {}        
        else{
         echo "<br/><span class='error'>Email must be valid.</span>";
         $a=0;         
        }
       }
      ?>
     </td>
    </tr>
    <tr>
     <td>Username*</td>
     <td><input type="text" name="username" maxlength="12" value="<?php if(isset($_POST['username'])) echo $_POST['username']; else echo $row['username'];?>"/>
      <?php
       if(isset($_POST['update'])){
           $username_pattern = "/^[a-zA-Z]{4,12}$/";
        $username = $_POST['username'];         
        if (preg_match($username_pattern,$username)) {}        
        else{
         echo "<br/><span class='error'>Username only contains letters. others are prohibited.</span>";
         $a=0;         
        }
       }
      ?>
     </td>
    </tr>
    <tr>
     <td>Password*</td>
     <td><input type="password" name="password" maxlength="12" value="<?php if(isset($_POST['password'])) echo $_POST['password']; else echo $row['password'];?>"/>
      <?php
       if(isset($_POST['update'])){
           $password_pattern = "/^[a-zA-Z0-9._-]{4,12}$/";
        $password = $_POST['password'];         
        if (preg_match($password_pattern,$password)) {}        
        else{
         echo "<br/><span class='error'>Password must be contain combination of 4 to 12 letters,digits,dot and hiphen.</span>";
         $a=0;         
        }
       }
      ?>
     </td>
    </tr>  
    <tr>
     <td>Password</td>
     <td><input type="password" name="password1" maxlength="12" value="<?php if(isset($_POST['password'])) echo $_POST['password']; else echo $row['password'];?>"/>
      <?php
       if((isset($_POST['password1']))!=(isset($_POST['password1']))){
        echo "<br/><span class='error'>Password did not match.</span>";
        $a=0;         
       }
      ?>
     </td>
    </tr>     
    <tr>
     <td>Privience/city</td>
     <td><input type="text" name="state" maxlength="12" value="<?php if(isset($_POST['state'])) echo $_POST['state']; else echo $row['state'];?>"/></td>
    </tr>
    <tr>
     <td>Country</td>
     <td>
      <select name="country">
       <option selected="selected" value="<?php if(isset($_POST['country'])) echo $_POST['country']; else echo   $row['country'];?>">
        <?php if(isset($_POST['country'])) echo $_POST['country']; else echo   $row['country']; ?>
       </option>
       <option value="bangladesh">Bangladesh</option>
       <option value="pakistan">Pakistan</option>
       <option value="uk">UK</option>
       <option value="espan">Espan</option> 
       <option value="france">France</option>
       <option value="usa">USA</option>
       <option value="oters">Others</option>

      </select>
     </td>
    </tr>
    <?php 
    }
    ?>
    <tr>
     <td></td>
     <td><input type="submit" name="update" value="Update"/></td>
    </tr>
   </table>
   </form>
  </div>
  
  
<?php
 if(isset($_POST['update'])){
  $user=$_POST['username'];  
  $pass=$_POST['password'];
  $email=$_POST['email'];
  $state=$_POST['state'];
  $country=$_POST['country'];
  if($a!=0){
   $upt=mysql_query("update userinfo set email='$email',password='$pass',username='$user',state='$state',country='$country' where id='$id'");
   if($upt){
    echo "Data is updated.";
   }
   else{
    echo "Error occured. try again.";
   }
  }
  else{
   echo "You must need to insert correct information.";
  }
  
  
 }

?>
  <div id="footer">
   Copyright @ taslim. Anybody can use it for personal and commercial.
  </div>
  
 </body>
</html>



delete php

When a user infomation is inserted then this page grab all the information from the database. If any user want to delete any data as they can by this page.

delete.php
<?php include("admin/dbconfig.php"); ?>

   <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html lang="en-US">
 <head>
  <title>A simple DATA Management system</title>
  <link rel="stylesheet" type="text/css" href="css/style.css">
  
 </head>
 <body>
  <div id="header">
   <h1>A simple DATA Management system</h1>
  </div>
  <div id="navbar">
   <a href="index.php">Home</a>   
   <a href="data.php">Data</a> 
  </div>
  <div id="content">  
   
    <?php
     $id=$_GET['id'];
 
  
     $del=mysql_query("delete   from userinfo where id='$id' "); 
      if($del){
       echo "Data is successfully deleted.";
      }
      else{
       echo "data is not deleted.";
      }  
      
    
    
    ?>
    
   
  </div>
  
 
  <div id="footer">
   Copyright @ taslim. Anybody can use it for personal and commercial.
  </div>
  
 </body>
</html>



Design this authenciation projects

For tableless desgin we need to design with css

css/style.css
body{
 width:790px;
 margin:0 auto;
}
#header{
 width:96%
 padding:2%;
}

#navbar{
 background:#f1f1f1;
 padding:2%;
 width:96%;
 color:darkgray;
 margin-top:60px;
}
#navbar a{
 padding:5px 10px;
 text-decoration:none;
 color:green;
}
#content{
 padding:2%;
 width:96%;
 margin:10px 0px;
 background:#f1f1f1;
 color:dimgray;
}

.box {
 width:15%;
 margin:.5%;
 padding:4.5%;
 float:left;    
 background:#f1f1f1;    
 text-decoration:none;       
 font-size:20px;
}

.box:hover{
 background:red;
}
.clear{
 clear:both;
}
#footer{
 background:#f6f6f6;
 padding:2%;
 width:96%;
 color:darkgray;
 margin-top:60px;
}

caption{
 background:green;
 color:white;
 padding:5px;
}
.data td{
 padding:5px;
}
.data tr:nth-child(2n+1){
 background:#f6f6f6;
 
}

.register{
 width:75%;
 margin:0 auto;
}
.register td{
 padding:5px;
 font-size:15px;
 color:dimgray;
}
input[type="text"],input[type="password"]{
 padding:10px;
 border:1px solid darkgray;
 width:200px;
}

input[type="submit"]{
 padding:10px 30px;
 border:1px solid darkgray;
 background:#f1f1f1;
}

.error{
 color:red;
}
select{
 text-transform:capitalize;
}
.login{
 width:60%;
 margin:0 auto;
}
.login td{
 padding:5px;
 font-size:15px;
 color:dimgray;
}

.data{
 width:100%;
}
td img{
 width:22px;
}
td a{
 cursor:pointer;
}
.add{
 text-decoration:none;
 padding:10px;
 text-align:center;
}   

The database file

Here show a database qury which is used in this project.

sql file



--
-- Table structure for table `userinfo`
--

CREATE TABLE IF NOT EXISTS `userinfo` (
  `id` int(100) NOT NULL AUTO_INCREMENT,
  `state` varchar(20) NOT NULL,
  `country` varchar(10) NOT NULL,
  `email` varchar(20) NOT NULL,
  `username` varchar(12) NOT NULL,
  `password` varchar(12) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=3 ;




5 comments:

  1. that was great!!!So much helpfull for me...thanks a lot...:)

    ReplyDelete
  2. unbelievable ! thanks so much keep up the good work!!!!!!!!!

    ReplyDelete
  3. Thanks Great Work man i will use this for a school project thanks man

    ReplyDelete
  4. This comment has been removed by the author.

    ReplyDelete