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>
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.
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...
Bookmarks