Advertisement
Guest User

phpFIle

a guest
Apr 4th, 2019
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.18 KB | None | 0 0
  1. <?php
  2. $host="aqc353.encs.concordia.ca";
  3. $port=3306;
  4. $socket="";
  5. $user="aqc353_4";
  6. $password="datab123";
  7. $dbname="aqc353_4";
  8. $mysqli = new mysqli($host, $user, $password, $dbname);
  9.  
  10. ?>
  11. <form method="POST" action="">
  12.  
  13. <?php
  14. $getAllBooks = "SELECT * FROM Book";
  15. $bookChosenCount = 0;
  16. $quantityOfBooks = array();
  17. $bookISBN = array();
  18. if ($result = $mysqli->query($getAllBooks)) {
  19. $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>";
  20. /* fetch associative array */
  21. while ($row = $result->fetch_assoc()) {
  22. $theAnswere = $theAnswere . '<tr><td><input type="checkbox" name="booksChosen[]" value="'.$bookChosenCount.'"/></td><td name="'.$bookChosenCount.'">' . $row ["ISBN"] . '</td><td>' .
  23. $row ["Title"] . '</td><td>' . $row ["Price"] . '</td><td>'. $row ["Quantity"].'</td>' . '<td><input type="number" min="1" max="50" name="qty'.$bookChosenCount.'" /></td></tr>' ;
  24.  
  25. $quantityOfBooks[$bookChosenCount] = $row["Quantity"];
  26. $bookISBN[$bookChosenCount] = $row["ISBN"];
  27. $bookChosenCount++;
  28.  
  29. }
  30. $theAnswere = $theAnswere . '</table>';
  31. echo $theAnswere;
  32.  
  33.  
  34. /* free result set */
  35. $result->free();
  36.  
  37. echo '<br><br>';
  38. echo '<input type="submit" name="submitButton" value="Buy">';
  39. }
  40. ?>
  41.  
  42. </form>
  43.  
  44. <?php
  45.  
  46. if ( isset( $_POST['submitButton'] ) ){
  47. foreach($_POST['booksChosen'] as $selected){
  48.  
  49. $state = "Completed";
  50. echo "qty of book: " . $quantityOfBooks[$selected] . "<br>";
  51.  
  52. if ($_POST["qty".$selected] > $quantityOfBooks[$selected]){
  53. $state = "backorder";
  54. }
  55. echo "state: " . $state . "<br>";
  56. echo "ISBN: " . $bookISBN[$selected]. "<br>";
  57. echo "QtyToBuy: " . $_POST["qty".$selected] . "<br>";
  58. echo "date: ". date('Y-m-d'). "<br>";
  59.  
  60. $insertSale = "INSERT into Sale (State, Quantity, `Date`, EmployeeID, CustomerEmail, BookISBN) ".
  61. " VALUES('".$state."',".$_POST["qty".$selected].",'".date('Y-m-d')."', 1230, 'email1',".$bookISBN[$selected].")";
  62.  
  63. echo $insertSale;
  64.  
  65. if ($result = $mysqli->query($insertSale)) {
  66. echo " The book ". $bookISBN[$selected]. "has been successfully purchased! <br>";
  67. }
  68. }
  69. }
  70.  
  71. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement