├── db.PNG ├── homepage.PNG ├── editprofile.PNG ├── bookstore ├── connectDB.php ├── image │ ├── bg.gif │ ├── food.jpg │ ├── logo.gif │ ├── logo.png │ ├── travel.jpg │ ├── technical.jpg │ ├── technology.jpg │ └── loading.svg ├── logout.php ├── checklogin.php ├── login.php ├── database.sql ├── style.css ├── index.php ├── register.php ├── edituser.php └── checkout.php └── README.md /db.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ywxbear/PHP-Bookstore-Website-Example/HEAD/db.PNG -------------------------------------------------------------------------------- /homepage.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ywxbear/PHP-Bookstore-Website-Example/HEAD/homepage.PNG -------------------------------------------------------------------------------- /editprofile.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ywxbear/PHP-Bookstore-Website-Example/HEAD/editprofile.PNG -------------------------------------------------------------------------------- /bookstore/connectDB.php: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /bookstore/image/bg.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ywxbear/PHP-Bookstore-Website-Example/HEAD/bookstore/image/bg.gif -------------------------------------------------------------------------------- /bookstore/logout.php: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /bookstore/image/food.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ywxbear/PHP-Bookstore-Website-Example/HEAD/bookstore/image/food.jpg -------------------------------------------------------------------------------- /bookstore/image/logo.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ywxbear/PHP-Bookstore-Website-Example/HEAD/bookstore/image/logo.gif -------------------------------------------------------------------------------- /bookstore/image/logo.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ywxbear/PHP-Bookstore-Website-Example/HEAD/bookstore/image/logo.png -------------------------------------------------------------------------------- /bookstore/image/travel.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ywxbear/PHP-Bookstore-Website-Example/HEAD/bookstore/image/travel.jpg -------------------------------------------------------------------------------- /bookstore/image/technical.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ywxbear/PHP-Bookstore-Website-Example/HEAD/bookstore/image/technical.jpg -------------------------------------------------------------------------------- /bookstore/image/technology.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ywxbear/PHP-Bookstore-Website-Example/HEAD/bookstore/image/technology.jpg -------------------------------------------------------------------------------- /bookstore/image/loading.svg: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # PHP Basic BookStore Website (For Study Purpose Only) 2 | This BookStore Website is using PHP and Database(MySQL). In this website you can Register and Edit Profile. 3 | And also all the book data will store at the database for easy to add, edit and delete. 4 | 5 | ## Home Page & Edit Profile Page: 6 | ![HomePage](/homepage.PNG) 7 | ![EditProfile](/editprofile.PNG) 8 | 9 | ## DataBase: 10 | ![Database](/db.PNG) 11 | 12 | ## How to run: 13 | Download [bookstore](https://github.com/weixiong15/PHP_Basic_BookStore_Website/tree/master/bookstore) folder and upload these file to your server or you can download an application called 14 | [XAMPP](https://www.apachefriends.org/index.html) or other. After, you need to import [database.sql](https://github.com/weixiong15/PHP_Basic_BookStore_Website/blob/master/bookstore/database.sql) to your server/XAMPP 15 | first. 16 | 17 | -------------------------------------------------------------------------------- /bookstore/checklogin.php: -------------------------------------------------------------------------------- 1 | prepare($sql); 11 | $stmt->execute(array( 12 | ':username' => $username, 13 | ':pwd' => $pwd 14 | )); 15 | 16 | if($stmt->rowCount()>0){ 17 | while ( $row = $stmt->fetch(PDO::FETCH_ASSOC) ) { 18 | $_SESSION['id']=$row['UserID']; 19 | } 20 | 21 | header("Location:index.php"); 22 | 23 | }else{ 24 | echo 'Login Fail'; 25 | header("Location:login.php?errcode=1"); 26 | } 27 | 28 | } 29 | ?> -------------------------------------------------------------------------------- /bookstore/login.php: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 |
5 |
6 | 7 |
8 |
9 |
10 |
11 |

Login

12 |
13 | Username:
14 |

15 | Password:
16 |

