Monday, 26 September 2016

php update and delete example using check box

02:08

(1)write code for   conn.inc.php file:
<?php
$con = mysql_connect('localhost','root','');
$db = mysql_select_db('admin_db');
if(!$con && !$db)
{
echo 'failed to connect..'.mysql_error();
}


?>

(2)write code   for  login_admin.php  file:

<?php
session_start();
require 'conn.inc.php';
if(isset($_POST['submit'])){
$admin_name=mysql_real_escape_string($_POST['admin']);
$password=mysql_real_escape_string($_POST['password']);
$admin_name=stripslashes($admin_name);
$password=stripslashes($password);
$password=md5($password);

$sql="select admin_name,password from admin_login where admin_name='".$admin_name."' and password='".$password."'";
$query=mysql_query($sql);
$row=mysql_num_rows($query);
if($row==1){
$_SESSION['admin']=$admin_name;
header('Location: welcome_admin.php');
}
else
{
 echo '<script type="text/javascript">alert("wrong username or password"); </script>';
}
}
?>

<form action="" method="POST">
Admin:<input type="text" name="admin" required><br>
password:<input type="password" name="password" required><br>
<input type="submit" name="submit" value="login">
</form>


(3)write  code for  welcome_admin.php  file:

<?php
session_start();
ob_start();
require 'conn.inc.php';

if(isset($_SESSION['admin'])){
$admin=$_SESSION['admin'];
echo '<b>welcome admin <i>'.$admin.'</i></b>';
echo "<div style='float:right'>
<label name=new style='margin-right:3px;'><a href=open_acc.php>Create new student account</a></label>
<label name=logout><a href=logout.php>logout</a></label>
</div>";
}
else{
header('Location:index.php');
}
?>
<div id="result" style="height:auto;width:auto;background-color: bisque;">
<form action="" method="POST">
<?php
$flag=true;
echo '<hr>';
$sql="select * from stud_detail";
$query=mysql_query($sql)or die(mysql_error());

echo '<table><tr>
      <th></th>
      <th>&nbsp&nbspName</th>
      <th>&nbsp&nbspuser name</th>
      <th>&nbsp&nbspPassword</th>
      <th>&nbsp&nbspContact number</th>
      <th>&nbsp&nbspAddress</th>
      <th>&nbsp&nbspGender</th>
                 <th>&nbsp&nbspEmail</th>
      <th>&nbsp&nbspBranch</th>
      <th>&nbsp&nbspBirth date</th>
      <th>&nbsp&nbspDate of admission</th>
      <th>&nbsp&nbspUpdate</th>
                 </tr>
                 <tr>';

while($row=mysql_fetch_array($query))
{
echo
                '<td><input type="checkbox" name="data[]" value="'.$row['user_name'].'">
                 <td>&nbsp&nbsp'.$row['name'].'</td>
                 <td>&nbsp&nbsp'.$row['user_name'].'</td>
                 <td>&nbsp&nbsp'.$row['password'].'</td>
                 <td>&nbsp&nbsp'.$row['contact_no'].'</td>
                 <td>&nbsp&nbsp'.$row['address'].'</td>
                 <td>&nbsp&nbsp'.$row['gender'].'</td>
                 <td>&nbsp&nbsp'.$row['email'].'</td>
                 <td>&nbsp&nbsp'.$row['branch'].'</td>
                 <td>&nbsp&nbsp'.$row['birth_date'].'</td>
                 <td>&nbsp&nbsp'.$row['admsn_date'].'</td>
                 <td><input type="submit" name="update" value="update"></td>
                 </tr>';
}

echo '</table>';
echo '<hr>';

               if(isset($_POST['delete']))
               {
               if(isset($_POST['data']))
                   {
                              foreach($_POST['data'] as $d){
                              $sql="delete from stud_detail where user_name='".$d."'";
                              $query=mysql_query($sql);
                   $sqli="delete from stud_login where user_name='".$d."'";
                              $queryi=mysql_query($sqli);
                              if($query && $queryi){
                              header('Location: welcome_admin.php');
                              }
                              else {
                              echo '<script type="text/javascript">alert("error in query processing..");</script>';
                              }
                              }
                }else {
                              echo '<script type="text/javascript">alert("select any one record..");</script>';
                              }
               }
               if(isset($_POST['update']))
               {
               if(isset($_POST['data']))
                   {
                             
                              $data=implode(' ',$_POST['data']);
                              $data=base64_encode($data);
                   header("Location: operations.php?q=".$data."");
                              }
                              else {
                              echo '<script type="text/javascript">alert("select any one record..");</script>';
                              }
               }

    if(isset($_POST['insert'])){
               header('Location: insert.php');
              
               }

              
