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

PHP Insert Image In MySQL

cracker16

Couch Co-Op King
C Rep
0
0
0
Rep
0
C Vouches
0
0
0
Vouches
0
Posts
110
Likes
110
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 300 XP
PHP Insert Image In MySQL

In this article, we are going to discuss PHP Insert Image In MySQL. We are going to create image file upload using PDO in PHP. This will be easy if you read or follow our previous tutorial in PHP Inserting Data To MySQL.

Let’s start with:
Creating our Table

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

  1. CREATE

    TABLE

    `tbl_image`

    (
  2. `tbl_image_id`

    INT

    (

    11

    )

    NOT

    NULL

    ,
  3. `first_name`

    VARCHAR

    (

    100

    )

    NOT

    NULL

    ,
  4. `last_name`

    VARCHAR

    (

    100

    )

    NOT

    NULL

    ,
  5. `image_location`

    VARCHAR

    (

    100

    )

    NOT

    NULL
  6. )

    ENGINE=

    InnoDB DEFAULT

    CHARSET=

    latin1;

Creating the Form Field - Modal Form

  1. <!-- Modal -->
  2. <div

    id

    =

    "myModal"

    class

    =

    "modal hide fade"

    tabindex

    =

    "-1"

    role=

    "dialog"

    aria-labelledby=

    "myModalLabel"

    aria-hidden=

    "true"

    >
  3. <div

    class

    =

    "modal-header"

    >

  4. <h3

    id

    =

    "myModalLabel"

    >

    Add</

    h3

    >
  5. </

    div

    >
  6. <div

    class

    =

    "modal-body"

    >
  7. <form

    method

    =

    "post"

    action

    =

    "upload.php"

    enctype

    =

    "multipart/form-data"

    >
  8. <table

    class

    =

    "table1"

    >
  9. <tr

    >
  10. <td

    ><label

    style

    =

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

    >

    FirstName</

    label

    ></

    td

    >
  11. <td

    width

    =

    "30"

    ></

    td

    >
  12. <td

    ><input

    type

    =

    "text"

    name

    =

    "first_name"

    placeholder=

    "FirstName"

    required /

    ></

    td

    >
  13. </

    tr

    >
  14. <tr

    >
  15. <td

    ><label

    style

    =

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

    >

    LastName</

    label

    ></

    td

    >
  16. <td

    width

    =

    "30"

    ></

    td

    >
  17. <td

    ><input

    type

    =

    "text"

    name

    =

    "last_name"

    placeholder=

    "LastName"

    required /

    ></

    td

    >
  18. </

    tr

    >
  19. <tr

    >
  20. <td

    ><label

    style

    =

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

    >

    Select your Image</

    label

    ></

    td

    >
  21. <td

    width

    =

    "30"

    ></

    td

    >
  22. <td

    ><input

    type

    =

    "file"

    name

    =

    "image"

    ></

    td

    >
  23. </

    tr

    >
  24. </

    table

    >
  25. </

    div

    >
  26. <div

    class

    =

    "modal-footer"

    >
  27. <button

    class

    =

    "btn"

    data-dismiss=

    "modal"

    aria-hidden=

    "true"

    >

    Close</

    button

    >
  28. <button

    type

    =

    "submit"

    name

    =

    "Submit"

    class

    =

    "btn btn-primary"

    >

    Upload</

    button

    >
  29. </

    div

    >
  30. </

    form

    >
  31. </

    div

    >

Creating Database Connection

  1. <?php
  2. $conn

    =

    new

    PDO(

    'mysql:host=localhost; dbname=upload_image'

    ,

    'root'

    ,

    ''

    )

    ;
  3. ?>