17 | 18 | 19 |
20 |
21 |
22 | 'Invalid username or password. Please try again.', 26 | 2 => 'Please login.' 27 | ]; 28 | 29 | $errcode = intval($_GET['errcode']); 30 | if (array_key_exists($errcode, $errorMessages)) { 31 | echo '' . htmlspecialchars($errorMessages[$errcode]) . ''; 32 | } 33 | } 34 | ?> 35 | 36 | 37 | -------------------------------------------------------------------------------- /bookstore/database.sql: -------------------------------------------------------------------------------- 1 | CREATE DATABASE BookStore; 2 | USE BookStore; 3 | 4 | CREATE TABLE Book( 5 | BookID varchar(50), 6 | BookTitle varchar(200), 7 | ISBN varchar(20), 8 | Price double(12,2), 9 | Author varchar(128), 10 | Type varchar(128), 11 | Image varchar(128), 12 | PRIMARY KEY (BookID) 13 | ); 14 | 15 | CREATE TABLE Users( 16 | UserID int not null AUTO_INCREMENT, 17 | UserName varchar(128), 18 | Password varchar(16), 19 | PRIMARY KEY (UserID) 20 | ); 21 | 22 | CREATE TABLE Customer ( 23 | CustomerID int not null AUTO_INCREMENT, 24 | CustomerName varchar(128), 25 | CustomerPhone varchar(12), 26 | CustomerIC varchar(14), 27 | CustomerEmail varchar(200), 28 | CustomerAddress varchar(200), 29 | CustomerGender varchar(10), 30 | UserID int, 31 | PRIMARY KEY (CustomerID), 32 | CONSTRAINT FOREIGN KEY (UserID) REFERENCES Users(UserID) ON DELETE SET NULL ON UPDATE CASCADE 33 | ); 34 | 35 | CREATE TABLE `Order`( 36 | OrderID int not null AUTO_INCREMENT, 37 | CustomerID int, 38 | BookID varchar(50), 39 | DatePurchase datetime, 40 | Quantity int, 41 | TotalPrice double(12,2), 42 | Status varchar(1), 43 | PRIMARY KEY (OrderID), 44 | CONSTRAINT FOREIGN KEY (BookID) REFERENCES Book(BookID) ON DELETE SET NULL ON UPDATE CASCADE, 45 | CONSTRAINT FOREIGN KEY (CustomerID) REFERENCES Customer(CustomerID) ON DELETE SET NULL ON UPDATE CASCADE 46 | ); 47 | 48 | CREATE TABLE Cart( 49 | CartID int not null AUTO_INCREMENT, 50 | CustomerID int, 51 | BookID varchar(50), 52 | Price double(12,2), 53 | Quantity int, 54 | TotalPrice double(12,2), 55 | PRIMARY KEY (CartID), 56 | CONSTRAINT FOREIGN KEY (BookID) REFERENCES Book(BookID) ON DELETE SET NULL ON UPDATE CASCADE, 57 | CONSTRAINT FOREIGN KEY (CustomerID) REFERENCES Customer(CustomerID) ON DELETE SET NULL ON UPDATE CASCADE 58 | ); 59 | 60 | 61 | INSERT INTO `book`(`BookID`, `BookTitle`, `ISBN`, `Price`, `Author`, `Type`, `Image`) VALUES ('B-001','Lonely Planet Australia (Travel Guide)','123-456-789-1',136,'Lonely Planet','Travel','image/travel.jpg'); 62 | INSERT INTO `book`(`BookID`, `BookTitle`, `ISBN`, `Price`, `Author`, `Type`, `Image`) VALUES ('B-002','Crew Resource Management, Second Edition','123-456-789-2',599,'Barbara Kanki','Technical','image/technical.jpg'); 63 | INSERT INTO `book`(`BookID`, `BookTitle`, `ISBN`, `Price`, `Author`, `Type`, `Image`) VALUES ('B-003','CCNA Routing and Switching 200-125 Official Cert Guide Library','123-456-789-3',329,'Cisco Press ','Technology','image/technology.jpg'); 64 | INSERT INTO `book`(`BookID`, `BookTitle`, `ISBN`, `Price`, `Author`, `Type`, `Image`) VALUES ('B-004','Easy Vegetarian Slow Cooker Cookbook','123-456-789-4',75.9,'Rockridge Press','Food','image/food.jpg'); -------------------------------------------------------------------------------- /bookstore/style.css: -------------------------------------------------------------------------------- 1 | body{ 2 | font-family: Arial; 3 | margin: 0 auto; 4 | } 5 | 6 | header { 7 | background-color: rgb(0,51,102); 8 | width: 100%; 9 | } 10 | header img { 11 | margin: 1%; 12 | } 13 | header .hf{ 14 | float: right; 15 | margin: 1.5%; 16 | } 17 | header .hi{ 18 | background-color: #fff; 19 | border: none; 20 | border-radius: 20px; 21 | text-align: center; 22 | transition-duration: 0.5s; 23 | padding: 8px 30px; 24 | cursor: pointer; 25 | color: #000; 26 | font-weight: bold; 27 | margin-top: 15%; 28 | } 29 | header .hi:hover{ 30 | background-color: #ccc; 31 | } 32 | 33 | 34 | table { 35 | border-collapse: collapse; 36 | } 37 | tr{background-color: #fff;} 38 | th { 39 | padding-top: 12px; 40 | padding-bottom: 12px; 41 | text-align: left; 42 | background-color: rgb(0,51,102); 43 | color: white; 44 | } 45 | table .btn{ 46 | background-color: #ec7115; 47 | border: none; 48 | text-align: center; 49 | transition-duration: 0.5s; 50 | padding: 8px 30px; 51 | cursor: pointer; 52 | color: #fff; 53 | margin-top: 5%; 54 | } 55 | table .btn:hover{ 56 | background-color: #e3e3e3; 57 | color: #ec7115; 58 | } 59 | 60 | .button{ 61 | background-color: rgb(0,51,102); 62 | border: none; 63 | border-radius: 20px; 64 | text-align: center; 65 | transition-duration: 0.5s; 66 | padding: 8px 30px; 67 | cursor: pointer; 68 | color: #fff; 69 | margin-top: 5%; 70 | font-weight: bold; 71 | } 72 | .button:hover { 73 | background-color: rgb(102,255,255); 74 | color: #000; 75 | } 76 | .cbtn{ 77 | background-color: #fff; 78 | border: none; 79 | border-radius: 20px; 80 | text-align: center; 81 | transition-duration: 0.5s; 82 | padding: 8px 30px; 83 | cursor: pointer; 84 | color: #000; 85 | font-weight: bold; 86 | } 87 | .cbtn:hover{ 88 | background-color: #ccc; 89 | } 90 | 91 | form{ 92 | margin-top: 2%; 93 | } 94 | input[type=text], input[type=password]{ 95 | width: 100%; 96 | padding: 12px; 97 | border-radius: 3px; 98 | box-sizing: border-box; 99 | border: 2px solid #ccc; 100 | transition: 0.5s; 101 | outline: none; 102 | } 103 | 104 | input[type=text]:focus, input[type=password]:focus { 105 | border: 2px solid rgb(0,51,102); 106 | } 107 | textarea { 108 | outline: none; 109 | border: 2px solid #ccc; 110 | } 111 | textarea:focus { 112 | border: 2px solid rgb(0,51,102); 113 | } 114 | 115 | .container { 116 | width: 55%; 117 | border-radius: 5px; 118 | background-color: #f2f2f2; 119 | padding: 20px; 120 | margin: 0 auto; 121 | } -------------------------------------------------------------------------------- /bookstore/index.php: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | connect_error) { 16 | die("Connection failed: " . $conn->connect_error); 17 | } 18 | 19 | $sql = "USE bookstore"; 20 | $conn->query($sql); 21 | 22 | $sql = "SELECT * FROM book WHERE BookID = '".$_POST['ac']."'"; 23 | $result = $conn->query($sql); 24 | 25 | while($row = $result->fetch_assoc()){ 26 | $bookID = $row['BookID']; 27 | $quantity = $_POST['quantity']; 28 | $price = $row['Price']; 29 | } 30 | 31 | $sql = "INSERT INTO cart(BookID, Quantity, Price, TotalPrice) VALUES('".$bookID."', ".$quantity.", ".$price.", Price * Quantity)"; 32 | $conn->query($sql); 33 | } 34 | 35 | if(isset($_POST['delc'])){ 36 | $servername = "localhost"; 37 | $username = "root"; 38 | $password = ""; 39 | 40 | $conn = new mysqli($servername, $username, $password); 41 | 42 | if ($conn->connect_error) { 43 | die("Connection failed: " . $conn->connect_error); 44 | } 45 | 46 | $sql = "USE bookstore"; 47 | $conn->query($sql); 48 | 49 | $sql = "DELETE FROM cart"; 50 | $conn->query($sql); 51 | } 52 | 53 | $servername = "localhost"; 54 | $username = "root"; 55 | $password = ""; 56 | 57 | $conn = new mysqli($servername, $username, $password); 58 | 59 | if ($conn->connect_error) { 60 | die("Connection failed: " . $conn->connect_error); 61 | } 62 | 63 | $sql = "USE bookstore"; 64 | $conn->query($sql); 65 | 66 | $sql = "SELECT * FROM book"; 67 | $result = $conn->query($sql); 68 | ?> 69 | 70 | '; 73 | echo '
'; 74 | echo ''; 75 | echo '
'; 76 | echo '
'; 77 | echo '
'; 78 | echo ''; 79 | } 80 | 81 | if(!isset($_SESSION['id'])){ 82 | echo '
'; 83 | echo '
'; 84 | echo ''; 85 | echo '
'; 86 | echo '
'; 87 | echo '
'; 88 | echo '
'; 89 | } 90 | echo '
'; 91 | echo ""; 92 | echo ""; 93 | while($row = $result->fetch_assoc()) { 94 | echo ""; 104 | } 105 | echo ""; 106 | echo "
"; 95 | echo ""; 96 | echo ''; 102 | echo "
'.''.'
Title: '.$row["BookTitle"].'
ISBN: '.$row["ISBN"].'
Author: '.$row["Author"].'
Type: '.$row["Type"].'
RM'.$row["Price"].'
97 |
98 | Quantity:
99 | 100 | 101 |
"; 103 | echo "
"; 107 | 108 | $sql = "SELECT book.BookTitle, book.Image, cart.Price, cart.Quantity, cart.TotalPrice FROM book,cart WHERE book.BookID = cart.BookID;"; 109 | $result = $conn->query($sql); 110 | 111 | echo ""; 112 | echo ""; 113 | $total = 0; 114 | while($row = $result->fetch_assoc()){ 115 | echo ""; 120 | $total += $row['TotalPrice']; 121 | } 122 | echo ""; 125 | echo "
Cart
"; 116 | echo '
'; 117 | echo $row['BookTitle']."
RM".$row['Price']."
"; 118 | echo "Quantity: ".$row['Quantity']."
"; 119 | echo "Total Price: RM".$row['TotalPrice']."
"; 123 | echo "Total: RM".$total."
"; 124 | echo "
"; 126 | echo '
'; 127 | ?> 128 | 129 | -------------------------------------------------------------------------------- /bookstore/register.php: -------------------------------------------------------------------------------- 1 | connect_error) { 75 | die("Connection failed: " . $conn->connect_error); 76 | } 77 | 78 | $sql = "USE bookstore"; 79 | $conn->query($sql); 80 | 81 | $sql = "INSERT INTO users(UserName, Password) VALUES('".$uname."', '".$upassword."')"; 82 | $conn->query($sql); 83 | 84 | $sql = "SELECT UserID FROM users WHERE UserName = '".$uname."'"; 85 | $result = $conn->query($sql); 86 | while($row = $result->fetch_assoc()){ 87 | $id = $row['UserID']; 88 | } 89 | 90 | $sql = "INSERT INTO customer(CustomerName, CustomerPhone, CustomerIC, CustomerEmail, CustomerAddress, CustomerGender, UserID) 91 | VALUES('".$name."', '".$contact."', '".$ic."', '".$email."', '".$address."', '".$gender."', ".$id.")"; 92 | $conn->query($sql); 93 | 94 | header("Location:index.php"); 95 | } 96 | } 97 | } 98 | } 99 | } 100 | } 101 | } 102 | } 103 | } 104 | } 105 | } 106 | } 107 | } 108 | function test_input($data){ 109 | $data = trim($data); 110 | $data = stripcslashes($data); 111 | $data = htmlspecialchars($data); 112 | return $data; 113 | } 114 | ?> 115 | 116 | 117 | 118 |
119 |
120 | 121 |
122 |
123 |
124 |
125 |
"> 126 |

Register:

127 | Full Name:
128 |

129 | 130 | User Name:
131 |

132 | 133 | New Password:
134 |

135 | 136 | IC Number:
137 |

138 | 139 | E-mail:
140 |

141 | 142 | Mobile Number:
143 |

144 | 145 |
146 | value="Male">Male 147 | value="Female">Female 148 |

149 | 150 |
151 | 152 |

153 | 154 | 155 | 156 |
157 |
158 |
159 | 160 | 161 | -------------------------------------------------------------------------------- /bookstore/edituser.php: -------------------------------------------------------------------------------- 1 | connect_error) { 22 | die("Connection failed: " . $conn->connect_error); 23 | } 24 | 25 | $sql = "USE bookstore"; 26 | $conn->query($sql); 27 | 28 | $sql = "SELECT users.UserName, users.Password, customer.CustomerName, customer.CustomerIC, customer.CustomerEmail, customer.CustomerPhone, customer.CustomerGender, customer.CustomerAddress 29 | FROM users, customer 30 | WHERE users.UserID = customer.UserID AND users.UserID = ".$_SESSION['id'].""; 31 | $result = $conn->query($sql); 32 | while($row = $result->fetch_assoc()){ 33 | $oUserName = $row['UserName']; 34 | $oPassword = $row['Password']; 35 | $oName = $row['CustomerName']; 36 | $oIC = $row['CustomerIC']; 37 | $oEmail = $row['CustomerEmail']; 38 | $oPhone = $row['CustomerPhone']; 39 | $oAddress = $row['CustomerAddress']; 40 | } 41 | 42 | 43 | if ($_SERVER["REQUEST_METHOD"] == "POST") { 44 | if (empty($_POST["name"])) { 45 | $nameErr = "Please enter your name"; 46 | }else{ 47 | if (!preg_match("/^[a-zA-Z ]*$/", $name)){ 48 | $nameErr = "Only letters and white space allowed"; 49 | $name = ""; 50 | }else{ 51 | $name = $_POST['name']; 52 | 53 | if (empty($_POST["uname"])) { 54 | $usernameErr = "Please enter your Username"; 55 | $uname = ""; 56 | }else{ 57 | $uname = $_POST['uname']; 58 | 59 | if (empty($_POST["upassword"])) { 60 | $passwordErr = "Please enter your Password"; 61 | $upassword = ""; 62 | }else{ 63 | $upassword = $_POST['upassword']; 64 | 65 | if (empty($_POST["ic"])){ 66 | $icErr = "Please enter your IC number"; 67 | }else{ 68 | if(!preg_match("/^[0-9 -]*$/", $ic)){ 69 | $icErr = "Please enter a valid IC number"; 70 | $ic = ""; 71 | }else{ 72 | $ic = $_POST['ic']; 73 | 74 | if (empty($_POST["email"])){ 75 | $emailErr = "Please enter your email address"; 76 | }else{ 77 | if (filter_var($email, FILTER_VALIDATE_EMAIL)){ 78 | $emailErr = "Invalid email format"; 79 | $email = ""; 80 | }else{ 81 | $email = $_POST['email']; 82 | 83 | if (empty($_POST["contact"])){ 84 | $contactErr = "Please enter your phone number"; 85 | }else{ 86 | if(!preg_match("/^[0-9 -]*$/", $contact)){ 87 | $contactErr = "Please enter a valid phone number"; 88 | $contact = ""; 89 | }else{ 90 | $contact = $_POST['contact']; 91 | 92 | if (empty($_POST["gender"])){ 93 | $genderErr = "* Gender is required!"; 94 | $gender = ""; 95 | }else{ 96 | $gender = $_POST['gender']; 97 | 98 | if (empty($_POST["address"])){ 99 | $addressErr = "Please enter your address"; 100 | $address = ""; 101 | }else{ 102 | $address = $_POST['address']; 103 | 104 | $servername = "localhost"; 105 | $username = "root"; 106 | $password = ""; 107 | 108 | $conn = new mysqli($servername, $username, $password); 109 | 110 | if ($conn->connect_error) { 111 | die("Connection failed: " . $conn->connect_error); 112 | } 113 | 114 | $sql = "USE bookstore"; 115 | $conn->query($sql); 116 | 117 | $sql = "UPDATE users SET UserName = '".$uname."', Password = '".$upassword."' WHERE UserID = " 118 | .$_SESSION['id'].""; 119 | $conn->query($sql); 120 | 121 | $sql = "UPDATE customer SET CustomerName = '".$name."', CustomerPhone = '".$contact."', 122 | CustomerIC = '".$ic."', CustomerEmail = '".$email."', CustomerAddress = '".$address."', 123 | CustomerGender = '".$gender."'"; 124 | $conn->query($sql); 125 | 126 | header("Location:index.php"); 127 | } 128 | } 129 | } 130 | } 131 | } 132 | } 133 | } 134 | } 135 | } 136 | } 137 | } 138 | } 139 | } 140 | function test_input($data){ 141 | $data = trim($data); 142 | $data = stripcslashes($data); 143 | $data = htmlspecialchars($data); 144 | return $data; 145 | } 146 | ?> 147 | 148 | 149 | 150 |
151 |
152 | 153 |
154 |
155 |
156 |
157 |
"> 158 |