?>

<input type="submit" name="delete" value="delete">
<input type="submit" name="insert" value="Add Record">
</form>
</div>
<div style="width: auto;height: 500px;background-color: bisque;">second form will go here</div>

(4)write code for  operations.php file:
<?php
session_start();
ob_start();
require 'conn.inc.php';
if(isset($_SESSION['admin'])){
   
                   $data=$_GET['q'];
        $data=base64_decode($data);
                              if(isset($_POST['update'])){
                 
                   $process='y';
                   $name=$_POST['name'];
                   $user_name=$_POST['user_name'];
                   $password=$_POST['password'];
                   $address=$_POST['address'];
                   $gender=$_POST['gender'];
                   $email=$_POST['email'];
                             
                   $contact_no=$_POST['contact_no'];
                             
                              if(is_numeric($contact_no)){
        $len=strlen($contact_no);
        if($len!=10){
        $alert.='contact no must be 10 digit';
        $process='n';
                   }
        }
        else
       {    
       $alert.='enter valid contact no..';
       $process='n';
       }
              
                             
                   $dob=$_POST['dob'];
                   $add_date=$_POST['add_date'];
                   $branch=$_POST['branch'];
                             
                              if($process=='y'){
                             
                              $sql="update stud_detail set name='".$name."',user_name='".$user_name."',password='".$password."',contact_no='".$contact_no."',address='".$address."',gender='".$gender."',email='".$email."',branch='".$branch."',birth_date='".$dob."',admsn_date='".$add_date."' where user_name='".$data."'";
                              $query=mysql_query($sql);
                              $sqli="update stud_login set user_name='".$user_name."',password='".$password."' where user_name='".$data."'";
                              $queryi=mysql_query($sqli);
                                if($query && $queryi)
                                {
                                  header('Location:welcome_admin.php');
                                }else {
                                echo 'something is wrong'.mysql_error();
                                }
                              }else {
                              echo $alert;
                              echo '<a href=welcome_admin.php>BACK TO UPDATE</a>';
                              exit();
                              }
                              }
}else {
header('Location: index.php');
}                           
?>
<head>
<script>


function name_valid(str){

var name_pattern =/^[A-Za-z]+$/;
//var name=document.getElementById("name").value()
if(str.match(name_pattern))
{
document.getElementById("ok").disabled=false;
document.getElementById("lb").innerHTML="";
}else{
alert("invalid name..");
document.getElementById("ok").disabled=true;
document.getElementById("lb").innerHTML="enter only characters";
}
}
</script>
</head>
<table>
<form action="" method="POST">
<?php
     $sql="select * from stud_detail where user_name='".$data."'";
                $query=mysql_query($sql);
                while($row=mysql_fetch_array($query)){
?>
<tr><th>
Student name:</th><td><input id="name" type="text" name="name" value="<?php echo $row['name'];?>" required onkeyup="name_valid(this.value)"></td></tr>
<tr><th></th><td><div id="lb"></div></td></tr>
<tr><th>
User name:</th><td><input type="text" name="user_name" value="<?php echo $row['user_name'];?>" required readonly></td></tr>
<tr><th></th><td>
<div id="lb2"></div></td></tr>
<tr><th>
password:</th><td><input type="text" name="password" value="<?php echo $row['password'];?>" required></td></tr>
<tr><th>
address:</th><td><input type="text" name="address" value="<?php echo $row['address'];?>" required></td></tr>
<tr><th>
gender:</th><td>
<input type="radio" name="gender" value="male" <?php if($row['gender']=='male') { echo 'checked'; }?> required>Male
<input type="radio" name="gender" value="female" <?php if($row['gender']=='female') { echo 'checked'; }?>>female</td></tr>
<tr><th>
Email:</th><td><input type="email" name="email" value="<?php echo $row['email'];?>" required></td></tr>
<tr><th>
contact number:</th><td><input type="text" name="contact_no" value="<?php echo $row['contact_no'];?>" required></td></tr>
<tr><th>
date of birth:</th><td><input type="date" name="dob" value="<?php echo $row['birth_date'];?>" required></td></tr>
<tr><th>
date of admission:</th><td><input type="date" name="add_date" value="<?php echo $row['admsn_date'];?>" required></td></tr>
<tr><th>
select branch:</th><td>
<select name="branch">
<option name="mech" value="mech" <?php if($row['branch']=='mech'){ echo 'selected'; }?>>Mech</option>
<option name="cse" value="cse" <?php if($row['branch']=='cse'){ echo 'selected'; }?>>CSE</option>
<option name="civil" value="civil" <?php if($row['branch']=='civil'){ echo 'selected';}?>>Civil</option>
<option name="e&tc" value="e&tc" <?php if($row['branch']=='e&tc'){ echo 'selected'; }?>>E&TC</option>
</select></td></tr>

<?php } ?>
<tr><th></th><td><input id="ok" type="submit" name="update" value="update"></td></tr>
</form>
</table>



