Click to See Complete Forum and Search --> : Inserting into mysql database


wvmlt
11-29-2004, 04:34 AM
I have a form where users can upload an avatar

<form name="uploader" method="post" action="<? echo $PHP_SELF; ?>" enctype="multipart/form-data">
<input type="file" name="image" value="Browse">
<input type="submit" name="upload" value="Upload Image"><br><BR>
Maximum size and dimensions accepted are 20 KB and 100 X 100 pixels.
</form>
<?php
$domain = "mydomain.com";
$path_after_domain = "forum/uploads/";
if($_POST['upload']) {

if ($_FILES['image']['name'] == "") {
echo "Please select a file to upload!\n";
exit;
}
$uploads = "/home/mydomain/public_html/forum/uploads";
$types_array = array("image/gif","image/pjpeg","image/x-png");

if (!in_array($_FILES['image']['type'], $types_array)) {
echo "That file type is not allowed!\n";
exit;
}

$max_filesize = 20000;

if ($_FILES['image']['size'] > $max_filesize) {
echo "That file is too large!<br>The maximum file size allowed is 20 KB.\n";
exit;
}
$imagesize = getimagesize($_FILES['image']['tmp_name']);
$imagewidth = $imagesize[0];
$imageheight = $imagesize[1];

$maxwidth = 100;
$maxheight = 100;

if($imagewidth > $maxwidth || $imageheight > $maxheight) {
echo "That file is too large!<br>The maximum file size allowed is 100 X 100 pixels.\n";
exit;
}

move_uploaded_file($_FILES['image']['tmp_name'], "".$uploads."/".$_FILES['image']['name']) or die ("Couldn't upload file!");







echo "File uploaded";
echo "<br><br><img src=\"http://".$domain."/".$path_after_domain.$_FILES['image']['name']."\">";
echo "<br><br>h t tp://".$domain."/".$path_after_domain.$_FILES['image']['name']."";
}
?>



The database name is forum and the table name is users and the field where the avatar's url is stored is useravatar. How can I do this, so when a user uploads an avatar, the url is sent to the database

Kyleva2204
11-29-2004, 06:55 AM
Originally posted by wvmlt
I have a form where users can upload an avatar

<form name="uploader" method="post" action="<? echo $PHP_SELF; ?>" enctype="multipart/form-data">
<input type="file" name="image" value="Browse">
<input type="submit" name="upload" value="Upload Image"><br><BR>
Maximum size and dimensions accepted are 20 KB and 100 X 100 pixels.
</form>
<?php
$domain = "mydomain.com";
$path_after_domain = "forum/uploads/";
if($_POST['upload']) {

if ($_FILES['image']['name'] == "") {
echo "Please select a file to upload!\n";
exit;
}
$uploads = "/home/mydomain/public_html/forum/uploads";
$types_array = array("image/gif","image/pjpeg","image/x-png");

if (!in_array($_FILES['image']['type'], $types_array)) {
echo "That file type is not allowed!\n";
exit;
}

$max_filesize = 20000;

if ($_FILES['image']['size'] > $max_filesize) {
echo "That file is too large!<br>The maximum file size allowed is 20 KB.\n";
exit;
}
$imagesize = getimagesize($_FILES['image']['tmp_name']);
$imagewidth = $imagesize[0];
$imageheight = $imagesize[1];

$maxwidth = 100;
$maxheight = 100;

if($imagewidth > $maxwidth || $imageheight > $maxheight) {
echo "That file is too large!<br>The maximum file size allowed is 100 X 100 pixels.\n";
exit;
}

move_uploaded_file($_FILES['image']['tmp_name'], "".$uploads."/".$_FILES['image']['name']) or die ("Couldn't upload file!");

$file_url = $domain/$path_after_domain/$_FILES['image']['name'];







echo "File uploaded";
echo "<br><br><img src=\"http://".$domain."/".$path_after_domain.$_FILES['image']['name']."\">";
echo "<br><br>h t tp://".$domain."/".$path_after_domain.$_FILES['image']['name']."";
}
?>



The database name is forum and the table name is users and the field where the avatar's url is stored is useravatar. How can I do this, so when a user uploads an avatar, the url is sent to the database
$file_url = $domain/$path_after_domain/$_FILES['image']['name']; is prb what you want :-\ it may not work... but heck its a try :-P.

Jaelan
11-29-2004, 04:34 PM
If I understand you correctly, this would be the code to connect to the right database, and insert the value $file_url into the column 'useravatar' of the 'users' table:


/*CONNECT TO DB 'FORUM' */
$conn = mysql_pconnect("localhost","username","password");
$mysql_select_db("forum");

/* INSERT INTO TABLE 'users' */
$file_url = "$domain/$path_after_domain/".$_FILES['image']['name'];
$query = "UPDATE users SET useravatar='$file_url' WHERE name='$user_name';

if (mysql_query($query, $conn)) {
echo "The url $file_url was inserted into the database successfully.";
}


Some things to take note of:
username and password in the mysql_pconnect() function refer to your mysql login values.

I assume you're using the user's name as a key, and thus the $query value will update only the sets in the database where name='user's name'.

This should work (though I'm a newb myself and don't know the difference between all the different connect functions PHP offers). If this wasn't what you were asking at all, I apologize
:D

--Jaelan

wvmlt
11-30-2004, 03:40 PM
Thanks guys.

I ended up putting
mysql_query("UPDATE ${table_prefix}users SET useravatar='$file_url' WHERE userid='$logincookie[user]'");

Directly below


move_uploaded_file($_FILES['image']['tmp_name'], "".$uploads."/".$_FILES['image']['name']) or die ("Couldn't upload file!");


Works like a charm :D