-
Notifications
You must be signed in to change notification settings - Fork 1
/
product.php
148 lines (135 loc) · 5.71 KB
/
product.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
<?php
session_start();
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<?php include 'plugins.php';?>
<script src="js/sweetalert.min.js"></script>
<script>
$(document).ready(function()
{
var urlParams = new URLSearchParams(window.location.search);
$("#txtPID").html(urlParams.get('pid'));
});
function calculate()
{
var rate = $("#lblRate").html();
var quantity = $("#txtQuantity").val();
var total = rate * quantity;
$("#lblTotal").html(total);
$("#txtTotal").val(total);
}
</script>
</head>
<body>
<div class="container">
<!-- include 'header.php'; -->
<div id="main">
<br>
<?php
// if(isset($_GET['pid'])==false)
// {
// header("Location: index.php");
// }
$pid = $_GET['pid'];
$servername="localhost";
$username="root";
$password="";
$dbname="orms";
//create connection
$conn=mysqli_connect($servername,$username,$password,$dbname);
//checking connection
if(!$conn)
{
die("connection failed".mysqli_connect_error());
}
$pname="";
$rate="";
$pimage="";
$description="";
$sql="SELECT * FROM product where pid=?" or die($mysqli->error());
$stmt = $conn->stmt_init();
$stmt->prepare($sql);
$stmt->bind_param("i",$pid);
$stmt->execute();
$result = $stmt->get_result();
$resultArray = $result->fetch_assoc();
if($resultArray)
{
$pname=$resultArray['pname'];
$rate=$resultArray['rate'];
$pimage=$resultArray['pimage'];
$description=$resultArray['description'];
}
else
{
echo "<script>swal('no data found');</script>";
}
mysqli_close($conn);
?>
<div class="row pt-5">
<div class="col-sm-4">
<img src="https://placehold.it/150x80?text=IMAGE" class="img-responsive" style="width:100%" alt="Image">
</div>
<div class="col-sm-8">
<div id="description">
<p class="lead">
Description: <?php echo $description; ?>
</p>
</div>
<div id="totalamount">
<p class="small">
<form action="<?php $_SERVER['PHP_SELF']; ?>" method="POST">
<input type="hidden" name="txtPID" id="txtPID" value="<?php $_GET['pid']; ?>">
Price: <i class="fa fa-inr"></i> <label id="lblRate"><?php echo $rate;?></label> /-
<input type="hidden" name="txtrate" id="txtrate" value='<?php echo $rate;?>'>
<br>
Quantity: <input id="txtQuantity" name="txtQuantity" type="number" onkeyup="calculate();" onchange="calculate();" min="1" class="form-control w-25" value="1" name="txtqty" id="txtqty">
<br>
Total: <i class="fa fa-inr"></i><label id="lblTotal"><?php echo $rate;?></label>/-
<input type="hidden" name="txtTotal" id="txtTotal" <?php echo $rate;?>>
<br>
<input type="submit" class="btn btn-primary" value="Add to Cart">
</form>
</p>
<?php
if($_SERVER["REQUEST_METHOD"]=="POST")
{
$servername="localhost";
$username="root";
$password="";
$dbname="orms";
$conn=mysqli_connect($servername,$username,$password,$dbname);
if(!$conn){
die("connection failed:" . mysqli_connect_error());
}
$userID = 1; //$_SESSION['userID'];
$quantity=$_POST['txtQuantity'];
$total=$_POST['txtrate']*$quantity;
$sessionID = session_id();
$sql="INSERT into cart (userID, sessionID, pid, quantity, total) values ('$userID', '$sessionID', '$pid', '$quantity', '$total')";
if (mysqli_query($conn,$sql))
{
echo "<script>swal('Cart','Product added to cart','info').then(() => { window.location.href='index.php'; });</script>";
}
else
{
echo"Error:" .mysqli_error($conn);
}
mysqli_close($conn);
}
?>
</div>
</div>
</div>
</div>
<br>
<br>
</div>
</body>
</html>