upload files in php, store path in database tables

Step 1 : A HTML Page to Display a form (we are doing everything in this Page, so the code to retrieve the image + form data and to image upload all as :

<!DOCTYPE html>
<html>
<head>
<title>PHP Form Upload</title>
</head>
<body>
	<form method='post' action='index.php' enctype='multipart/form-data'>
		<input type="text" name="text1" id="text1" /> 
		Select File: <input type='file' name='filename' size='10' /> 
		<input type='submit' value='Upload' />
	</form>
<?php
if ($_FILES) 
{
    $title = $_POST['text1'];
    
    $fileName = $_FILES["filename"]["name"];
    $fileTmpLoc = $_FILES["filename"]["tmp_name"];
    
    // Full Path and file name from current directory
    $pathAndName = "uploads/" . $fileName;

    // Run the move_uploaded_file() function here
    $moveResult = move_uploaded_file($fileTmpLoc, $pathAndName);

    // Evaluate the value returned from the function if needed
    if ($moveResult == true) 
    {
        // If everything good, connect to database
        $db_server = mysql_connect("localhost", "username", "password");
        
        if (! $db_server)
            die("Unable to connect to MySQL: " . mysql_error());
        
        mysql_select_db("test") or
                 die("Unable to select database: " . mysql_error());
                 
        // Insert Query
        
        $query = "INSERT INTO imagesupload(title,imagesPath) VALUES('$title','uploads/$fileName')";
        
        $result = mysql_query($query);
        if (! $result)
            die("Database access failed: " . mysql_error());
        
        // Done insert, selct last record to display
        
        $sql = "SELECT * FROM imagesupload ORDER BY id DESC LIMIT 1";
        $result = mysql_query($sql);
        
        while ($row = mysql_fetch_array($result))
        {
            ?>
            <p> Title = <?php echo $row['title']; ?> </p>
            <img src="<?php echo $row['imagesPath']; ?>">
        <?php
        // All done
        }
    } 
    else
    {
        echo "File Upload Problem";
    }
}

?>
</body>
</html>

Step 2 : out table Structure is as follows, you may have yours customize structure

mysql> desc imagesupload;
+------------+--------------+------+-----+---------+----------------+
| Field      | Type         | Null | Key | Default | Extra          |
+------------+--------------+------+-----+---------+----------------+
| id         | int(11)      | NO   | PRI | NULL    | auto_increment |
| title      | varchar(20)  | NO   |     | NULL    |                |
| imagesPath | varchar(100) | NO   |     | NULL    |                |
+------------+--------------+------+-----+---------+----------------+
3 rows in set (0.01 sec)

Open form fill details and result will be displayed thereafter

Output



In our database details are displayed as :

mysql> select * from imagesupload;
+----+------------+------------------------+
| id | title      | imagesPath             |
+----+------------+------------------------+
|  1 | FirstImage | uploads/Dummyimage.jpg |
+----+------------+------------------------+
1 row in set (0.00 sec)