PDO Format CRUD Oparetion in PHP

 conn.php

<?php

$servername = "localhost";

// $port = 3306;

$username = "root";

$password = "";


try {

    $conn = new PDO("mysql:host=$servername;dbname=crud_oparetion", $username, $password);

  // set the PDO error mode to exception

  $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

  echo "Connected successfully";

} catch(PDOException $e) {

  echo "Connection failed: " . $e->getMessage();

}

?>


index.php

<?php 


include "conn.php";


if ($_SERVER['REQUEST_METHOD'] == "POST") {

    $name = $_REQUEST['name'];

    $address = $_REQUEST['address'];

    $phone = $_REQUEST['phone'];

// exit;

$sql = $conn->prepare("INSERT INTO tab (name, address, phone) VALUES (? ,? ,?)");

$sql->bindParam(1, $name, PDO::PARAM_STR);

$sql->bindParam(2, $address, PDO::PARAM_STR);

$sql->bindParam(3, $phone, PDO::PARAM_STR);

$sql->execute();


}


?>


<!DOCTYPE html>

<html lang="en">

<head>

    <meta charset="UTF-8">

    <meta http-equiv="X-UA-Compatible" content="IE=edge">

    <meta name="viewport" content="width=device-width, initial-scale=1.0">

    <title>Document</title>

</head>

<body>

    <form method="POST">

        <table>

            <tr>

                <th>NAME</th>

                <td><input type="text" name="name" id="name" placeholder="Enter name"></td>

            </tr>

            <tr>

                <th>ADDRESS</th>

                <td><input type="text" name="address" id="address" placeholder="Enter address"></td>

            </tr>

            <tr>

                <th>PHONE</th>

                <td><input type="text" name="phone" id="phone" placeholder="Enter phone"></td>

            </tr>

            <tr>

                <td><input type="submit" name="btn" id="btn" value="submit"></td>

            </tr>

        </table>

    </form>


    <table border="1" cellspacing="0">

        <tr>

            <th>sl no</th>

            <th>Name</th>

            <th>Address</th>

            <th>phone</th>

            <th>Action</th>

        </tr>

        <tr>

            <?php 

     

            $query = $conn->prepare("SELECT * FROM tab");

            $query->execute();

            $fetch = $query->fetchAll();

            foreach($fetch as $fetchiee) 

            {  

            ?>

            <tr>

                <td><?php echo $fetchiee['id']?></td>

                <td><?php echo $fetchiee['name']?></td>

                <td><?php echo $fetchiee['address']?></td>

                <td><?php echo $fetchiee['phone']?></td>

                <td>

                    <a href="edit.php?edit_data=<?php echo $fetchiee['id']?>"><button type="submit">EDIT</button></a>

                    <a href="delete.php?delete_data=<?php echo $fetchiee['id']?>"><button type="submit">DELETE</button></a>

                </td>

            </tr>

        </tr>

        <?php } ?>

    </table>

</body>

</html>


edit.php

<?php 

include "conn.php";


if (isset($_GET['edit_data'])) {

    $edit_data = $_GET['edit_data'];

    $query = $conn->prepare("SELECT * FROM tab WHERE id = '$edit_data'");

    $query->execute();

    $fetch = $query->fetch();

}

if (isset($_POST['btn'])) {

    $hidden = $_POST['hidden'];

    $name = $_POST['name'];

    $address = $_POST['address'];

    $phone = $_POST['phone'];

    $query = $conn->prepare("UPDATE tab SET name='$name' address='$address',phone'$phone' WHERE id = '$hidden'");

    if ($query) {

        echo "updated";

        header("Location: index.php");

    }else{

        echo "not";

    }

}


?>



<!DOCTYPE html>

<html lang="en">

<head>

    <meta charset="UTF-8">

    <meta http-equiv="X-UA-Compatible" content="IE=edge">

    <meta name="viewport" content="width=device-width, initial-scale=1.0">

    <title>Document</title>

</head>

<body>

<form method="POST">

        <table>

            <tr>

                <td><input type="hidden" name="hidden" id="hidden" placeholder="Enter hidden" value="<?php echo $fetch['id']?>"></td>

            </tr>

            <tr>

                <th>NAME</th>

                <td><input type="text" name="name" id="name" placeholder="Enter name" value="<?php echo $fetch['name']?>"></td>

            </tr>

            <tr>

                <th>ADDRESS</th>

                <td><input type="text" name="address" id="address" placeholder="Enter address" value="<?php echo $fetch['address']?>"></td>

            </tr>

            <tr>

                <th>PHONE</th>

                <td><input type="text" name="phone" id="phone" placeholder="Enter phone" value="<?php echo $fetch['phone']?>"></td>

            </tr>

            <tr>

                <td><input type="submit" name="btn" id="btn" value="submit"></td>

            </tr>

        </table>

    </form>

</body>

</html>


delete.php

<?php 

include "conn.php";


if (isset($_GET['delete_data'])) {

    $delete_data = $_GET['delete_data'];

    $query = $conn->prepare("DELETE FROM tab WHERE id = '$delete_data'");

    $query->execute();

    // $fetch = $query->fetch();

    header("Location: index.php");

}


?>

Comments

Popular posts from this blog

C.R.U.D Oparetion in (PHP Code)

Search Dropdown - jQuery

Ajax Crud Oparetion (Only Insert Code "create") | PHP | Jquery | Ajax | CODE