Edit Profile:

159 | Full Name:
160 |

161 | 162 | User Name:
163 |

164 | 165 | New Password:
166 |

167 | 168 | IC Number:
169 |

170 | 171 | E-mail:
172 |

173 | 174 | Mobile Number:
175 |

176 | 177 |
178 | value="Male">Male 179 | value="Female">Female 180 |

181 | 182 |
183 | 184 |

185 | 186 | 187 | 188 |
189 |
190 |
191 | 192 | 193 | -------------------------------------------------------------------------------- /bookstore/checkout.php: -------------------------------------------------------------------------------- 1 | 2 | 3 |
4 |
5 | 6 | 7 |
8 |
9 | connect_error) { 20 | die("Connection failed: " . $conn->connect_error); 21 | } 22 | 23 | $sql = "USE bookstore"; 24 | $conn->query($sql); 25 | 26 | $sql = "SELECT CustomerID from customer WHERE UserID = ".$_SESSION['id'].""; 27 | $result = $conn->query($sql); 28 | while($row = $result->fetch_assoc()){ 29 | $cID = $row['CustomerID']; 30 | } 31 | 32 | $sql = "UPDATE cart SET CustomerID = ".$cID." WHERE 1"; 33 | $conn->query($sql); 34 | 35 | $sql = "SELECT * FROM cart"; 36 | $result = $conn->query($sql); 37 | while($row = $result->fetch_assoc()){ 38 | $sql = "INSERT INTO `order`(CustomerID, BookID, DatePurchase, Quantity, TotalPrice, Status) 39 | VALUES(".$row['CustomerID'].", '".$row['BookID'] 40 | ."', CURRENT_TIME, ".$row['Quantity'].", ".$row['TotalPrice'].", 'N')"; 41 | $conn->query($sql); 42 | } 43 | $sql = "DELETE FROM cart"; 44 | $conn->query($sql); 45 | 46 | $sql = "SELECT customer.CustomerName, customer.CustomerIC, customer.CustomerGender, customer.CustomerAddress, customer.CustomerEmail, customer.CustomerPhone, book.BookTitle, book.Price, book.Image, `order`.`DatePurchase`, `order`.`Quantity`, `order`.`TotalPrice` 47 | FROM customer, book, `order` 48 | WHERE `order`.`CustomerID` = customer.CustomerID AND `order`.`BookID` = book.BookID AND `order`.`Status` = 'N' AND `order`.`CustomerID` = ".$cID.""; 49 | $result = $conn->query($sql); 50 | echo '
'; 51 | echo '
'; 52 | ?> 53 | 54 | Order Successful'; 56 | echo ""; 57 | echo ""; 58 | echo ""; 59 | $row = $result->fetch_assoc(); 60 | echo ""; 61 | echo ""; 62 | echo ""; 63 | echo ""; 64 | echo ""; 65 | echo ""; 66 | echo ""; 67 | echo ""; 68 | 69 | $sql = "SELECT customer.CustomerName, customer.CustomerIC, customer.CustomerGender, customer.CustomerAddress, customer.CustomerEmail, customer.CustomerPhone, book.BookTitle, book.Price, book.Image, `order`.`DatePurchase`, `order`.`Quantity`, `order`.`TotalPrice` 70 | FROM customer, book, `order` 71 | WHERE `order`.`CustomerID` = customer.CustomerID AND `order`.`BookID` = book.BookID AND `order`.`Status` = 'N' AND `order`.`CustomerID` = ".$cID.""; 72 | $result = $conn->query($sql); 73 | $total = 0; 74 | while($row = $result->fetch_assoc()){ 75 | echo ""; 80 | $total += $row['TotalPrice']; 81 | } 82 | echo ""; 83 | echo "
Order Summary
Name: ".$row['CustomerName']."
No.Number: ".$row['CustomerIC']."
E-mail: ".$row['CustomerEmail']."
Mobile Number: ".$row['CustomerPhone']."
Gender: ".$row['CustomerGender']."
Address: ".$row['CustomerAddress']."
Date: ".$row['DatePurchase']."
"; 76 | echo ''; 77 | echo $row['BookTitle']."
RM".$row['Price']."
"; 78 | echo "Quantity: ".$row['Quantity']."
"; 79 | echo "
Total Price: RM".$total."
"; 84 | echo "
"; 85 | 86 | $sql = "UPDATE `order` SET Status = 'y' WHERE CustomerID = ".$cID.""; 87 | $conn->query($sql); 88 | } 89 | 90 | $nameErr = $emailErr = $genderErr = $addressErr = $icErr = $contactErr = ""; 91 | $name = $email = $gender = $address = $ic = $contact = ""; 92 | $cID; 93 | 94 | if(isset($_POST['submitButton'])){ 95 | if (empty($_POST["name"])) { 96 | $nameErr = "Please enter your name"; 97 | }else{ 98 | if (!preg_match("/^[a-zA-Z ]*$/", $name)){ 99 | $nameErr = "Only letters and white space allowed"; 100 | $name = ""; 101 | }else{ 102 | $name = $_POST['name']; 103 | if (empty($_POST["ic"])){ 104 | $icErr = "Please enter your IC number"; 105 | }else{ 106 | if(!preg_match("/^[0-9 -]*$/", $ic)){ 107 | $icErr = "Please enter a valid IC number"; 108 | $ic = ""; 109 | }else{ 110 | $ic = $_POST['ic']; 111 | if (empty($_POST["email"])){ 112 | $emailErr = "Please enter your email address"; 113 | }else{ 114 | if (filter_var($email, FILTER_VALIDATE_EMAIL)){ 115 | $emailErr = "Invalid email format"; 116 | $email = ""; 117 | }else{ 118 | $email = $_POST['email']; 119 | if (empty($_POST["contact"])){ 120 | $contactErr = "Please enter your phone number"; 121 | }else{ 122 | if(!preg_match("/^[0-9 -]*$/", $contact)){ 123 | $contactErr = "Please enter a valid phone number"; 124 | $contact = ""; 125 | }else{ 126 | $contact = $_POST['contact']; 127 | if (empty($_POST["gender"])){ 128 | $genderErr = "* Gender is required!"; 129 | $gender = ""; 130 | }else{ 131 | $gender = $_POST['gender']; 132 | if (empty($_POST["address"])){ 133 | $addressErr = "Please enter your address"; 134 | $address = ""; 135 | }else{ 136 | $address = $_POST['address']; 137 | 138 | $servername = "localhost"; 139 | $username = "root"; 140 | $password = ""; 141 | 142 | $conn = new mysqli($servername, $username, $password); 143 | 144 | if ($conn->connect_error) { 145 | die("Connection failed: " . $conn->connect_error); 146 | } 147 | 148 | $sql = "USE bookstore"; 149 | $conn->query($sql); 150 | 151 | $sql = "INSERT INTO customer(CustomerName, CustomerPhone, CustomerIC, CustomerEmail, CustomerAddress, CustomerGender) 152 | VALUES('".$name."', '".$contact."', '".$ic."', '".$email."', '".$address."', '".$gender."')"; 153 | $conn->query($sql); 154 | 155 | $sql = "SELECT CustomerID from customer WHERE CustomerName = '".$name."' AND CustomerIC = '".$ic."'"; 156 | $result = $conn->query($sql); 157 | while($row = $result->fetch_assoc()){ 158 | $cID = $row['CustomerID']; 159 | } 160 | 161 | $sql = "UPDATE cart SET CustomerID = ".$cID." WHERE 1"; 162 | $conn->query($sql); 163 | 164 | $sql = "SELECT * FROM cart"; 165 | $result = $conn->query($sql); 166 | while($row = $result->fetch_assoc()){ 167 | $sql = "INSERT INTO `order`(CustomerID, BookID, DatePurchase, Quantity, TotalPrice, Status) 168 | VALUES(".$row['CustomerID'].", '".$row['BookID'] 169 | ."', CURRENT_TIME, ".$row['Quantity'].", ".$row['TotalPrice'].", 'N')"; 170 | $conn->query($sql); 171 | } 172 | $sql = "DELETE FROM cart"; 173 | $conn->query($sql); 174 | } 175 | } 176 | } 177 | } 178 | } 179 | } 180 | } 181 | } 182 | } 183 | } 184 | } 185 | function test_input($data){ 186 | $data = trim($data); 187 | $data = stripcslashes($data); 188 | $data = htmlspecialchars($data); 189 | return $data; 190 | } 191 | ?> 192 | 275 |
276 | "; 279 | 280 | echo 'Name:
'; 281 | echo '

