Skip to content

Commit a48ae03

Browse files
authored
Add files via upload
1 parent 347a8ee commit a48ae03

18 files changed

+681
-0
lines changed

JS/Delete.js

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
function confirmDelete(id) {
2+
var confirmation = confirm("Are you sure you want to delete this item?");
3+
if (confirmation) {
4+
window.location.href = '../Model/Delete.php?id=' + id;
5+
}
6+
}

JS/Delete_image.js

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
function confirmDelete(id) {
2+
var confirmation = confirm("Are you sure you want to delete this item?");
3+
if (confirmation) {
4+
window.location.href = '../Model/Delete_image.php?id=' + id;
5+
}
6+
}

JS/Delete_image2.js

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
function confirmDelete(id) {
2+
var confirmation = confirm("Are you sure you want to delete this item?");
3+
if (confirmation) {
4+
window.location.href = '../Model/Delete_image2.php?id=' + id;
5+
}
6+
}

JS/Main_Preview.js

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
document.getElementById('fileToUpload').addEventListener('change', function (event) {
2+
var preview = document.getElementById('preview');
3+
preview.src = URL.createObjectURL(event.target.files[0]);
4+
});

JS/Many_Preview.js

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
document.getElementById('product_images').addEventListener('change', handleFileSelect);
2+
3+
function handleFileSelect(event) {
4+
const previewContainer = document.getElementById('image-preview-container');
5+
previewContainer.innerHTML = '';
6+
7+
const files = event.target.files;
8+
9+
for (const file of files) {
10+
const reader = new FileReader();
11+
12+
reader.onload = function (e) {
13+
const imagePreview = document.createElement('div');
14+
imagePreview.classList.add('col-md-3');
15+
16+
const image = document.createElement('img');
17+
image.src = e.target.result;
18+
image.classList.add('img-fluid'); // Bootstrap class for responsive images
19+
20+
imagePreview.appendChild(image);
21+
previewContainer.appendChild(imagePreview);
22+
};
23+
24+
reader.readAsDataURL(file);
25+
}
26+
}

JS/Summer.js

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
2+
$(document).ready(function() {
3+
$('#summernote').summernote({
4+
height: 150,
5+
toolbar: [
6+
['style', ['bold', 'italic', 'underline', 'clear']],
7+
['font', ['strikethrough', 'superscript', 'subscript']],
8+
['para', ['ul', 'ol']],
9+
['insert', ['link']],
10+
['view', ['fullscreen', 'codeview']],
11+
],
12+
});
13+
});
14+

JS/jquery.js

+2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Model/Activate.php

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
include_once('Connection.php');
3+
4+
if (isset($_GET['id'])){
5+
6+
$id = $_GET['id'];
7+
$status = "0";
8+
$current_date = date('Y-m-d H:i:sa');
9+
10+
$sql = "UPDATE products SET status='$status', updated_at='$current_date' WHERE id='$id'";
11+
$affected_row = $conn->exec($sql);
12+
13+
}
14+
15+
header('location: ../View/index.php');
16+
?>

Model/Connection.php

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
$db_name = "";
4+
$username = "root";
5+
$password = "";
6+
7+
try {
8+
$conn = new PDO($db_name, $username, $password);
9+
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
10+
// echo"Database is connected successfully";
11+
} catch (PDOException $error) {
12+
echo"Database is not Connected" . $error->getMessage();
13+
}
14+
15+
?>

Model/Deactivate.php

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
include_once('Connection.php');
3+
4+
if (isset($_GET['id'])){
5+
6+
$id = $_GET['id'];
7+
$status = "1";
8+
$current_date = date('Y-m-d H:i:sa');
9+
10+
$sql = "UPDATE products SET status='$status', updated_at='$current_date' WHERE id='$id'";
11+
$affected_row = $conn->exec($sql);
12+
13+
}
14+
15+
header('location: ../View/index.php');
16+
?>

Model/Delete.php

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
include_once('Connection.php');
3+
4+
if(isset($_GET['id'])){
5+
$id = $_GET['id'];
6+
// echo $productId;
7+
8+
$stmt = $conn->prepare("DELETE FROM product_images WHERE product_id = ?");
9+
$stmt->execute([$id]);
10+
11+
$stmt = $conn->prepare("DELETE FROM products WHERE id = ?");
12+
$stmt->execute([$id]);
13+
14+
echo "Product deleted successfully.";
15+
} else {
16+
echo "Invalid request.";
17+
}
18+
$conn = null;
19+
header('location: ../View/index.php');
20+
?>