(5)write code for  open_acc.php file:

<?php
session_start();
require 'conn.inc.php';
if(isset($_SESSION['admin'])){

    if(isset($_POST['submit'])){
                
                 $username=mysql_real_escape_string($_POST['username']);
                 $password=mysql_real_escape_string($_POST['password']);
                
                 $username=stripslashes($username);
                 $password=stripslashes($password);
              
               $sql="SELECT user_name from stud_login where user_name='".$username."'";
               $query=mysql_query($sql);
              
               $row=mysql_num_rows($query);
                  
            if($row>=1){
                echo '<script type="text/javascript"> alert("user name already exist");</script><br>';
              }
            else{
                                             $sqli="insert into stud_login values('','".$username."','".$password."')";
                                             $queryi=mysql_query($sqli);
                                             if($queryi){
                                                echo '<script type="text/javascript"> alert("new student account created");</script>';
                                             }
                                             else{
                                                echo mysql_error();
                                             }
                              }                                           
                  
               }


}else {
header('Location: index.php');
}
?>
<head>
<script>
function check_valid(str)
{
var variable;

if(window.XMLHttpRequest){
variable=new XMLHttpRequest();

}
else{
variable=new ActiveXObject("Microsoft.XMLHTTP");
}
variable.onreadystatechange=function(){
if(variable.readyState==4 && variable.status==200)
{
document.getElementById("lb").innerHTML=variable.responseText;
if(variable.responseText=='user name already taken'){
document.getElementById("ok").disabled=true;
}else if(variable.responseText=='available'){
document.getElementById("ok").disabled=false;
}
}
}
variable.open("GET","user_name.php?q="+str,true);
variable.send();
}

function check_conpass(str)
{

var pass=document.getElementById("pass").value;
var con_pass=document.getElementById("con_pass").value;
if(pass!=con_pass){
document.getElementById("lb2").innerHTML="password not match";
document.getElementById("ok").disabled=true;
}else{
document.getElementById("lb2").innerHTML="";
document.getElementById("ok").disabled=false;
}
}

</script>
</head>
<div style="width: 300px;margin: 200px 0px 0px 450px;background-color:beige">
<form action="#" method="POST" name="form">
student user name:<input type="text" name="username" required onblur="check_valid(this.value)"><br>
<div id="lb"></div>
student password:<input id="pass" type="password" name="password" required><br>
confirm password:<input id="con_pass" type="password" name="confirm_password" required onblur="check_conpass(this.value)"><br>
<div id="lb2"></div>
<input id="ok" style="margin-left: 74px;" type="submit" name="submit" value="create account!!">
</form>
<a href="welcome_admin.php">BACK</a>
</div>

(6)write code for user_name.php file:
<?php
require 'conn.inc.php';
$user_name=$_GET['q'];
$sql="select user_name from stud_login where user_name='".$user_name."'";
$query=mysql_query($sql);
$row=mysql_num_rows($query);
if($user_name=='')
{
echo '';
}
else if($row>=1)
{
echo 'user name already taken';
}
else{
echo 'available';
}
?>