Creating Insert Statement

  1. <?php

  2. require_once

    (

    'db.php'

    )

    ;

  3. if

    (

    isset

    (

    $_POST

    [

    'Submit'

    ]

    )

    )

    {

  4. move_uploaded_file

    (

    $_FILES

    [

    "image"

    ]

    [

    "tmp_name"

    ]

    ,

    "uploads/"

    .

    $_FILES

    [

    "image"

    ]

    [

    "name"

    ]

    )

    ;
  5. $location

    =

    $_FILES

    [

    "image"

    ]

    [

    "name"

    ]

    ;
  6. $fname

    =

    $_POST

    [

    'first_name'

    ]

    ;
  7. $lname

    =

    $_POST

    [

    'last_name'

    ]

    ;

  8. $conn

    ->

    setAttribute

    (

    PDO::

    ATTR_ERRMODE

    ,

    PDO::

    ERRMODE_EXCEPTION

    )

    ;
  9. $sql

    =

    "INSERT INTO tbl_image (first_name, last_name, image_location)
  10. VALUES ('$fname

    ', '$lname

    ', '$location

    ')"

    ;

  11. $conn

    ->

    exec

    (

    $sql

    )

    ;
  12. echo

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

    ;
  13. }
  14. ?>

Source Code for displaying data

  1. <table

    cellpadding

    =

    "0"

    cellspacing

    =

    "0"

    border

    =

    "0"

    class

    =

    "table table-striped table-bordered"

    id

    =

    "example"

    >
  2. <div

    class

    =

    "alert alert-info"

    >
  3. <button

    type

    =

    "button"

    class

    =

    "close"

    data-dismiss=

    "alert"

    >

    &times;

    </

    button

    >
  4. <strong

    ><i

    class

    =

    "icon-user icon-large"

    ></

    i

    >

    &nbsp;

    Data Table</

    strong

    >
  5. </

    div

    >
  6. <thead

    >
  7. <tr

    >
  8. <th

    style

    =

    "text-align:center;"

    >

    User Image</

    th

    >
  9. <th

    style

    =

    "text-align:center;"

    >

    FirstName</

    th

    >
  10. <th

    style

    =

    "text-align:center;"

    >

    LastName</

    th

    >
  11. </

    tr

    >
  12. </

    thead

    >
  13. <tbody

    >
  14. <?php
  15. require_once(

    'db.php'

    )

    ;
  16. $result =

    $conn->

    prepare("SELECT * FROM tbl_image ORDER BY tbl_image_id ASC");
  17. $result->execute();
  18. for($i=0; $row = $result->fetch(); $i++){
  19. $id=$row['tbl_image_id'];
  20. ?>
  21. <tr

    >
  22. <td

    style

    =

    "text-align:center; margin-top:10px; word-break:break-all; width:450px; line-height:100px;"

    >
  23. <?php if(

    $row[

    'image_location'

    ]

    !=

    ""

    )

    : ?>
  24. <img

    src

    =

    "uploads/<?php echo $row['image_location']; ?>

    " width="100px" height="100px" style="border:1px solid #333333;">
  25. <?php else: ?>
  26. <img

    src

    =

    "images/default.png"

    width

    =

    "100px"

    height

    =

    "100px"

    style

    =

    "border:1px solid #333333;"

    >
  27. <?php endif; ?>
  28. </

    td

    >
  29. <td

    style

    =

    "text-align:center; word-break:break-all; width:300px;"

    >

    <?php echo $row [

    'first_name'

    ]

    ; ?></

    td

    >
  30. <td

    style

    =

    "text-align:center; word-break:break-all; width:200px;"

    >

    <?php echo $row [

    'last_name'

    ]

    ; ?></

    td

    >
  31. </

    tr

    >
  32. <?php }

    ?>
  33. </

    tbody

    >
  34. </

    table

    >

Output:

Adding new user.
1_79.png

After adding a user.
2_37.png


And, that's all. Kindly click the "Download Code" button for a full source code. Enjoy coding. Thank you very much.

So what can you say about this work? Share your thoughts in the comment section below 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 hidden text.
 

452,292

323,348

323,357

Top