PHP PDO : How To Insert Update Delete Search Data In MySQL Using PDO In PHP [ with source code ] 2



How To Insert Update Delete And Search Data From MySQL Database In PHP Using PDO

Part 3: https://www.youtube.com/watch?v=-Ev-M7qGIao

PHP Programming Tutorials ➜ http://bit.ly/2Lh7zPM

PHP And MySQL Tutorials ➜ http://bit.ly/2kdXWot

PHP Course For Beginners ➜ http://bit.ly/2GxRodb

Web Development Course ➜ http://bit.ly/2ITRQrj

Website Builder Tool ➜ http://bit.ly/2JgaZ64

Programming Projects:
http://1bestcsharp.blogspot.com/p/programming-projects.html

visit our blog…

Related Posts

5 Comments

  1. Hi I followed your topic and the below code just frustrates me as I already did my best, yet it only can Search data. The "Add" button doesn't accept any entry.

    Please help. thanks.

    <?php

    $dsn = 'mysql:host=****.****.com;dbname=*******';
    $username = '*****_***';
    $password = '*******';

    try{

    $con = new PDO($dsn,$username,$password);
    $con->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

    } catch (Exception $ex) {
    echo 'Not Connected '.$ex->getMessage();
    }

    $id = '';
    $fname = '';
    $mname = '';
    $lname = '';
    $age = '';
    $position = '';
    $passport = '';
    $mobile1 = '';
    $mobile2 = '';
    $linedup = '';
    $selected = '';
    $rejected = '';
    $reserved = '';
    $medical = '';
    $process = '';
    $deploy = '';
    $remark = '';

    function getPosts()
    {
    $posts = array();
    $posts[0] = $_POST['id'];
    $posts[1] = $_POST['fname'];
    $posts[2] = $_POST['mname'];
    $posts[3] = $_POST['lname'];
    $posts[4] = $_POST['age'];
    $posts[5] = $_POST['position'];
    $posts[6] = $_POST['passport'];
    $posts[7] = $_POST['mobile1'];
    $posts[8] = $_POST['mobile2'];
    $posts[9] = $_POST['linedup'];
    $posts[10] = $_POST['selected'];
    $posts[11] = $_POST['rejected'];
    $posts[12] = $_POST['reserved'];
    $posts[13] = $_POST['medical'];
    $posts[14] = $_POST['process'];
    $posts[15] = $_POST['deploy'];
    $posts[16] = $_POST['remark'];
    return $posts;
    }

    //Search and Display Data

    if(isset($_POST['search']))
    {
    $data = getPosts();
    if(empty($data[0]))
    {
    echo 'Enter the Id to Search';
    } else {

    $searchStmt = $con->prepare('SELECT * FROM workers WHERE id = :id');
    $searchStmt->execute(array(
    ':id'=> $data[0]
    ));
    if($searchStmt)
    {
    $user = $searchStmt->fetch();
    if(empty($data))
    {
    echo 'No Data for this Id';
    }

    $id = $user[0];
    $fname = $user[1];
    $mname = $user[2];
    $lname = $user[3];
    $age = $user[4];
    $position = $user[5];
    $passport = $user[6];
    $mobile1 = $user[7];
    $mobile2 = $user[8];
    $linedup = $user[9];
    $selected = $user[10];
    $rejected = $user[11];
    $reserved = $user[12];
    $medical = $user[13];
    $process = $user[14];
    $deploy = $user[15];
    $remark = $user[16];

    }
    }
    }

    // Insert
    if(isset($_POST['insert']))
    {
    $data = getPosts();
    if(empty($data[1]) || empty($data[2]) || empty($data[3]) || empty($data[4]) || empty($data[5]) || empty($data[6]) || empty($data[7]) || empty($data[8]) || empty($data[9]) || empty($data[10]) || empty($data[11]) || empty($data[12]) || empty($data[13]) || empty($data[14]) || empty($data[15]) || empty($data[16]) || empty($data[17]))
    {
    echo 'Unable to Register';
    } else {
    $insertStmt = $con->prepare('INSERT INTO workers(fname,mname,lname,age,position,passport,mobile1,mobile2,linedup,selected, rejected,reserved,medical,process,deploy,remark) VALUES(:fname,:mname,:lname,:age,:position,:passport,:mobile1,:mobile2,:linedup,:selected,:rejected,:reserved,:medical, :process,:deploy,:remark)');
    $insertStmt->execute(array(
    ':fname'=> $data[1],
    ':mname'=> $data[2],
    ':lname'=> $data[3],
    ':age'=> $data[4],
    ':position'=> $data[5],
    ':passport'=> $data[6],
    ':mobile1'=> $data[7],
    ':mobile2'=> $data[8],
    ':linedup'=> $data[9],
    ':selected'=> $data[10],
    ':rejected'=> $data[11],
    ':reserved'=> $data[12],
    ':medical'=> $data[13],
    ':process'=> $data[14],
    ':deploy'=> $data[15],
    ':remark'=> $data[16],

    ));
    if($insertStmt)
    {
    echo 'Data was registered';

    }

    }

    }

    ?>

    <!DOCTYPE Html>
    <html>
    <head>
    <title>INSERT UPDATE DELETE SEARCH</title>
    </head>
    <body>
    <form action="php_insert_update_delete_search.php" method="POST">
    <input type="text" name="id" placeholder="id" value="<?php echo $id;?>"><br><br>
    <input type="text" name="fname" placeholder="First Name" value="<?php echo $fname;?>"><br><br>
    <input type="text" name="mname" placeholder="Middle Name" value="<?php echo $mname;?>"><br><br>
    <input type="text" name="lname" placeholder="Last Name" value="<?php echo $lname;?>"><br><br>
    <input type="number" name="age" placeholder="Age" value="<?php echo $age;?>"><br><br>
    <input type="text" name="position" placeholder="Position" value="<?php echo $position;?>"><br><br>
    <input type="text" name="passport" placeholder="Passport" value="<?php echo $passport;?>"><br><br>
    <input type="number" name="mobile1" placeholder="Mobile1" value="<?php echo $mobile1;?>"><br><br>
    <input type="number" name="mobile2" placeholder="Mobile2" value="<?php echo $mobile2;?>"><br><br>
    <input type="text" name="linedup" placeholder="Linedup" value="<?php echo $linedup;?>"><br><br>
    <input type="text" name="selected" placeholder="Selected" value="<?php echo $selected;?>"><br><br>
    <input type="text" name="rejected" placeholder="Rejected" value="<?php echo $rejected;?>"><br><br>
    <input type="text" name="reserved" placeholder="Reserved" value="<?php echo $reserved;?>"><br><br>
    <input type="text" name="medical" placeholder="Medical" value="<?php echo $medical;?>"><br><br>
    <input type="text" name="process" placeholder="Process" value="<?php echo $process;?>"><br><br>
    <input type="text" name="deploy" placeholder="Deployment" value="<?php echo $deploy;?>"><br><br>
    <input type="text" name="remark" placeholder="Remark" value="<?php echo $remark;?>"><br><br>

    <!– Input For Add Values To Database–>
    <input type="submit" name="insert" value="Add">

    <!– Input For Edit Values –>
    <input type="submit" name="update" value="Update">

    <!– Input For Clear Values –>
    <input type="submit" name="delete" value="Delete">

    <!– Input For Find Values With The given ID –>
    <input type="submit" name="search" value="Find">

    </form>
    </body>
    </html>

Leave a Reply

Your email address will not be published.

© 2022 Code As Pro - Theme by WPEnjoy · Powered by WordPress