www.webdeveloper.com
Results 1 to 4 of 4

Thread: Uploading image and storing it in MySQL Database

  1. #1
    Join Date
    Dec 2012
    Posts
    20

    Question Uploading image and storing it in MySQL Database

    Hi there guys can someone help me with this simple profile code. Everything is perfect I just wanna store Profile Pictures in the database. Can someone help me add simple code to upload photo to database???
    My code is as follows

    Code:
    <html>
    <head></head>
    <body>
    
    <?php
    
    
        $username=$_POST['username'];
    	$password=$_POST['password'];
    	$confirm_password=$_POST['confirm_password'];
    	$email=$_POST['email'];
    	$first_name=$_POST['first_name'];
    	$last_name=$_POST['last_name'];
    	
        $conn=mysql_connect("localhost","root","******") or die('Error');
        mysql_select_db("oracle",$conn) or die('Error');
    	$sql="INSERT INTO profile (username,password,confirm_password,email,first_name,last_name,image)//image attribute in DB is blob type.
    	        VALUES ('$username','$password','$confirm_password','$email','$first_name','$last_name',' ')";
    	mysql_query($sql,$conn);
    	mysql_close($conn);
    	}
    ?>
    
    
    
    
        <form method="post" action="test.php" enctype="multipart/form-data">
        Username :<input type="text" name="username"/><br>
        Password :<input type="text" name="password"/><br>
        Confirm Password : <input type="text" name="confirm_password"/><br>
        Email : <input type="text" name="email"/><br>
        First Name :<input type="text" name="first_name"/><br>
        Last Name :<input type="text" name="last_name"/><br>
        Upload Image :<input type="file" name="image" id="file"><br>
        <input type="submit" value="Submit"/>
         </form>
    
    </body>
    </html>

  2. #2
    Join Date
    Dec 2012
    Posts
    95
    You really should escape all your form results with mysql_real_escape_string to prevent SQL injection, to answer you question you would upload an image like this

    Code:
    <html>
    <head></head>
    <body>
    
    <?php
    
    
        $username=$_POST['username'];
    	$password=$_POST['password'];
    	$confirm_password=$_POST['confirm_password'];
    	$email=$_POST['email'];
    	$first_name=$_POST['first_name'];
    	$last_name=$_POST['last_name'];
    	$image = file_get_contents($_FILES['image']['tmp_name']);
            $image = mysql_real_escape_string($image);
        $conn=mysql_connect("localhost","root","******") or die('Error');
        mysql_select_db("oracle",$conn) or die('Error');
    	$sql="INSERT INTO profile (username,password,confirm_password,email,first_name,last_name,image)//image attribute in DB is blob type.
    	        VALUES ('$username','$password','$confirm_password','$email','$first_name','$last_name','$image')";
    	mysql_query($sql,$conn);
    	mysql_close($conn);
    	}
    ?>
    
    
    
    
        <form method="post" action="test.php" enctype="multipart/form-data">
        Username :<input type="text" name="username"/><br>
        Password :<input type="text" name="password"/><br>
        Confirm Password : <input type="text" name="confirm_password"/><br>
        Email : <input type="text" name="email"/><br>
        First Name :<input type="text" name="first_name"/><br>
        Last Name :<input type="text" name="last_name"/><br>
        Upload Image :<input type="file" name="image" id="file"><br>
        <input type="submit" value="Submit"/>
         </form>
    
    </body>
    </html>
    But it's usually considered better practise to upload the file to the server and then simply store a link to the file in the database rather than storing the whole image.

  3. #3
    Join Date
    Dec 2012
    Posts
    20
    Thanks so much, for simple code .

    How can we save path of image in database?

  4. #4
    Join Date
    Jan 2013
    Posts
    2
    First just pick a place were you want to upload the photos eg. $path = './photos/'; and add to the table you use something like picture varchar(255). and when you upload the picture just add the name of the photo to sql and when you want to see it you use $path.$name...

Thread Information

Users Browsing this Thread

There are currently 4 users browsing this thread. (0 members and 4 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles