Sunday, 29 May 2016

Core PHP Insert Update Delete View With MySqli





Connection:

Conn.php

<?php

$conn = mysqli_connect("localhost","root","","stud");
// Check connection
if (!$conn) {
    die("Connection failed: " . mysqli_connect_error());
}

?>

Stud:

<?php
include 'conn.php';

if($_POST)
{

    extract($_POST);
    $abc= "INSERT into student (name,address,email,password) values ('$name','$address','$email','$password')";

    if($conn->query($abc) == TRUE)
    {
        echo "Record Inserted";
    } 
    else
    {
        echo "Record not Inserted";
    }
}
?>
<html>
<head>
<title>Stud Registration</title>
</head>
<body>
<form action="" method="POST">
<table border="1">
<tr>
<th>Name</th>
<td><input type="text" name="name"></td>
</tr>
<tr>
<th>Address</th>
<td><input type="text" name="address"></td>
</tr>
<tr>
<th>Email</th>
<td><input type="email" name="email"></td>
</tr>
<tr>
<th>password</th>
<td><input type="password" name="password"></td>
</tr>

<tr>
<td><input type="submit" name="insert" value="insert"></td>
<td><input type="submit" name="view" value="view">
</td>
</tr>
</table>
</body>
</html>


Insert:

<?php
include 'conn.php';

if($_POST)
{

    extract($_POST);
    $abc= "INSERT into student (name,address,email,password) values ('$name','$address','$email','$password')";

    if($conn->query($abc)==TRUE)
    {
        echo "Record Inserted";
    } 
    else
    {
        echo "Record not Inserted";
    }
}
$conn->close();




?>

View :

<?php
include 'conn.php';


$abc= "SELECT * from student";
 $result=$conn->query($abc);

?>

<!DOCTYPE html>
<html>
<head>
    <title></title>
</head>
<body>
<table border="2">
<tr>
<th>Name</th>
<th>Address</th>
<th>Email</th>
<th>Password</th>
<th>Action</th>
</tr>

<?php
      while($data = $result->fetch_array())
      {
    ?>
<tr>
<td><?php echo $data['name']?></td>
<td><?php echo $data['address']?></td>
<td><?php echo $data['email']?></td>
<td><?php echo $data['password']?></td>

                <td><a href="edit.php?stud_id=<?php echo $data['stud_id'];?>">Edit</a></td>
                <td><a href="delete.php?stud_id=<?php echo $data['stud_id'];?>">Delete</a></td>
</tr>
<?php
 }
 ?>


</table>
</body>
</html>

Edit:

<?php
include 'conn.php';
extract($_GET);
$abc="SELECT * from student where stud_id='$stud_id' ";

$result=$conn->query($abc);
$data=$result->fetch_array();

if($_POST){
        extract($_POST);

        $abc = "UPDATE student SET name='$name',address='$address',email='$email',password='$password' WHERE stud_id='$stud_id'";

        if ($conn->query($abc) === TRUE){
        header("location:view.php?msg=updated");
    }
    else{
        echo "not updated";
    }
}
$conn->close();

?>
<html>
<head>
<title>Stud Registration</title>
</head>
<body>
<form action="" method="POST">
<table border="1">
<tr>
<th>Stud ID</th>
<td><input type="text" name="stud_id" value="<?php echo $data['stud_id'] ?>"></td>
</tr>
<tr>
<th>Name</th>
<td><input type="text" name="name" value="<?php echo $data['name'] ?>"></td>
</tr>
<tr>
<th>Address</th>
<td><input type="text" name="address"value="<?php echo $data['address'] ?>"></td>
</tr>
<tr>
<th>Email</th>
<td><input type="email" name="email"value="<?php echo $data['email'] ?>"></td>
</tr>
<tr>
<th>password</th>
<td><input type="password" name="password" value="<?php echo $data['password'] ?>"></td>
</tr>

<tr>
<td><input type="submit" name="insert" value="insert"></td>
<td><input type="submit" name="view" value="view">
</td>
</tr>
</table>
</body>
</html>


Delete:

<?php
    include "conn.php" ;
    extract($_GET);
    $abc = "DELETE FROM student WHERE stud_id='$stud_id'";

    if($conn->query($abc) === TRUE){
        header("location:view.php?msg=Deleted");
    }
    else{
        header("location:view.php?msg=Error");   
    }

    $conn->close();
?>
 

Database Snap::




SHARE THIS

Author:

0 comments: