lets say im making a dating site. and i want people to be able to insert their own pictures. how would i do that? i have a mysql database. and i want this to happen in php. and then how would i make it display that persons image when someone visits their profile?

Recommended Answers

All 8 Replies

You have two options, you can store the images in mysql itself, which I would not recommend, or you can upload the images to your server and store the paths to the user's images in mysql.

When a user hits a profile, it would query the database for 1 or more photos of the user retrieve the path and then create a series of <img> tags with the correct paths.

can you show me an example of the second one?

What exactly do you want an example of? The database structure, uploading files with php or storing/retrieving the data from the database?

Try this sample idea.
First, create a table with the following fields. user_name,user_img and user_id. Set user_id as your primary key. It can be integer that increment automatically. Save the table as userfile

Create a PHP connection script. In this example we save it as connection.php. Below is the code.

<?php
     
    error_reporting(0);
    $connection = Mysql_connect('localhost','root','');
    mysql_select_db('name_of_db');
?>

You can now create another page which will allow the user to save their profile. Lets say we save this script as save_profile.php

<?php

    error_reporting(0);
    session_start();
    include 'connection.php';

echo "<form action='save_profile.php' method='post' enctype='multipart/form-data'>";
echo "USER NAME : <input type='text' name='username'>"."<br>";
echo "PICTURE : <input type='file' name='file' id='file'>"."<br>";
echo "<input type='submit' name='submit' value='Save'>";
echo "</form>";

if($_POST['submit']=="Save")
{
   $user = $_POST['user'];
   $img = "image_folder/".$_FILES["file"]["name"];

//now move/save or store the image file into the folder.
//you must also create a folder named image_folder in your drive together with this php script.
move_uploaded_file($_FILES["file"]["tmp_name"],"../wwwroot/image_folder/".$_FILES["file"]["name"]);

 $query = mysql_query("INSERT INTO userfile(user_name,user_img) values('$user','$img')");
  
}

?>

Now here's the code in viewing. (Hay naku. medyo nahihirapan nako.)

<?php
error_reporting(0);
session_start();
include 'conn.php';

$query = mysql_query("SELECT * from userfile");
echo "<table>";
echo "<tr>";
echo "<td>"."NAME"."</td>";
echo "<td>"."PICTURE"."</td>";
while($result = mysql_fetch_array($query))
{
   echo "<tr>";
   echo "<td>".$result['user_name']."</td>";
?>
   <td><img src="<?php echo $result['user_img'];?>"></td>
<?php
 echo "</table>";


}

?>

Obviously, this code is very general. But I think you can have at least a little knowledge/idea on what you want. I hope this can help.

i am using this and i want to picture to go to a directory userpics. i keep getting *failed*
here is my code:

<?php
session_start();
$id=$_SESSION['uid'];
$img = "image_folder/".$_FILES["file"]["name"];
move_uploaded_file($_FILES["file"]["tmp_name"],"/userpics/".$_FILES["file"]["name"]);

$query = mysql_query("UPDATE members SET picture='$img' WHERE id='$id'");
if($query){
	header('location:memberspage.php')
}else{
		echo"fail";
	}

im no longer getting an error, now it just isnt sending the image to the directory

Make sure that your folder is writable.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.