-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy patheditProduct.php
131 lines (103 loc) · 3.65 KB
/
editProduct.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
<!DOCTYPE html>
<html>
<head>
<title>My Shop | Edit Product</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.0-beta2/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-BmbxuPwQa2lc/FVzBcNJ7UAyJxM6wuqIj61tLrc4wSX0szH/Ev+nYRRuWlolflfl" crossorigin="anonymous">
</head>
<body>
<div class="container">
<?php include('nav.php');
require('checkloginstatus.php');
if ($_SESSION['role']=='customer') {
# code...
echo "You are customer cannot access this page";
header('location:index.php');
}
$productId = $_GET['id'];
//echo "$productId";
//select
$sql = "SELECT * FROM product WHERE id=$productId";
//connection
require('dbconnect.php');
//execute
$result = mysqli_query($conn,$sql);
if ($result) {
# code...
$productRecord = mysqli_fetch_assoc($result);
//echo "".$productRecord['name'];
}else{
echo "Something went wrong";
}
?>
<h1>Edit <?php echo($productRecord['name'])?></h1>
<div class="row">
<div class="col-4">
<img src="https://www.clipartmax.com/png/small/38-383442_shop-printed-revolution-online-and-earn-cash-add-product-icon-free.png" class="img-fluid">
</div>
<div class="col-6">
<form method="POST" action="">
<div class="mb-3">
<label for="exampleInputEmail1" class="form-label">Product Name</label>
<input type="text" class="form-control" name="product_name" value=<?php echo "".$productRecord['name'];?>>
</div>
<div class="mb-3">
<label for="exampleInputEmail1" class="form-label">Product Description</label>
<input type="text" class="form-control" name="product_desc" value=<?php echo($productRecord['description']);?>>
</div>
<input type="hidden" name="productId" value=<?php echo($productRecord['id']);?>>
<div class="mb-3">
<label for="exampleInputEmail1" class="form-label">Product Cost</label>
<input type="number" class="form-control" name="product_cost" value=<?php echo($productRecord['cost']);?>>
</div>
<button name="save" type="submit" class="btn btn-primary">UPDATE</button>
</form>
</div>
</div>
<?php
/*
1.connection to db - php and our db
2.Capture the data from
3.Insert -
sql query
*/
//require('dbconnect.php');
if (isset($_POST['save'])) {
# code...
$productName = $_POST['product_name'];
$productCost = $_POST['product_cost'];
$productDesc = $_POST['product_desc'];
$productId = $_POST['productId'];
//save above into database shop - tables - product
//INSERT query Values ???
$sql = "UPDATE product SET name=?,description=?,cost=? WHERE id=$productId";
//prepare statement - check if the above insert is correct or not
if ($stmt = mysqli_prepare($conn,$sql)) {
# code...
//bind the paramers - ? ?? -
//- insert data type - varchar -s , int - i double d
mysqli_stmt_bind_param($stmt,"ssd",$param_name,$param_desc,$param_cost);
//bind
$param_name = $productName;
$param_cost = $productCost;
$param_desc = $productDesc;
//execute the command - sql query - insert into db
if (mysqli_stmt_execute($stmt)) {
# code...
echo "Product updated successfully in the database";
//redirect go to my products
header("location:showproduct.php");
}else{
echo "Something went wrong.Try again.".mysqli_error($conn);
}
//close the stm
mysqli_stmt_close($stmt);
}else{
echo "Error in the query";
}
//close connection.
mysqli_close($conn);
}
?>
</div>
</body>
</html>