Click to See Complete Forum and Search --> : [RESOLVED] User Avitar Upload pic
mitchell
11-16-2007, 07:38 AM
i have a really good plan but i cant do this , bear with me guys.
i want a system that a user can upload a picture and then i can easyily put a
echo "$user [picture]
on the profile page and it gets there uploaded picture.
this is how my site handles users:
$user = MYSQL_QUERY("SELECT * from users WHERE id='$_COOKIE[id]' AND password = '$_COOKIE[pass]'");
$user = mysql_fetch_array($user);
i will be so happy if anyone can help me or give me any info about this.
trymbill
11-16-2007, 07:58 AM
Hi Mitchell,
First off you need an upload form to upload the image from the clients computer to your server. Here are a few nice tutorials on this:
http://www.reconn.us/content/view/30/51/
http://www.tizag.com/phpT/fileupload.php
http://www.phpfreaks.com/tutorials/36/0.php
After that, you can simply update the users mysql row and set "image = 'imagename.jpg'" and display it on his page like so:
<img src="/path/to/image/<?=$user[image]?>" />
Hope this helps ...
bokeh
11-16-2007, 08:01 AM
$user = MYSQL_QUERY("SELECT * from users WHERE id='$_COOKIE[id]' AND password = '$_COOKIE[pass]'");
$user = mysql_fetch_array($user);You need to drop the habit of variable swapping like that.
To answer your question:echo "<img src='{$user['avatar_URL']}' alt='user avatar'>";
trymbill
11-16-2007, 08:09 AM
To answer your question:echo "<img src='{$user['avatar_URL']}' alt='user avatar'>";
What does {} do inside the '{$user['avatar_URL']}' ?? :)
mitchell
11-16-2007, 08:28 AM
thanks for helping , ive choosen
http://www.reconn.us/content/view/30/51/
but how does it know whitch picture is whos if i put:
<img src="/path/to/image/<?=$user[image]?>" />
mitchell
11-16-2007, 08:52 AM
Ok i have set everything up but what code should i put so it stores the image in the database
trymbill
11-16-2007, 08:55 AM
You can just add a column into your "users" table and name it "image_location" or something and then you store the name of the image there at the same time the user uploads the image.
So when your user looks at his page, he sees his image.
bokeh
11-16-2007, 08:58 AM
What does {} do inside the '{$user['avatar_URL']}' ?? :)Complex (curly) syntax (http://www.php.net/manual/en/language.types.string.php#language.types.string.parsing.complex)
mitchell
11-16-2007, 09:02 AM
thanks so much
ive added the image_location but how do i store the name of the image there at the same time the user uploads the image?
ive choosen http://www.reconn.us/content/view/30/51/
trymbill
11-16-2007, 09:07 AM
mysql_query("update users set image_location='.$_FILES['image']['name'].' where id='$_COOKIE[id]' AND password = '$_COOKIE[pass]'");
bokeh
11-16-2007, 09:08 AM
thanks so much
ive added the image_location but how do i store the name of the image there at the same time the user uploads the image?This is dealt with in depth in this thread (http://www.webdeveloper.com/forum/showthread.php?t=96532).
mitchell
11-16-2007, 09:10 AM
your a star people thanks so much for helping
but where should i put:
mysql_query("update users set image_location='.$_FILES['image']['name'].' where id='$_COOKIE[id]' AND password = '$_COOKIE[pass]'");
in the code.
http://www.reconn.us/content/view/30/51/
trymbill
11-16-2007, 09:53 AM
your a star people thanks so much for helping
but where should i put:
mysql_query("update users set image_location='.$_FILES['image']['name'].' where id='$_COOKIE[id]' AND password = '$_COOKIE[pass]'");
in the code.
http://www.reconn.us/content/view/30/51/
Doesn't matter as long if $_FILES['image']['name'] has been declared. Just test it out for your self and you will see ...
mitchell
11-16-2007, 10:04 AM
Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING
mitchell
11-16-2007, 10:05 AM
all i did was put it here:
//we verify if the image has been uploaded, and print error instead
$copied = copy($_FILES['image']['tmp_name'], $newname);
if (!$copied)
{
echo '<h1>Copy unsuccessfull!</h1>';
$errors=1;
}}}}
//If no errors registred, print the success message
if(isset($_POST['Submit']) && !$errors)
{
echo "<h1>File Uploaded Successfully! Try again!</h1>";
}
mysql_query("update users set image_location='.$_FILES['image']['name'].' where id='$_COOKIE[id]' AND password = '$_COOKIE[pass]'");
?>
bokeh
11-16-2007, 03:53 PM
Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRINGmysql_query("update users set image_location='{$_FILES['image']['name']} where id='{$_COOKIE['id']}' AND password = '{$_COOKIE['pass']}'"); Also don't forget to validate/clean those user editable variables (to prevent SQL injection).
mitchell
11-16-2007, 06:18 PM
thanks alot for the code but looking at the code {$_FILES['image']['name']} are we sure thats the right value to be stored in the code, but i might know why its not working because it might not be connecting to the database with the mysql_query.
This is the connection code:
<?
$dbhost = 'test';
$dbusername = 'test';
$dbpasswd = 'test';
$database_name = 'test';
// Database Stuff
$connection = mysql_connect("$dbhost","$dbusername","$dbpasswd")
or die ("Couldn't connect to server.");
$db = mysql_select_db("$database_name", $connection)
or die("Couldn't select database.");
$user = MYSQL_QUERY("SELECT * from users WHERE id='$_COOKIE[id]' AND password = '$_COOKIE[pass]'");
$user = mysql_fetch_array($user);
?>
is that correct for the script to connect
mitchell
11-17-2007, 12:38 PM
is
$_FILES['image']['name']
the right thing to get to put in the users image_location field:
http://www.reconn.us/content/view/30/51/
bokeh
11-18-2007, 03:43 AM
is $_FILES['image']['name'] the right thing to get to put in the users image_location fieldIf that variable contains the filename you want to use then yes, otherwise no.