|
| 1 | +<?php |
| 2 | +session_start(); |
| 3 | +require_once '../config/connect.php'; |
| 4 | +if (!isset($_SESSION['email']) & empty($_SESSION['email'])) { |
| 5 | + header('location: login.php'); |
| 6 | +} |
| 7 | + |
| 8 | +if (isset($_POST) & !empty($_POST)) { |
| 9 | + $prodname = mysqli_real_escape_string($connection, $_POST['productname']); |
| 10 | + $description = mysqli_real_escape_string($connection, $_POST['productdescription']); |
| 11 | + $category = mysqli_real_escape_string($connection, $_POST['productcategory']); |
| 12 | + $price = mysqli_real_escape_string($connection, $_POST['productprice']); |
| 13 | + |
| 14 | + |
| 15 | + if (isset($_FILES) & !empty($_FILES)) { |
| 16 | + $name = $_FILES['productimage']['name']; |
| 17 | + $size = $_FILES['productimage']['size']; |
| 18 | + $type = $_FILES['productimage']['type']; |
| 19 | + $tmp_name = $_FILES['productimage']['tmp_name']; |
| 20 | + |
| 21 | + $max_size = 10000000; |
| 22 | + $extension = substr($name, strpos($name, '.') + 1); |
| 23 | + |
| 24 | + if (isset($name) && !empty($name)) { |
| 25 | + if (($extension == "jpg" || $extension == "jpeg") && $type == "image/jpeg" && $size<=$max_size) { |
| 26 | + $location = "uploads/"; |
| 27 | + if (move_uploaded_file($tmp_name, $location.$name)) { |
| 28 | + //$smsg = "Uploaded Successfully"; |
| 29 | + $sql = "INSERT INTO products (name, description, catid, price, thumb) VALUES ('$prodname', '$description', '$category', '$price', '$location$name')"; |
| 30 | + $res = mysqli_query($connection, $sql); |
| 31 | + if ($res) { |
| 32 | + //echo "Product Created"; |
| 33 | + header('location: products.php'); |
| 34 | + } else { |
| 35 | + $fmsg = "Failed to Create Product"; |
| 36 | + } |
| 37 | + } else { |
| 38 | + $fmsg = "Failed to Upload File"; |
| 39 | + } |
| 40 | + } else { |
| 41 | + $fmsg = "Only JPG files are allowed and should be less that 1MB"; |
| 42 | + } |
| 43 | + } else { |
| 44 | + $fmsg = "Please Select a File"; |
| 45 | + } |
| 46 | + } else { |
| 47 | + $sql = "INSERT INTO products (name, description, catid, price) VALUES ('$prodname', '$description', '$category', '$price')"; |
| 48 | + $res = mysqli_query($connection, $sql); |
| 49 | + if ($res) { |
| 50 | + header('location: products.php'); |
| 51 | + } else { |
| 52 | + $fmsg = "Failed to Create Product"; |
| 53 | + } |
| 54 | + } |
| 55 | +} |
| 56 | +?> |
| 57 | +<?php include 'inc/header.php'; ?> |
| 58 | +<?php include 'inc/nav.php'; ?> |
| 59 | + |
| 60 | +<section id="content"> |
| 61 | + <div class="content-blog"> |
| 62 | + <div class="container"> |
| 63 | + <?php if (isset($fmsg)) { |
| 64 | + ?><div class="alert alert-danger" role="alert"> <?php echo $fmsg; ?> </div><?php |
| 65 | +} ?> |
| 66 | + <?php if (isset($smsg)) { |
| 67 | + ?><div class="alert alert-success" role="alert"> <?php echo $smsg; ?> </div><?php |
| 68 | + } ?> |
| 69 | + <form method="post" enctype="multipart/form-data"> |
| 70 | + <div class="form-group"> |
| 71 | + <label for="Productname">Product Name</label> |
| 72 | + <input type="text" class="form-control" name="productname" id="Productname" placeholder="Product Name"> |
| 73 | + </div> |
| 74 | + <div class="form-group"> |
| 75 | + <label for="productdescription">Product Description</label> |
| 76 | + <textarea class="form-control" name="productdescription" rows="3"></textarea> |
| 77 | + </div> |
| 78 | + |
| 79 | + <div class="form-group"> |
| 80 | + <label for="productcategory">Product Category</label> |
| 81 | + <select class="form-control" id="productcategory" name="productcategory"> |
| 82 | + <option value="">---SELECT CATEGORY---</option> |
| 83 | + <?php |
| 84 | + $sql = "SELECT * FROM category"; |
| 85 | + $res = mysqli_query($connection, $sql); |
| 86 | + while ($r = mysqli_fetch_assoc($res)) { |
| 87 | + ?> |
| 88 | + <option value="<?php echo $r['id']; ?>"><?php echo $r['name']; ?></option> |
| 89 | + <?php |
| 90 | + } ?> |
| 91 | + </select> |
| 92 | + </div> |
| 93 | + |
| 94 | + <div class="form-group"> |
| 95 | + <label for="productprice">Product Price</label> |
| 96 | + <input type="text" class="form-control" name="productprice" id="productprice" placeholder="Product Price"> |
| 97 | + </div> |
| 98 | + <div class="form-group"> |
| 99 | + <label for="productimage">Product Image</label> |
| 100 | + <input type="file" name="productimage" id="productimage"> |
| 101 | + <p class="help-block">Only jpg/png are allowed.</p> |
| 102 | + </div> |
| 103 | + |
| 104 | + <button type="submit" class="btn btn-default">Submit</button> |
| 105 | + </form> |
| 106 | + |
| 107 | + </div> |
| 108 | + </div> |
| 109 | +</section> |
| 110 | + |
| 111 | +<?php include 'inc/footer.php' ?> |
0 commit comments