Advertisement
Guest User

Untitled

a guest
Feb 2nd, 2016
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.69 KB | None | 0 0
  1. <?php
  2.  
  3.  
  4. class TableRows extends RecursiveIteratorIterator {
  5. function __construct($it) {
  6. parent::__construct($it, self::LEAVES_ONLY);
  7. }
  8. }
  9.  
  10. $servername = "localhost";
  11. $username = "root";
  12. $password = "";
  13.  
  14. try {
  15. $conn = new PDO("mysql:host=$servername;dbname=program", $username, $password);
  16. $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  17. echo "<script>alert('Connected successfully');</script>";
  18. }
  19. catch(PDOException $e)
  20. {
  21. echo "Connection failed: " . $e->getMessage();
  22. }
  23.  
  24. if (isset($_POST['sub']) == 1) {
  25. $id2 = $_POST['id2'];
  26. $qty2 = $_POST['qty2'];
  27. $to = "xxxxxx";
  28. $subject = "New sale report";
  29. $header = "From The shop";
  30.  
  31. $stmt = $conn->prepare("SELECT brand_id FROM brands WHERE brand_id = :id2");
  32. $stmt->bindParam(':id2', $id2);
  33. $stmt->execute();
  34. $result = $stmt->setFetchMode(PDO::FETCH_ASSOC);
  35. foreach(new TableRows(new RecursiveArrayIterator($stmt->fetchAll())) as $k=>$v) {
  36. $r = $v;
  37. $s = $r - $qty2;
  38. if ($stmt->rowCount() === 1) {
  39. echo $s;
  40. $sql_new = "UPDATE brands SET brand_qty='$s'";
  41. $conn->exec($sql_new);
  42. }
  43. echo "<script>alert('New record successfully updated.')</script>";
  44. }
  45. $stmt_m = $conn->prepare("SELECT brand_id FROM brands");
  46. $stmt_m->execute();
  47. $results = $stmt_m->setFetchMode(PDO::FETCH_ASSOC);
  48. foreach ( new TableRows(new RecursiveArrayIterator($stmt_m->fetchAll())) as $t=>$d ) {
  49. $body = "Item sold: ". $d ."nn Quantity sold: ". $s ." ";
  50. }
  51. //mail($to, $subject, $body, $header);
  52. }
  53.  
  54. $id = $_POST['id'];
  55. $qty = $_POST['qty'];
  56.  
  57. if (isset($_POST['submit']) == 1) {
  58. $stmt_mt = $conn->prepare("SELECT brand_id FROM brands WHERE brand_id = :id");
  59. $stmt_mt->bindParam(':id', $id);
  60. $stmt_mt->execute();
  61.  
  62.  
  63. if($stmt_mt->rowCount() === 1) {
  64. echo "<script>alert('exists in database.')</script>";
  65. } else {
  66. echo "<script>alert('adding hoodie to database.')</script>";
  67. $sql = "INSERT INTO brands (brand_id, brand_qty) VALUES ('$id', '$qty')";
  68. $conn->exec($sql);
  69. echo "<script>alert('New record inserted succesfully.')</script>";
  70. }
  71. }
  72.  
  73.  
  74.  
  75. ?>
  76.  
  77. <!DOCTYPE html>
  78. <html>
  79. <head>
  80. <title></title>
  81. </head>
  82. <body>
  83. <form method="post">
  84. <input type="text" placeHolder="Enter ID" name="id" />
  85. <input type="number" placeHolder="Quantity" name="qty" />
  86. <input name="submit" type="submit" value="submit" />
  87. </form>
  88.  
  89. <form method="post">
  90. <input type="text" placeHolder="Enter ID" name="id2" />
  91. <input type="number" placeHolder="Number of sales" name="qty2" />
  92. <input name="sub" type="submit" value="submit" />
  93. </form>
  94.  
  95. </body>
  96. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement