Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- $host="aqc353.encs.concordia.ca";
- $port=3306;
- $socket="";
- $user="aqc353_4";
- $password="datab123";
- $dbname="aqc353_4";
- $mysqli = new mysqli($host, $user, $password, $dbname);
- ?>
- <form method="POST" action="">
- <?php
- $getAllBooks = "SELECT * FROM Book";
- $bookChosenCount = 0;
- $quantityOfBooks = array();
- $bookISBN = array();
- if ($result = $mysqli->query($getAllBooks)) {
- $theAnswere = "<table width='100%'><tr><td id='col1'></td><td>ISBN</td><td>Title</td><td>Price</td><td>Stock Available</td><td>Amount to Buy</td></tr>";
- /* fetch associative array */
- while ($row = $result->fetch_assoc()) {
- $theAnswere = $theAnswere . '<tr><td><input type="checkbox" name="booksChosen[]" value="'.$bookChosenCount.'"/></td><td name="'.$bookChosenCount.'">' . $row ["ISBN"] . '</td><td>' .
- $row ["Title"] . '</td><td>' . $row ["Price"] . '</td><td>'. $row ["Quantity"].'</td>' . '<td><input type="number" min="1" max="50" name="qty'.$bookChosenCount.'" /></td></tr>' ;
- $quantityOfBooks[$bookChosenCount] = $row["Quantity"];
- $bookISBN[$bookChosenCount] = $row["ISBN"];
- $bookChosenCount++;
- }
- $theAnswere = $theAnswere . '</table>';
- echo $theAnswere;
- /* free result set */
- $result->free();
- echo '<br><br>';
- echo '<input type="submit" name="submitButton" value="Buy">';
- }
- ?>
- </form>
- <?php
- if ( isset( $_POST['submitButton'] ) ){
- foreach($_POST['booksChosen'] as $selected){
- $state = "Completed";
- echo "qty of book: " . $quantityOfBooks[$selected] . "<br>";
- if ($_POST["qty".$selected] > $quantityOfBooks[$selected]){
- $state = "backorder";
- }
- echo "state: " . $state . "<br>";
- echo "ISBN: " . $bookISBN[$selected]. "<br>";
- echo "QtyToBuy: " . $_POST["qty".$selected] . "<br>";
- echo "date: ". date('Y-m-d'). "<br>";
- $insertSale = "INSERT into Sale (State, Quantity, `Date`, EmployeeID, CustomerEmail, BookISBN) ".
- " VALUES('".$state."',".$_POST["qty".$selected].",'".date('Y-m-d')."', 1230, 'email1',".$bookISBN[$selected].")";
- echo $insertSale;
- if ($result = $mysqli->query($insertSale)) {
- echo " The book ". $bookISBN[$selected]. "has been successfully purchased! <br>";
- }
- }
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement