Sunday, December 14, 2014

HOW TO DISPLAY IMAGE ON PHP PAGE

After storing the image in database, now its time to display it. so how do we do it?

The codes:

<!DOCTYPE HTML>
<html>
<body>

<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">
ID:<br><input type="varchar" name="id">
<input type="submit" name="submit" value="Search">
</form>

<?php

$db_host = "localhost";
$db_username = "root";
$db_pass = "usbw";
$db_name = "image";

@mysql_connect ("$db_host", "$db_username", "$db_pass") or die ("Could not connect to MySQL");
@mysql_select_db("$db_name") or die ("No database");

if(isset($_POST['submit'])) {

    //id no sent from form
    $id=$_POST['id'];

    // To protect MySQL injection
    $id= stripslashes($id);
    $id= mysql_real_escape_string($id);
    $data=mysql_query("SELECT * FROM img WHERE id='$id'");

    if ($row = mysql_fetch_array($data)) {

    $photo = $row['path'];
    echo "<style='text-align:center'><center><img src=$photo alt=Profile Photo height=300px  
    width=300px></center>"."</br>";
    }
}
?>
</body>
</html>

Lets test the code!



We are done. yea! easy peasy right...  have a try and good luck! ><


HOW TO STORE IMAGE IN MYSQL DATABASE FROM PHP

My previous post of the same topic was rather too easy. But how do we do it using some programming say PHP?. so lets start...

<!DOCTYPE HTML> 

<?php
   $db_host = "localhost";
   $db_username = "root";
   $db_pass = "usbw";
   $db_name = "image";
?>
<html>
<head>
</head>
<body>

<form> name="Image" enctype="multipart/form-data" action="<?php echo $_SERVER['PHP_SELF']; ?>"              method="POST"> 
         <center><fieldSet style="width:50%">
                        <p><input type="file" name="Photo" size="2000000"        
                         accept="image/gif, image/jpeg, image/x-ms-bmp, image/x-png" size="26"></p>
                        <input type="submit" name="submit" value="Upload image" size="50"> 
        </fieldSet></center>
</form>

$uploadDir='images/'; //image upload folder

if(isset($_POST['submit'])) {
   $fileName = $_FILES['Photo']['name'];
   $tmpName  = $_FILES['Photo']['tmp_name'];
   $fileSize = $_FILES['Photo']['size'];
   $fileType = $_FILES['Photo']['type'];
   $filePath = $uploadDir . $fileName;
   $result = move_uploaded_file($tmpName, $filePath);
   
   if (!$result) {
      echo "Error uploading file";
   exit; 
   }
   
   if(!get_magic_quotes_gpc()) {
       $fileName = addslashes($fileName);
       $filePath = addslashes($filePath); 
   }

   @mysql_connect ("$db_host", "$db_username", "$db_pass") or die ("Could not connect to 
       MySQL");
   @mysql_select_db("$db_name") or die ("No database");
   @mysql_query(" INSERT INTO `img`(`path`) VALUES ('$filePath')"); 

   echo '<script>alert("Image is successfully uploaded");</script>';
} 
else {
   echo "Complete the form before submitting";
}
?>
</body>
</html>

This is how it looks like on PHP page



Testing our codes:

1) upload new image [canary.jpg] : 


2) recheck your database to see if the image is successfully uploaded or not. (im my case, YEA! it works)



 SO WE ONLY FINISH THE FIRST STEP. LETS HIT THE NEXT ONE


HOW TO STORE IMAGE IN SQL DATABASE DIRECTLY

What you really need?
  • USBwebserver or any other tools Xampp etc.
1) create a new folder [images] inside root folder

2) inside images folder, insert any picture you would like to store into database. say, i use below picture and name it arrow.jpg (use simple/short name)

3) Sign in into PHP Admin, Username: root , Password: usbw


4) create new database , and name it 'image' 

5) inside image database, create a table 'img' with 2 columns .
  • name: id - type: INT - length: 3 - AI: tick
  • name: path - type: VARCHAR - length: 200 


6) press INSERT tab at the top and fill in the value as shown in the figure below:
  • for path, write in the name of the folder [images] and followed by the image's name [arrow.jpg] :           images/arrow.jpg

SO WE ONLY FINISH THE FIRST STEP. LETS HIT THE NEXT ONE..

Saturday, September 6, 2014

Since nobody asks/tags 20 FACTS ABOUT ME me on instagram, I feel tempted to try this and post it on this new blog. So here we go

20 FACTS ABOUT ME

1. I will never look good on make up (i tried once and it looked like i’m having a rough time talking to an idiot)
2. I still can’t differentiate between LENGKUAS, KUNYIT and SERAI.
3. I can never apply MARA but a lot of people asking me to try, they just dunno why i couldnt do dat
4. I have never celebrate RAYA in my kampung since its really far
 from Malaysia
5. I don’t have any close friend that i can talk shit about each other
6. I don’t like being told to dress up like a WOMAN , to be updated with fashion nowadays. Fuck off. I don’t revolve around fashion.. I’ve my own style..even selekeh, still me..
7. I dislike being at crowded place cause I tend to feel insecure as if people r staring weirdly at me..
8. I am kinda tv series-maniac.. i get attached to tv series easily.
9. Tend to forget other’s name
10. My sisters believe that I have split personality. Maybe. I aint Norman Bates. I don’t kill.
11. I’ve never say I LOVE YOU to my parents verbally.
12. I’m not a shy girl but I will feel awkward around people i know/i don’t sometimes
13. I believe I can be the next christina perri. ><
14. I like to do shopping alone
15. I have so many crushes (few i’ve fallen so hard) but none of them have crush on me. life sucks heh
16. I dislike taking picture outside of any building because it won’t come out pretty.
17. I like to do programming and I dream someday I could hack Daniel Sharman’s twitter. ><
18. I love British.. Everything about british (the boys,the men, the accent, the culture, the landscape, the buildings).
19. Whenever I topup my credit phone RM5/RM10 it won’t be RM0 until next 5 months (the worst scenario) because…
20. I am a weirdo


The End.. xx