'; 282 | 283 | echo 'IC Number:
'; 284 | echo '

'; 285 | 286 | echo 'E-mail:
'; 287 | echo '

'; 288 | 289 | echo 'Mobile Number:
'; 290 | echo '

'; 291 | 292 | echo '
'; 293 | echo 'Male'; 294 | echo 'Female'; 295 | echo '

'; 296 | 297 | echo '
'; 298 | echo ''; 299 | echo '

'; 300 | ?> 301 | 302 | '; 304 | echo '

'; 305 | } 306 | 307 | if(isset($_POST['submitButton'])){ 308 | $servername = "localhost"; 309 | $username = "root"; 310 | $password = ""; 311 | 312 | $conn = new mysqli($servername, $username, $password); 313 | 314 | if ($conn->connect_error) { 315 | die("Connection failed: " . $conn->connect_error); 316 | } 317 | 318 | $sql = "USE bookstore"; 319 | $conn->query($sql); 320 | 321 | $sql = "SELECT customer.CustomerName, customer.CustomerIC, customer.CustomerGender, customer.CustomerAddress, customer.CustomerEmail, customer.CustomerPhone, book.BookTitle, book.Price, book.Image, `order`.`DatePurchase`, `order`.`Quantity`, `order`.`TotalPrice` 322 | FROM customer, book, `order` 323 | WHERE `order`.`CustomerID` = customer.CustomerID AND `order`.`BookID` = book.BookID AND `order`.`Status` = 'N' AND `order`.`CustomerID` = ".$cID.""; 324 | $result = $conn->query($sql); 325 | 326 | echo ''; 327 | echo ""; 328 | echo ""; 329 | $row = $result->fetch_assoc(); 330 | echo ""; 331 | echo ""; 332 | echo ""; 333 | echo ""; 334 | echo ""; 335 | echo ""; 336 | echo ""; 337 | 338 | $sql = "SELECT customer.CustomerName, customer.CustomerIC, customer.CustomerGender, customer.CustomerAddress, customer.CustomerEmail, customer.CustomerPhone, book.BookTitle, book.Price, book.Image, `order`.`DatePurchase`, `order`.`Quantity`, `order`.`TotalPrice` 339 | FROM customer, book, `order` 340 | WHERE `order`.`CustomerID` = customer.CustomerID AND `order`.`BookID` = book.BookID AND `order`.`Status` = 'N' AND `order`.`CustomerID` = ".$cID.""; 341 | $result = $conn->query($sql); 342 | $total = 0; 343 | while($row = $result->fetch_assoc()){ 344 | echo ""; 349 | $total += $row['TotalPrice']; 350 | } 351 | echo ""; 352 | echo "
Order Summary
Name: ".$row['CustomerName']."
No.Number: ".$row['CustomerIC']."
E-mail: ".$row['CustomerEmail']."
Mobile Number: ".$row['CustomerPhone']."
Gender: ".$row['CustomerGender']."
Address: ".$row['CustomerAddress']."
Date: ".$row['DatePurchase']."
"; 345 | echo ''; 346 | echo $row['BookTitle']."
RM".$row['Price']."
"; 347 | echo "Quantity: ".$row['Quantity']."
"; 348 | echo "
Total Price: RM".$total."
"; 353 | 354 | $sql = "UPDATE `order` SET Status = 'y' WHERE CustomerID = ".$cID.""; 355 | $conn->query($sql); 356 | } 357 | ?> 358 |
359 | 360 | --------------------------------------------------------------------------------