• We just launched and are currently in beta. Join us as we build and grow the community.

Advertise Here

Advertise Here

Advertise Here

Add, Edit, Delete with data table using PDO in PHP/MySQL

Rrb

Infrastructure Optimizer
R Rep
0
0
0
Rep
0
R Vouches
0
0
0
Vouches
0
Posts
54
Likes
188
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 100 XP
This simple project is created using PDO or it's called PHP Data Objects and it's a database driven using MySQL as a database. This project is intended for beginners in using PDO. It has a basic code so everyone can easily to understand and learn.

Creating our Table

We are going to make our database.
  1. Open the PHPMyAdmin.
  2. Create a database and name it as "add_pdo".
  3. After creating a database name, then we are going to create our table. And name it as "student".
  4. Kindly copy the code below.

  1. CREATE

    TABLE

    IF

    NOT

    EXISTS

    `student`

    (
  2. `student_id`

    INT

    (

    100

    )

    NOT

    NULL

    AUTO_INCREMENT

    ,
  3. `fname`

    VARCHAR

    (

    100

    )

    NOT

    NULL

    ,
  4. `mname`

    VARCHAR

    (

    100

    )

    NOT

    NULL

    ,
  5. `lname`

    VARCHAR

    (

    100

    )

    NOT

    NULL

    ,
  6. `address`

    VARCHAR

    (

    100

    )

    NOT

    NULL

    ,
  7. `email`

    VARCHAR

    (

    100

    )

    NOT

    NULL

    ,
  8. PRIMARY

    KEY

    (

    `student_id`

    )
  9. )

    ENGINE=

    InnoDB DEFAULT

    CHARSET=

    latin1 AUTO_INCREMENT

    =

    4

    ;

Database Connection

  1. <?php

    $conn

    =

    new

    PDO(

    'mysql:host=localhost; dbname=add_pdo'

    ,

    'root'

    ,

    ''

    )

    ;

    ?>

Add, Update, and Delete in PDO Query

Modal Add Form
1_74.png

  1. <form

    method

    =

    "post"

    action

    =

    "add.php"

    enctype

    =

    "multipart/form-data"

    >
  2. <table

    class

    =

    "table1"

    >
  3. <tr

    >
  4. <td

    ><label

    style

    =

    "color:#3a87ad; font-size:18px;"

    >

    FirstName</

    label

    ></

    td

    >
  5. <td

    width

    =

    "30"

    ></

    td

    >
  6. <td

    ><input

    type

    =

    "text"

    name

    =

    "fname"

    placeholder=

    "FirstName"

    required /

    ></

    td

    >
  7. </

    tr

    >
  8. <tr

    >
  9. <td

    ><label

    style

    =

    "color:#3a87ad; font-size:18px;"

    >

    MiddleName</

    label

    ></

    td

    >
  10. <td

    width

    =

    "30"

    ></

    td

    >
  11. <td

    ><input

    type

    =

    "text"

    name

    =

    "mname"

    placeholder=

    "MiddleName"

    required /

    ></

    td

    >
  12. </

    tr

    >
  13. <tr

    >
  14. <td

    ><label

    style

    =

    "color:#3a87ad; font-size:18px;"

    >

    LastName</

    label

    ></

    td

    >
  15. <td

    width

    =

    "30"

    ></

    td

    >
  16. <td

    ><input

    type

    =

    "text"

    name

    =

    "lname"

    placeholder=

    "LastName"

    required /

    ></

    td

    >
  17. </

    tr

    >
  18. <tr

    >
  19. <td

    ><label

    style

    =

    "color:#3a87ad; font-size:18px;"

    >

    Address</

    label

    ></

    td

    >
  20. <td

    width

    =

    "30"

    ></

    td

    >
  21. <td

    ><input

    type

    =

    "text"

    name

    =

    "address"

    placeholder=

    "Address"

    required /

    ></

    td

    >
  22. </

    tr

    >
  23. <tr

    >
  24. <td

    ><label

    style

    =

    "color:#3a87ad; font-size:18px;"

    >

    Email</

    label

    ></

    td

    >
  25. <td

    width

    =

    "30"

    ></

    td

    >
  26. <td

    ><input

    type

    =

    "email"

    name

    =

    "email"

    placeholder=

    "Email"

    required /

    ></

    td

    >
  27. </

    tr

    >

  28. </

    table

    >


  29. </

    div

    >
  30. <div

    class

    =

    "modal-footer"

    >
  31. <button

    class

    =

    "btn"

    data-dismiss=

    "modal"

    aria-hidden=

    "true"

    >

    Close</

    button

    >
  32. <button

    type

    =

    "submit"

    name

    =

    "Submit"

    class

    =

    "btn btn-primary"

    >

    Add</

    button

    >
  33. </

    div

    >


  34. </

    form

    >