Model/Delete_image.php

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
include_once('Connection.php');
3+
4+
$id = $_GET['id'];
5+
6+
$stmt = $conn->prepare("SELECT * FROM product_images WHERE id='$id'");
7+
$stmt->execute();
8+
$images = $stmt->fetch(PDO::FETCH_ASSOC);
9+
10+
$pid = $images['product_id'];
11+
12+
if(isset($_GET['id'])){
13+
14+
$sql = "DELETE FROM product_images WHERE id='$id'";
15+
$conn->exec($sql);
16+
}
17+
// $conn = null;
18+
header("location: ../View/ProductView.php?id=$pid");
19+
20+
?>

Model/Delete_image2.php

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
include_once('Connection.php');
3+
4+
$id = $_GET['id'];
5+
6+
$stmt = $conn->prepare("SELECT * FROM product_images WHERE id='$id'");
7+
$stmt->execute();
8+
$images = $stmt->fetch(PDO::FETCH_ASSOC);
9+
10+
$pid = $images['product_id'];
11+
12+
if(isset($_GET['id'])){
13+
14+
$sql = "DELETE FROM product_images WHERE id='$id'";
15+
$conn->exec($sql);
16+
}
17+
// $conn = null;
18+
header("location: ../Model/Update.php?id=$pid");
19+
20+
?>

Model/InsertData.php

+139
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
<?php
2+
include_once('Connection.php');
3+
4+
if(isset($_REQUEST['submit'])){
5+
$title = $_REQUEST['title'];
6+
$price = $_REQUEST['price'];
7+
$discount_price = $_REQUEST['discount_price'];
8+
$short_desc = $_REQUEST['short_desc'];
9+
$long_desc = $_REQUEST['long_desc'];
10+
$file_name = $_FILES['fileToUpload']['name']; // get name
11+
move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], 'file/'.$file_name);
12+
13+
if (isset($_REQUEST['true'])) {
14+
$status = "1";
15+
}else {
16+
$status = "0";
17+
}
18+
19+
$sql = "INSERT INTO products (title,price,discount_price,short_desc,long_desc,main_image,status) VALUES ('$title','$price','$discount_price','$short_desc','$long_desc','$file_name','$status')";
20+
$affected_row = $conn->exec($sql);
21+
22+
// Calculate the remaining upload
23+
$totalImages = count($_FILES['product_images']['tmp_name']);
24+
25+
// $remaining_uploads = $upload_limit - $num_uploaded_images;
26+
if ($totalImages > 8) {
27+
echo "You can't upload more than 10 images.";
28+
exit;
29+
}
30+
31+
$productId = $conn->lastInsertId();
32+
echo $productId;
33+
$uploadDir = '../Model/uploads/';
34+
foreach ($_FILES['product_images']['tmp_name'] as $key => $tmp_name) {
35+
$uploadFile = $uploadDir . basename($_FILES['product_images']['name'][$key]);
36+
move_uploaded_file($tmp_name, $uploadFile);
37+
$stmt = $conn->prepare("INSERT INTO product_images (product_id, image_path) VALUES (?, ?)");
38+
$stmt->execute([$productId, $uploadFile]);
39+
}
40+
echo '<script>alert("Your Prodcut add is successufully !!!")</script>';
41+
42+
header('location: ../View/index.php');
43+
}
44+
?>
45+
<!DOCTYPE html>
46+
<html lang="en">
47+
<head>
48+
<meta charset="UTF-8">
49+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
50+
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet">
51+
<!-- jQuery -->
52+
<script src="https://code.jquery.com/jquery-3.5.1.min.js"></script>
53+
<!-- Summernote CSS and JS -->
54+
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/summernote/0.8.18/summernote-bs4.min.css">
55+
<script src="https://cdnjs.cloudflare.com/ajax/libs/summernote/0.8.18/summernote-bs4.min.js"></script>
56+
<title>Insert data </title>
57+
<style>
58+
.h {
59+
margin-top: 20px;
60+
margin-left: 310px;
61+
}
62+
63+
</style>
64+
</head>
65+
<body>
66+
<div class="container">
67+
<h2><a href="../View/index.php">Home Page</a></h2>
68+
<div class="row">
69+
<div class="col-sm-6 border h p-4 ">
70+
<form action="" method="POST" enctype="multipart/form-data">
71+
<h3 class="text-center mt-3">Product</h3>
72+
<div class="row mt-4">
73+
<div class="form-group col">
74+
<label for="title">Product Title</label>
75+
<input type="text" class="form-control" name="title" id="title" placeholder="Enter Product Tilte">
76+
</div>
77+
</div>
78+
<div class="row mt-3">
79+
<div class="form-group col">
80+
<label for="price">Product Price</label>
81+
<input type="number" class="form-control" name="price" id="price" placeholder="Enter the product Price">
82+
</div>
83+
<div class="form-group col">
84+
<label for="discount_price">Discount</label>
85+
<input type="number" class="form-control" name="discount_price" id="discount_price" placeholder="Enter discount on prodcut ">
86+
</div>
87+
</div>
88+
<div class="row mt-3">
89+
<div class="form-group col">
90+
<label for="short_desc">Short Description</label>
91+
<textarea class="form-control" name="short_desc" id="short_desc"cols="30" rows="5"></textarea>
92+
</div>
93+
</div>
94+
<div class="row mt-3">
95+
<div class="form-group col">
96+
<label for="long_desc">Long Description</label>
97+
<textarea id="summernote" name="long_desc"></textarea>
98+
</div>
99+
</div>
100+
<div class="row mt-3">
101+
<div class="form-group col">
102+
<label for="fileToUpload">Main Image</label>
103+
<input class="form-control" type="file" name="fileToUpload" id="fileToUpload">
104+
</div>
105+
</div>
106+
<img class='card-img-top mt-2' id="preview" style='width:24%;'/>
107+
108+
<div class="row mt-3">
109+
<div class="form-group col">
110+
<label for="product_images">Multiple Images:</label>
111+
<input type="file" class="form-control" name="product_images[]" id="product_images" multiple accept="image/*" >
112+
</div>
113+
</div>
114+
<div class="row mt-5" id="image-preview-container"></div>
115+
116+
<div class="row mt-3">
117+
<div class="form-group col">
118+
<div class="form-check">
119+
<input class="form-check-input" type="checkbox" name="true" value="" id="status" />
120+
<label class="form-check-label" for="status">Status</label>
121+
</div>
122+
</div>
123+
</div>
124+
<div class="row">
125+
<div class="text-center mt-4 col">
126+
<button type="submit" class="btn btn-primary w-50" name="submit">Add Product</button>
127+
</div>
128+
</div>
129+
</form>
130+
</div>
131+
</div>
132+
</div>
133+
<script src="../JS/Summer.js"></script>
134+
<script src="../JS/Main_Preview.js"></script>
135+
<script src="../JS/Many_Preview.js"></script>
136+
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js"></script>
137+
138+
</body>
139+
</html>

