PHP: Uploading Image with autocode

PHP Code

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<link rel="stylesheet" type="text/css" href="bootstrap-3.3.5-dist/css/bootstrap.min.css"/>
</head>

<body>
<?php
function autocode()
{
$host = 'localhost';
$user = 'root';
$pass = '';
$dbname = 'bhabha';
    	$db = new PDO("mysql:host=$host;dbname=$dbname", $user, $pass);
        $max = "SELECT MAX(eid) as max FROM student";
        $result = $db->query($max);

        $result->setFetchMode(PDO::FETCH_ASSOC);
        $row = $result->fetch();

        if (substr($row["max"], 4, 4) < 1001) {
            $eid = "SOFT1001";
        } else {
            $emax = substr($row["max"], 4, 4) + 1;
            $eid = "SOFT" . $emax;
        }
		$GLOBALS['newcode']=$eid;
}

if(isset($_POST["upload"]))
{
$host = 'localhost';
$user = 'root';
$pass = '';
$dbname = 'bhabha';

$name=$_POST['name'];
$allowedExts = array("gif", "jpeg", "jpg", "png");
$extension = end(explode(".", $_FILES["file"]["name"]));
if ($_FILES["file"]["type"] == "image/gif"
|| $_FILES["file"]["type"] == "image/jpeg"
|| $_FILES["file"]["type"] == "image/jpg"
|| $_FILES["file"]["type"] == "image/png"
&& in_array($extension, $allowedExts))
{
if ($_FILES["file"]["size"] < 50000)
 
{
move_uploaded_file($_FILES["file"]["tmp_name"], "img/".$_FILES["file"]["name"]);
$newimg="img/".$_FILES["file"]["name"];



$db = new PDO("mysql:host=$host;dbname=$dbname", $user, $pass);
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
autocode();
$sql = "insert into student (eid,ename,image_path) values('$newcode','$name','$newimg')";

if ($db->query($sql)) {
echo "<script type= 'text/javascript'>alert('Image Uploaded Successfully');</script>";
} else {
echo "<script type= 'text/javascript'>alert('Data not successfully updated.');</script>";
}
$db = null;

}
else
{
echo "<script type='text/javascript'> alert('invalid file!! maximun Size 50kb only ')

</script>";
}

}

else
{
echo "<script type='text/javascript'> alert('invalid file!! Image should be jpg, jpeg, gif, png extension only ')

</script>";
}

}
?>
<div style="width:60%; margin:auto">
<div class="panel panel-info">
    <div class="panel-heading"> <span  style="color:black; font-weight:bold">visit us </span>codetpoint.com | 
    <span style="color:#09F">sssoftaid.com <span></div>
    <div class="panel-body">
    

                           <form action="photo.php" method="post" enctype="multipart/form-data">
 								<label >Name</label>
                                <input type="text" name="name"  class="form-control" />
                               
                                <label >Image Upload</label>
                                <input type="file" name="file"  class="form-control" />
                                           
        	                    			<br />
                                <button type="submit"  name="upload" class="btn btn-info" >Upload</button>
                                
		                     </form>
        
    
    </div>
</div>

</div>
                 

</body>
</html>

MySQL Database

12

Run the  PHP Script

 111