Add PDO Query
  1. <?php
  2. require_once

    (

    'db.php'

    )

    ;

  3. $fname

    =

    $_POST

    [

    'fname'

    ]

    ;
  4. $mname

    =

    $_POST

    [

    'mname'

    ]

    ;
  5. $lname

    =

    $_POST

    [

    'lname'

    ]

    ;
  6. $address

    =

    $_POST

    [

    'address'

    ]

    ;
  7. $email

    =

    $_POST

    [

    'email'

    ]

    ;

  8. $conn

    ->

    setAttribute

    (

    PDO::

    ATTR_ERRMODE

    ,

    PDO::

    ERRMODE_EXCEPTION

    )

    ;
  9. $sql

    =

    "INSERT INTO student (fname, mname, lname, address, email)
  10. VALUES ('$fname

    ', '$mname

    ', '$lname

    ', '$address

    ', '$email

    ')"

    ;

  11. $conn

    ->

    exec

    (

    $sql

    )

    ;
  12. echo

    "<script>alert('Account successfully added!'); window.location='index.php'</script>"

    ;
  13. ?>

Result After Adding Data
11_4.png

Edit Form
2_32.png

  1. <?php
  2. include(

    'db.php'

    )

    ;
  3. $result =

    $conn->

    prepare("SELECT * FROM student where student_id='$ID'");
  4. $result->execute();
  5. for($i=0; $row = $result->fetch(); $i++){
  6. $id=$row['student_id'];
  7. ?>
  8. <form

    class

    =

    "form-horizontal"

    method

    =

    "post"

    action

    =

    "edit_PDO.php<?php echo '?student_id='.$id; ?>

    " enctype="multipart/form-data" style="float: right;">
  9. <legend

    ><h4

    >

    Edit</

    h4

    ></

    legend

    >
  10. <h4

    >

    Personal Information</

    h4

    >
  11. <hr

    >
  12. <div

    class

    =

    "control-group"

    >
  13. <label

    class

    =

    "control-label"

    for

    =

    "inputPassword"

    >

    FirstName:</

    label

    >
  14. <div

    class

    =

    "controls"

    >
  15. <input

    type

    =

    "text"

    name

    =

    "fname"

    required value

    =

    <?php echo $row[

    'fname'

    ]

    ; ?>

    >
  16. </

    div

    >
  17. </

    div

    >
  18. <div

    class

    =

    "control-group"

    >
  19. <label

    class

    =

    "control-label"

    for

    =

    "inputPassword"

    >

    MiddleName:</

    label

    >
  20. <div

    class

    =

    "controls"

    >
  21. <input

    type

    =

    "text"

    name

    =

    "mname"

    required value

    =

    <?php echo $row[

    'mname'

    ]

    ; ?>

    >
  22. </

    div

    >
  23. </

    div

    >
  24. <div

    class

    =

    "control-group"

    >
  25. <label

    class

    =

    "control-label"

    for

    =

    "inputEmail"

    >

    LastName:</

    label

    >
  26. <div

    class

    =

    "controls"

    >
  27. <input

    type

    =

    "text"

    name

    =

    "lname"

    required value

    =

    <?php echo $row[

    'lname'

    ]

    ; ?>

    >
  28. </

    div

    >
  29. </

    div

    >
  30. <div

    class

    =

    "control-group"

    >
  31. <label

    class

    =

    "control-label"

    for

    =

    "inputPassword"

    >

    Address:</

    label

    >
  32. <div

    class

    =

    "controls"

    >
  33. <input

    type

    =

    "text"

    name

    =

    "address"

    required value

    =

    <?php echo $row[

    'address'

    ]

    ; ?>

    >
  34. </

    div

    >
  35. </

    div

    >
  36. <div

    class

    =

    "control-group"

    >
  37. <label

    class

    =

    "control-label"

    for

    =

    "inputPassword"

    >

    Email:</

    label

    >
  38. <div

    class

    =

    "controls"

    >
  39. <input

    type

    =

    "email"

    name

    =

    "email"

    required value

    =

    <?php echo $row[

    'email'

    ]

    ; ?>

    >
  40. </

    div

    >
  41. </

    div

    >

  42. <div

    class

    =

    "control-group"

    >
  43. <div

    class

    =

    "controls"

    >

  44. <button

    type

    =

    "submit"

    name

    =

    "update"

    class

    =

    "btn btn-success"

    style

    =

    "margin-right: 65px;"

    >

    Save</

    button

    >
  45. <a

    href

    =

    "index.php"

    class

    =

    "btn"

    >

    Back</

    a

    >
  46. </

    div

    >
  47. </

    div

    >
  48. </

    form

    >
  49. <?php }

    ?>

Edit PDO Query
  1. <?php
  2. include

    'db.php'

    ;

  3. $get_id

    =

    $_REQUEST

    [

    'student_id'

    ]

    ;

  4. $fname

    =

    $_POST

    [

    'fname'

    ]

    ;
  5. $mname

    =

    $_POST

    [

    'mname'

    ]

    ;
  6. $lname

    =

    $_POST

    [

    'lname'

    ]

    ;
  7. $address

    =

    $_POST

    [

    'address'

    ]

    ;
  8. $email

    =

    $_POST

    [

    'email'

    ]

    ;

  9. $sql

    =

    "UPDATE student SET fname ='$fname

    ', mname ='$mname

    ', lname ='$lname

    ',
  10. address ='$address

    ', email ='$email

    ' WHERE student_id = '$get_id

    ' "

    ;

  11. $conn

    ->

    exec

    (

    $sql

    )

    ;
  12. echo

    "<script>alert('Successfully Edit The Account!'); window.location='index.php'</script>"

    ;
  13. ?>

Result After Edditing Data
22.png

Modal Delete Form
3_5.png

  1. <div

    id

    =

    "delete<?php echo $id;?>

    " class="modal hide fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
  2. <div

    class

    =

    "modal-header"

    >
  3. <h3

    id

    =

    "myModalLabel"

    >

    Delete</

    h3

    >
  4. </

    div

    >
  5. <div

    class

    =

    "modal-body"

    >
  6. <p

    ><div

    style

    =

    "font-size:larger;"

    class

    =

    "alert alert-danger"

    >

    Are you Sure you want Delete <b

    style

    =

    "color:red;"

    ><?php echo $row[

    'fname'

    ]

    ." "

    .$row[

    'mname'

    ]

    ." "

    .$row[

    'lname'

    ]

    ; ?></

    b

    >

    Data?</

    p

    >
  7. </

    div

    >
  8. <hr

    >
  9. <div

    class

    =

    "modal-footer"

    >
  10. <button

    class

    =

    "btn btn-inverse"

    data-dismiss=

    "modal"

    aria-hidden=

    "true"

    >

    No</

    button

    >
  11. <a

    href

    =

    "delete.php<?php echo '?student_id='.$id; ?>

    " class="btn btn-danger">Yes</

    a

    >
  12. </

    div

    >
  13. </

    div

    >
  14. </

    div

    >

Delete PDO Query
  1. <?php
  2. require_once

    (

    'db.php'

    )

    ;

  3. $get_id

    =

    $_GET

    [

    'student_id'

    ]

    ;

  4. // sql to delete a record
  5. $sql

    =

    "Delete from student where student_id = '$get_id

    '"

    ;

  6. // use exec() because no results are returned
  7. $conn

    ->

    exec

    (

    $sql

    )

    ;
  8. header

    (

    'location:index.php'

    )

    ;
  9. ?>

Result After Deleting Data
33.png


And, that's all, you can have Add, Edit, Delete with data table using PDO in PHP/MySQL or kindly click the "Download Code" button to download the full source code.

This is the full source code for Inserting Data, Updating Data, and Deleting Data in MySQL.

Share us your thoughts and comments below. Thank you so much for dropping by and reading this tutorial post. For more updates, don’t hesitate and feel free to visit this website more often and please share this with your friends or email me at [email protected]. Practice Coding. Thank you very much.


Download
You must upgrade your account or reply in the thread to view the hidden content.
 

452,496

342,574

342,582

Top