I'm creating a link sharing website and I want the user that registers to upload an avatar and use that avatar throughout the website.

I got so far that the user can register but can't find a way for him/her to have an avatar.

Here I have the signup.php (for the users to register in) file so that you can see what I mean.

<?php
    include 'connect.php';
    include 'header.php';

    echo '<h3>Register</h3><br />';

    if($_SERVER['REQUEST_METHOD'] != 'POST')
    {
      /*the form hasn't been posted yet, display it
  note that the action="" will cause the form to post to the same page it is on */

    echo '<form method="post" action="">
    <b>Username: </b><input type="text" name="user_name" /><br/><br/>
    <b>Password: </b><input type="password" name="user_pass"><br/><br/>
    <b>Confirm assword: </b><input type="password" name="user_pass_check"><br/>      <br/>
    <b>E-mail: </b><input type="email" name="user_email"><br/><br/>

          ///////////////////////////////////////////////////////////
          ///////// must I use <input type="file"> here???? /////////
          ///////// and how do I put it in the database???? /////////
          /////////////////////////////////////////////////////////// 

    <input type="submit" value="Join" />
 </form>';
    }
    else
    {
    /* so, the form has been posted, we'll process the data in three steps:
    1.  Check the data
    2.  Let the user refill the wrong fields (if necessary)
    3.  Save the data 
*/
$errors = array(); /* declare the array for later use */

if(isset($_POST['user_name']))
{
    //the user name exists
    if(!ctype_alnum($_POST['user_name']))
    {
        $errors[] = 'The username can only contain letters and digits.';
    }
    if(strlen($_POST['user_name']) > 30)
    {
        $errors[] = 'The username cannot be longer than 30 characters.';
    }
}
else
{
    $errors[] = 'The username field must not be empty.';
}


if(isset($_POST['user_pass']))
{
    if($_POST['user_pass'] != $_POST['user_pass_check'])
    {
        $errors[] = 'The two passwords did not match.';
    }
}
else
{
    $errors[] = 'The password field cannot be empty.';
}

if(!empty($errors)) 

    /*check for an empty array, if there are errors, they're in this array (note the !     operator)*/
{
    echo 'Uh-oh.. a couple of fields are not filled in correctly..<br /><br />';
    echo '<ul>';
    foreach($errors as $key => $value) /* walk through the array so all the errors get displayed */
    {
        echo '<li>' . $value . '</li>'; /* this generates a nice error list */
    }
    echo '</ul>';
}
else
{



    //the form has been posted without, so save it
    //notice the use of mysql_real_escape_string, keep everything safe!
    //also notice the sha1 function which hashes the password
    $sql = "INSERT INTO
                users(user_name, user_pass, user_email ,user_date, user_level)
            VALUES('" . mysql_real_escape_string($_POST['user_name']) . "',
                   '" . sha1($_POST['user_pass']) . "',
                   '" . mysql_real_escape_string($_POST['user_email']) . "',
                    NOW(),
                    0)";

    $result = mysql_query($sql);
    if(!$result)
    {
        //something went wrong, display the error
        echo 'Something went wrong while registering. Please try again   later.';
        //echo mysql_error(); //debugging purposes, uncomment when needed
    }
    else
    {
        echo 'Succesfully registered. You can now <a href="signin.php">sign  in</a> and start sharing links.';
    }
}
}

include 'footer.php';
?>

Here are my database files. Must I create a new one for the user's images? Please help!!

CREATE TABLE users (  
user_id     INT(8) NOT NULL AUTO_INCREMENT,  
user_name   VARCHAR(30) NOT NULL,  
user_pass   VARCHAR(255) NOT NULL,  
user_email  VARCHAR(255) NOT NULL,  
user_date   DATETIME NOT NULL,  
user_level  INT(8) NOT NULL,  
UNIQUE INDEX user_name_unique (user_name),  
PRIMARY KEY (user_id)  
);

CREATE TABLE categories (  
cat_id          INT(8) NOT NULL AUTO_INCREMENT,  
cat_name        VARCHAR(255) NOT NULL,  
cat_description     VARCHAR(255) NOT NULL,  
UNIQUE INDEX cat_name_unique (cat_name),  
PRIMARY KEY (cat_id)  
);

CREATE TABLE topics (  
topic_id        INT(8) NOT NULL AUTO_INCREMENT,  
topic_subject       VARCHAR(255) NOT NULL,  
topic_date      DATETIME NOT NULL,  
topic_cat       INT(8) NOT NULL,  
topic_by        INT(8) NOT NULL,  
PRIMARY KEY (topic_id)  
); 

CREATE TABLE posts (  
post_id         INT(8) NOT NULL AUTO_INCREMENT,  
post_content        TEXT NOT NULL,  
post_date       DATETIME NOT NULL,  
post_topic      INT(8) NOT NULL,  
post_by     INT(8) NOT NULL,  
PRIMARY KEY (post_id)  
);

I'm kind of new to this field of work and would REALLY appreciate it if you can help me!!!


Thanks!

Recommended Answers

All 7 Replies

For simplicity I would add a table with image_id and image name (given that images folder is fixed). Then in users I will add user_image_id which will tell me what avatar user belongs to. The rest is simple!
It might not be the best (I rushed to answer with no deep thinking/scrutiny) but it should work!

Member Avatar for diafol

An increment or hash system for the naming of images may be useful. If you're uploading to the same folder, eventually, users will upload with the same name - and depending on your code - may overwrite. You can enter BLOB data from the file directly into the DB, so it doesn't much matter what happens to the file then. You could even run a cron job to delete images every day or week.

Otherwise, an useful thing to do is to rename the image on upload, e.g. username_time.ext (ardav_1318622004.jpg). You then store this name in the DB.

Otherwise, an useful thing to do is to rename the image on upload, e.g. username_time.ext (ardav_1318622004.jpg). You then store this name in the DB.

I think this is the best than Blob. Blobs are heavy and will make DB grow fast especially if images limit are big

Member Avatar for diafol

I think this is the best than Blob. Blobs are heavy and will make DB grow fast especially if images limit are big

I'd like to agree - I hate blobs. However, some of the very popular BBs use them, so I don't know how heavy they are.

I'd like to agree - I hate blobs. However, some of the very popular BBs use them, so I don't know how heavy they are.

the rule of thumb is if it fits your need use it and when it become too much migrate to something more feasible!

Member Avatar for diafol

The OPer has left the building... :)

The OPer has left the building... :)

Very funny ;)

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.