PHP Tutorials Uploading Storing an Image inside a MySQL Database Part 2 YouTube


source


41 responses to “PHP Tutorials Uploading Storing an Image inside a MySQL Database Part 2 YouTube”

  1. ok so I had the cracked image problem, I kept trying till and I got it to work with this code, it's written in the same php file no need to make a get.php

    IMPORTANT : staff.image is my database.table, just write your table name if u declared the database
    make sure you replace my variables with your correct ones, copy pasting the code won't work, you'll need to adjust with yours.

    if ($image_size==FALSE)
    {

    echo "Make sure you upload an image";
    }
    else
    {
    if (!mysqli_query($conn,"INSERT INTO staff.image(`user`, `img`, `img_name`)
    VALUES('".$_SESSION['user']."', '$image', '$Img_Name')"))
    {
    echo "problem uploading image";
    }
    else
    {
    $lastid = $_SESSION['user'];
    echo "Image uploaded.<p /> Your image:<p />";

    $image3 = "SELECT * FROM staff.image";
    $r=mysqli_query($conn, $image3);
    $row = mysqli_fetch_array($r);

    echo '<img src="data:image/jpeg;base64,' . base64_encode( $row['img'] ) . '" />';
    echo $row['img_name'];
    }

    }

    I'm on YT most of the time so if anyone has any problems with their code just reply to my comment and I'll try to help as much as I can

  2. when i add the header("Content-type: image/jpeg"); all i get is a square in the top left corner with no other content. not even my image. what am i doing wrong. All my code is the same from then on

  3. the image is inserted into the database but it is showing an image symbol not the image…i am trying to update actually..here is my code …

    profile.php

    <form action="profile.php" method="POST" enctype="multipart/form-data">
    <h4><b>FILE</b></h4>
    <input type="file" name="image">
    <input type="submit" value="upload">
    </form>
    <?php

    $file = $_FILES['image']['tmp_name'];

    if(!isset($file))
    echo "please select an image";
    else
    {
    $image = addslashes(file_get_contents($_FILES['image']['tmp_name']));
    $image_name = addslashes($_FILES['image']['name']);
    $image_size = getimagesize($_FILES['image']['tmp_name']);

    if($image_size == false)
    echo "that's not an image";
    else
    {
    if(!$query = mysqli_query($mysqli,"UPDATE user SET profile = '".$image."' WHERE id = '".$_SESSION['user_id']."' "))
    echo "problem uploading";
    else
    {
    $lastid = $_SESSION['user_id'];
    echo "image uploaded.<p> your image: </p><img src=get.php?id=$lastid>";
    }
    }
    }
    ?>

    get.php

    <?php include("connect.php");
    session_start();

    $id = $_SESSION['user_id'];

    $image = mysqli_query("SELECT * FROM user WHERE id='".$id."'");

    $row = mysqli_fetch_array($image);

    echo $row['image'];
    ?>

  4. use longblob instead of blob, and write <img src='get.php?id=$lastid' /> instead of what he wrote (use ' before get and after $lastid). With that last thing it worked for me (longblob is if you use big big pictures )

  5. when im running mine it looks like this

    File: Choose File Upload
    Your image:
    "; } } } ?>

    and the image is not showing,
    what am i gonna do? i really need it for our thesis can somebody help me please 🙁

  6. If the image is not showing and you're just getting a broken image icon when you open the get.php file in your browser then it might be because you're including somthing in that document. So remove any include "document.php" from the get document. Also make sure you do not have AdBlock enabled as it will prevent you from loading an image from another page like we're doing here.

  7. The showing a broken icon image was driving me MAD man, but I got it. I fixed two things. You need to try them both independently or together. First, I changed the header to header("Content-type: image/png"); instead of /jpeg.           Secondly, on the get.php, I used the database id field as he did in this video "id" EXCEPT in my database, mine I called it 'IMG_ID"  so make sure your database id name matches on the SELECT WHERE statement to whatever you called it.  so I changed it from ("SELECT * FROM Member_IMAGE WHERE id='$id'");  TO ("SELECT * FROM Member_IMAGE WHERE IMG_ID='$id'");  It took me 4 hours to figure it out and hundreds of mistakes, but I have it working. Good luck. 

  8. THe other thing you HAVE to do is put "enctype="multipart/form-data"" in your form tag like so:
    <form enctype="multipart/form-data" action="index.php" method="POST">

    It was driving me absolutely mad because i couldn't figure out what i was doing wrong until I read about that extra tag attribute you have to put in for newer versions of PHP.
    The other thing I changed in my script was when the database connection is established. You should only establish the connection once you know that the uploaded file is valid and is an image. I know its just a rough tutorial but i wanted to say something 😛 For the curious people who want to make their code more efficient.

  9. Something wrong with the code. The img data is being stored in the database but a broken image logo in the output. I changed blob to longblob but still doesn't work.

    Your other tutorials are great, but this one is not complete. Please take it down if you won't fix the tutorial because it does waste people's time and effort. But thanks for your other tutorials. 

  10. my images are not fully displaying. only like the fist 200px in width is displaying. I think its something to do with the file size. in the video his files were like 3.5kb mine are 64kb.

Leave a Reply