(7)write code for insert.php file:
<?php
session_start();
ob_start();
require 'conn.inc.php';
if(isset($_SESSION['admin'])){
echo '<div style="float:right;"><a href=index.php>BACK</a></div>';

  if(isset($_POST['submit'])){
     $username=mysql_real_escape_string($_POST['username']);
                 $password=mysql_real_escape_string($_POST['password']);
                
                 $username=stripslashes($username);
                 $password=stripslashes($password);
                
               $process='y';
    $name=$_POST['name'];
               $address=$_POST['address'];
               $gender=$_POST['gender'];
               $email=$_POST['email'];
               $branch=$_POST['branch'];
               $contact_no=$_POST['contact_no'];
              
               if(is_numeric($contact_no)){
   
    $len=strlen($contact_no);
    if($len!=10){
    $alert.='contact no must be 10 digit';
    $process='n';
                }
     }
     else
     {    
       $alert.='enter valid contact no..';
       $process='n';
     }
                
               $dob=$_POST['dob'];
               $add_date=$_POST['add_date'];
               if($process=='y'){
               $sql="insert into stud_detail values('','".$name."','".$username."','".$password."','".$contact_no."','".$address."','".$gender."','".$email."','".$branch."','".$dob."','".$add_date."')";
               $query=mysql_query($sql);
               $sqli="insert into stud_login values('','".$username."','".$password."')";
               $queryi=mysql_query($sqli);
               if($query && $queryi){
                 echo '<script type="text/javascript">alert("congo!!!");</script><br>';
                 unset($_SESSION['student']);
     }else{
               echo 'error in query processing..'.mysql_error();
               }
               }else{
               echo '<script type="text/javascript">alert("'.$alert.'");</script><br>';
               /*echo $alert;
               echo '<a href=insert.php>BACK</a>';
               exit();*/
               }
  }
}
else{
header('Location:index.php');
}
?>

<head>
<script>
function check_valid(str)
{
var variable;

if(window.XMLHttpRequest){
variable=new XMLHttpRequest();

}
else{
variable=new ActiveXObject("Microsoft.XMLHTTP");
}
variable.onreadystatechange=function(){
if(variable.readyState==4 && variable.status==200)
{
document.getElementById("lb").innerHTML=variable.responseText;
if(variable.responseText=='user name already taken'){
document.getElementById("submit").disabled=true;
}else if(variable.responseText=='available'){
document.getElementById("submit").disabled=false;
}
}
}
variable.open("GET","user_name.php?q="+str,true);
variable.send();
}

function check_conpass(str)
{

var pass=document.getElementById("pass").value;
var con_pass=document.getElementById("con_pass").value;
if(pass!=con_pass){
document.getElementById("lb2").innerHTML="password not match";
document.getElementById("submit").disabled=true;
}else{
document.getElementById("submit").disabled=false;
}
}

function name_valid(str){

var name_pattern =/^[A-Za-z]+$/;
//var name=document.getElementById("n").value()
if(str.match(name_pattern))
{
document.getElementById("submit").disabled=false;
}else{
alert("enter valid name..");
document.getElementById("submit").disabled=true;
}

}


</script>
</head>
<table>
<form action="" method="POST">
<tr><th>Student name:</th><td><input id="n" type="text" name="name" onblur="name_valid(this.value)" required></td></tr>
<tr><th>student user name:</th><td><input type="text" name="username" required onblur="check_valid(this.value)"></td></tr>
<tr><th></th><td><div id="lb"></div></td></tr>
<tr><th>student password:</th><td><input id="pass" type="password" name="password" required><br></td></tr>
<tr><th>confirm password:</th><td><input id="con_pass" type="password" name="confirm_password" required onblur="check_conpass(this.value)"></td></tr>
<tr><th></th><td><div id="lb2"></div></td></tr>
<tr><th>address:</th><td><input type="text" name="address" required></td></tr>
<tr><th>gender:</th><td>
<input type="radio" name="gender" value="male" required>Male
<input type="radio" name="gender" value="female">female</td></tr>

<tr><th>Email:</th><td><input type="email" name="email"></td></tr>
<tr><th>conatct number:</th><td><input type="text" name="contact_no" required></td></tr>
<tr><th>date of birth:</th><td><input type="date" name="dob" required></td></tr>
<tr><th>date of admission:</th><td><input type="date" name="add_date" required></td></tr>
<tr><th>select branch:</th><td>
<select name="branch">
<option name="select">--select branch--</option>
<option name="Mech">Mech</option>
<option name="CSE">CSE</option>
<option name="Civil">Civil</option>
<option name="E&TC">E&TC</option>
</select></td></tr>
<tr><th></th><td><input id="submit" type="submit" name="submit" value="Add Record"></td></tr>
</form>
</table>


(8)write code for logout.php file:
<?php
session_start();
if(isset($_SESSION['admin'])){
unset($_SESSION['admin']);
header('Location: index.php');

}else {
header('Location: index.php');
}

?>

Written by

We are Creative Blogger Theme Wavers which provides user friendly, effective and easy to use themes. Each support has free and providing HD support screen casting.

1 comments:

PHP Training Mumbai, PHP MySQL Training Mumbai, PHP Course Mumbai, PHP Institute Mumbai

 

© 2013 PHP TRAINING IN MUMBAI BY OM SIR . All rights resevered. Designed by OM SIR

Back To Top