Jump to content

unable to display image from database


azra

Recommended Posts

             1. HTML FORM

            

          #for user to enter the data

        

         <html>

            <title>reg</title>

            <style type="text/css">

            body {

            background-color: rgb(200,200,200);

            color: white;

            padding: 20px;

            font-family: Arial, Verdana, sans-serif;}

            h4 {

            background-color: DarkCyan;

            padding: inherit;}

            h3 {

            background-color: #ee3e80;

            padding: inherit;}

            p {

            background-color: white;

            color: rgb(100,100,90);

            padding: inherit;}

           

            

           

            </style>

           

            <form method="POST" action="login_back.php" enctype="multipart/form-data"></br>

            &nbsp<font color="DarkCyan"> Choose a user name:</font> <input type="text" name="username">

            </br></br>

            &nbsp<font color="DarkCyan"> First name:</font> <input type="text" name="firstname"/>

            </br></br>

            &nbsp<font color="DarkCyan">  Last name:</font><input type="text" name="lastname"/>

            </br></br>

            &nbsp<font color="DarkCyan"> File: <input type="file" name="image"></font>

            </br></br>

            <input type="submit" value="Save and Proceed">

            </form>

            </html>

            

            

            ----------

             2 STORING IN DATABASE

        #backend processing to store and retrieve data from db 

        

            <?php

            error_reporting(0);

            #echo "<body style='background-color:rgb(200,200,200)'>";

            session_start();

            #if( isset($_POST['username']) && isset($_FILES['image']) )

            #{

            $_SESSION['username']=$_POST['username'];

            $_SESSION['firstname']=$_POST['firstname'];

            $lastname=$_POST['lastname'];

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

            $image_size=getimagesize($_FILES['image']['tmp_name']);

            if(!isset($file))

            echo"please select an image";

            else

            {

            #$image=$_FILES['image']['tmp_image']; //grabing the file content

            $image_name=$_FILES['image']['name']; //grabing image name

            $image_size=getimagesize($_FILES['image']['tmp_name']); //getting image size

            }

            echo "</br>";

            

            

            #connection to db

            mysql_connect("localhost","root","")or die(mysql_error());

            mysql_select_db("wordgraphic")or die(mysql_error());

            

            

            

            #checking the available username

            $query = mysql_query("SELECT * FROM userdata WHERE username = '" . $_SESSION['username'] . "'" ); 

            $ans=mysql_num_rows($query);

            if ($ans > 0) 

            { 

                 echo "Username already in use please try another.";

           

            }

            

            else if($image_size==FALSE)

            {

            echo"That's not an image.";

            

            }

            

            else

            {

            #Insert data into mysql

            

            #1.Inserting user name & image into db

            $sql="INSERT INTO userdata(username, firstname, lastname, image)VALUES('" . $_SESSION['username'] . "', '" . $_SESSION['firstname'] . "', '$lastname','$image')";

            $result1=mysql_query($sql);

            

            if($result1)

            {

            echo "</br>";

            echo "Registration successful";

            echo "</br>";

            

            //displaying image

            $lastid=mysql_insert_id();//get the id of the last record

            echo "uploaded image is :";

            echo "<img src='get.php?id=".$lastid."'>"; 

        

        >  this command has some mistake

        

            

            

            }#if insertion into db successful

            else 

            {

            echo "Problem in database operation";

           

            }

            }# else block of unique username n img

            }#end of isset

            ?>

        

        

        ----------

        

            3. GET.PHP

       #additional file that retrieve image from database

    

            <?php

            #connection to db

            mysql_connect("localhost","root","")or die(mysql_error());

            mysql_select_db("wordgraphic")or die(mysql_error());

            if(isset($_REQUEST['id']) )

        

        > this block of code is not running

        

            {

            $mid=(int)($_REQUEST['id']);

            

            $image=mysql_query("SELECT * FROM userdata WHERE id=$mid") or die("Invalid query: " . mysql_error());

            $image=mysql_fetch_assoc($image);

            $image=$image['image'];

            header("Content-type: image/jpeg");

            echo $image;

            }

            else

            echo"error";

            ?>

Link to comment
Share on other sites

1. Use code tags for code in the forum! Your code is hard to read without formatting!

 

2. Where you say "this command has some mistake", how do you know, what is is it like when in the html source page?

 
3. At the end, where you write echo "error"; There is a missing }, which will make the whole script fail!
 
4. Turn error reporting on ;)
Link to comment
Share on other sites

 

1. Use code tags for code in the forum! Your code is hard to read without formatting!

 

2. Where you say "this command has some mistake", how do you know, what is is it like when in the html source page?

 
3. At the end, where you write echo "error"; There is a missing }, which will make the whole script fail!
 
4. Turn error reporting on ;)

 

1.m new on online discussing i will try n repost sorry fr d inconvenience caused.

2.i know the command has some mistake because user name, image first name, last name are getting stored in db seamlessly but when m trying to display image from database i am facing prob and in the third segment of code isset() segment is not running hence, data is not getting transfered between pages .

3. else has only one statement under it i.e. echo so skipping {} will do as with most other languages.

4. turned on error reporting.

thank you

Link to comment
Share on other sites

2. Seamlessly, do you mean successfully or concatenated together? Looking at that code they shouldn't get concatenated!

... Have you tried manually calling the view image page? That code looks right to me!

3. Oops

 

... When testing the view page manually output some other text to verify, not the image ;)

 

 

BTW you could you edit the initial post so tha it uses code blocks, you'll be surprised how much easier it is to spot issues when properly indented...

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.