<?php
include 'dbconnect.php';

if(isset($_GET['type']) && $_GET['type']!=''){
    $type = $_GET['type'];

    if($type=='delete'){ 
        $id = $_GET['id'];
        $delete_sql = "delete from user_register where id='$id'";
        mysqli_query($con,$delete_sql);
    }
}

if(isset($_POST['submit'])){
  $image = rand(11111111,99999999).'_'.$_FILES['image']['name'];
  $target_img = "upload/".$image;
  move_uploaded_file($_FILES['image']['tmp_name'],$target_img);
  
  $sql = "insert into user_register(image) values('$image')";
  $res = mysqli_query($con,$sql);
}

?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="stylesheet" href="css/style.css">
    <link rel="stylesheet" href="css/bootstrap.css">
    <title>Document</title>
</head>
<body>
<?php require "navbar.php"; ?>
<div class="container">
  <div class="container my-4">
    <h4>Gallery Form</h4>
    <form method="post" enctype="multipart/form-data">
        <div class="form-group">
            <label>Image</label>
            <input type="file" name="image" class="form-control">
        </div>
        <div class="form-group">
            <button class="btn btn-primary" name="submit">Submit</button>
        </div>
    </form>
</div>
<div class="container-fluid my-3">
    <table class="table table-bordered">
        <thead>
            <tr>
                <th>Image</th>
                <th></th>
            </tr>
        </thead>
        <tbody>
            <?php
                $sql = "select * from user_register order by id desc";
                $result = mysqli_query($con,$sql);
                while($row1=mysqli_fetch_assoc($result)){
            ?>
            <tr>
                <td><img src="upload/<?php echo $row1['image']?>" height="60"></td>
                <td class="text-right">
                    <?php 
                    echo "<a class='btn btn-danger' href='?type=delete&id=".$row1['id']."'>Delete</a>";
                    ?>
                </td>
            </tr>
            <?php } ?>
        </tbody>
    </table>
</div>
</body>
</html>