-
Notifications
You must be signed in to change notification settings - Fork 0
/
addcart.php
50 lines (41 loc) · 1.14 KB
/
addcart.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
<?php
// connect to database
include 'partials/dbconnect.php';
// check if user is logged in
session_start();
if (!isset($_SESSION['uid'])) {
echo "<script>
alert('Login to add products to cart');
window.location.href='index.php';
</script>";
}
// retrieve product ID from query string
$pid = $_GET['p_id'];
// insert the product into the shopping basket for the current user
$sql = "INSERT IGNORE INTO shopping_basket (p_id, u_id) VALUES ($pid, {$_SESSION['uid']})";
$result = mysqli_query($conn, $sql);
// check if the query was successful
if ($result) {
if (mysqli_affected_rows($conn) > 0) {
// show a success message
$message = "Product added to shopping basket";
echo "<script>
alert('$message');
window.location.href='shopping_cart.php';
</script>";
} else {
// show an error message
$message = "Product is already in the shopping basket";
echo "<script>
alert('$message');
window.location.href='index.php';
</script>";
}
} else {
$message = "Error adding product to shopping basket";
echo "<script>
alert('$message');
window.location.href='shopping_cart.php';
</script>";
}
?>