<?php
$connect = mysqli_connect("localhost","root","","shopping");
?>
<!DOCTYPE html>
<html>
<head>
	<title>demo1</title>
	<link rel="stylesheet" type="text/css" href="css/login.css">
	<link rel="stylesheet" type="text/css" href="css/table.css">
	<style type="text/css">
		img{
			width: 50px;
			height: 50px;
		}
	</style>
</head>
<body>
	<div class="header">
		<h2>Add Image</h2>
	</div>
	<form method="post" enctype="multipart/form-data">
	<div class="input-group">
		<label>Name</label>
		<input type="text" name="pname" required><br>
	</div>
	<div class="input-group">
		<label>Select Photo</label>
		<input type="file" id="image" name="image" value="" multiple=""><br>
	</div>
	<center>
	<div class="input-group">
		<button class="btn" id="insert_image" type="submit" name="submit_button">Submit</button>
	</div>
	<?php
	$sql1="SELECT name,image_dir FROM test";
	$result = $connect-> query($sql1);
	$uploads_dir = 'uploads/demo';

	if ($result -> num_rows > 0) {
		while ($rows = $result-> fetch_assoc()) { ?>
			<table>
			<tr>
				<td><?php echo $rows['name']; ?></td>
				<?php
                if(!empty($rows['image_dir'])){?>
				<td><img src="<?php echo $rows['image_dir']; ?>" /></td><?php } ?>
			</tr>
		</table>
	<?php   
		}
	}
	else{ ?>
		<h1 align="center">0 result found :(</h1>
	<?php
	}
	?>
	</center>
	</form>
	<?php
	if($_SERVER['REQUEST_METHOD'] == 'POST')
	{
		$pname   = isset($_POST['pname'])?strtoupper($_POST['pname']):'';
		$image   = isset($_POST['image'])?$_POST['image']:'';
	$uploads_dir = 'uploads/demo';
    $img_path = 'http://localhost/folder/uploads/demo/';
    $tmp_name = $_FILES["image"]["tmp_name"];
    $name = basename($_FILES["image"]["name"]);
    move_uploaded_file($tmp_name, "$uploads_dir/$name");
    $p_img_url =  $img_path.$name;
                         // $img_url = $uploads_dir.$name;    
                         if(!empty($name))
                        {
                             $p_img_url = $img_path.$name;
                        }
                        else
                        {
                            $p_img_url = '';
                        }
                // Insert Student Details into database
                  $sql = "INSERT INTO test(name,image_dir) VALUES ('$pname','$p_img_url');";
if (mysqli_query($connect,$sql)) 
	{
		echo '<script>alert("Image Inserted sucessfully");</script>';	
	}
}
?>
</body>
</html>