0

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?

3
Contributors
8
Replies
9
Views
6 Years
Discussion Span
Last Post by lyrico
0

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.

0

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

0

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.

0

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";
	}
0

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

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.