Model/Serach.php

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
$search_value = $_POST["search"];
3+
// echo $search_value;
4+
include_once('Connection.php');
5+
6+
$stmt = $conn->prepare("SELECT * FROM products WHERE title LIKE '%{$search_value}%' OR id LIKE '%{$search_value}%' OR price LIKE '%{$search_value}%' ");
7+
$stmt->execute();
8+
$test = $stmt->fetchAll(PDO::FETCH_ASSOC);
9+
10+
if(count($test) > 0 ){
11+
foreach($test as $user) {
12+
$dt = ($user['status'] == 1) ? "<a href='../Model/Activate.php?id=".$user['id']."'><button type='button'
13+
class='btn btn-outline-success'>Enable</button></a>" : "<a href='../Model/Deactivate.php?id=".$user['id']."'><button type='button'
14+
class='btn btn-outline-danger'>Disable</button></a>" ;
15+
echo"<tr >";
16+
echo"<th>".$user['id']."</th>";
17+
echo"<td>".$user['title']."</td>";
18+
echo"<td>".$user['price']."</td>";
19+
echo"<td>".$dt."</td>";
20+
echo"<td>".'<img style="width:50px; height:50px" src="../Model/file/'.$user['main_image'].'"/>'."</td>";
21+
echo "<td><a href='ProductView.php?id=".$user['id']."' class='btn btn-secondary btn-sm'>View</a></td>";
22+
echo "<td><a href='../Model/Update.php?id=".$user['id']."' class='btn btn-success btn-sm'>Edit</a></td>";
23+
echo "<td><button onclick='confirmDelete(\"{$user['id']}\")' class='btn btn-danger btn-sm'>Delete</button></td>";
24+
echo"</tr>";
25+
}
26+
}else{
27+
echo "<td colspan='8'>No Record Found.</td>";
28+
}
29+
30+
?>

0 commit comments

Comments
 (0)