rename image during upload not working with Mysql set
<p>I have the bellow code which I was hoping to change/rename image name on upload to user id so I can avoid file overwrite and insert the name into database sadly after I added rename code the code is not able to upload image or update the database we out showing any error but if I remove the rename code everything was working. 
Can one help me how to solve it or is there any better way I can do it?  </p>

<pre><code>&lt;?php
$user_id = htmlentities($_SESSION['user']['id'], ENT_QUOTES, 'UTF-8');
$username = htmlentities($_SESSION['user']['username'], ENT_QUOTES, 'UTF-8');
require("connection.php");
if(@$_POST ['submit']) {
    $file = $_FILES ['file'];
    $name1 = $file ['name'];
    $type = $file ['type'];
    $size = $file ['size'];
    $tmppath = $file ['tmp_name'];
    if($type == 'jpeg' || $type == 'png' || $type == 'jpg') {
        $name1 = $user_id.$type; // rename image
        if($name1!="") {
            if(move_uploaded_file ($tmppath, 'users/'.$name1)) {
                $sql=("INSERT INTO USERS set photo='$name1' WHERE username='$username'");
                mysql_query ($sql) or die ('could not updated:'.mysql_error());
                echo ("Profile picture updated");
            }
        }
    } 
}
?&gt;
</code></pre>
