Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class TableRows extends RecursiveIteratorIterator {
- function __construct($it) {
- parent::__construct($it, self::LEAVES_ONLY);
- }
- }
- $servername = "localhost";
- $username = "root";
- $password = "";
- try {
- $conn = new PDO("mysql:host=$servername;dbname=program", $username, $password);
- $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
- echo "<script>alert('Connected successfully');</script>";
- }
- catch(PDOException $e)
- {
- echo "Connection failed: " . $e->getMessage();
- }
- if (isset($_POST['sub']) == 1) {
- $id2 = $_POST['id2'];
- $qty2 = $_POST['qty2'];
- $to = "xxxxxx";
- $subject = "New sale report";
- $header = "From The shop";
- $stmt = $conn->prepare("SELECT brand_id FROM brands WHERE brand_id = :id2");
- $stmt->bindParam(':id2', $id2);
- $stmt->execute();
- $result = $stmt->setFetchMode(PDO::FETCH_ASSOC);
- foreach(new TableRows(new RecursiveArrayIterator($stmt->fetchAll())) as $k=>$v) {
- $r = $v;
- $s = $r - $qty2;
- if ($stmt->rowCount() === 1) {
- echo $s;
- $sql_new = "UPDATE brands SET brand_qty='$s'";
- $conn->exec($sql_new);
- }
- echo "<script>alert('New record successfully updated.')</script>";
- }
- $stmt_m = $conn->prepare("SELECT brand_id FROM brands");
- $stmt_m->execute();
- $results = $stmt_m->setFetchMode(PDO::FETCH_ASSOC);
- foreach ( new TableRows(new RecursiveArrayIterator($stmt_m->fetchAll())) as $t=>$d ) {
- $body = "Item sold: ". $d ."nn Quantity sold: ". $s ." ";
- }
- //mail($to, $subject, $body, $header);
- }
- $id = $_POST['id'];
- $qty = $_POST['qty'];
- if (isset($_POST['submit']) == 1) {
- $stmt_mt = $conn->prepare("SELECT brand_id FROM brands WHERE brand_id = :id");
- $stmt_mt->bindParam(':id', $id);
- $stmt_mt->execute();
- if($stmt_mt->rowCount() === 1) {
- echo "<script>alert('exists in database.')</script>";
- } else {
- echo "<script>alert('adding hoodie to database.')</script>";
- $sql = "INSERT INTO brands (brand_id, brand_qty) VALUES ('$id', '$qty')";
- $conn->exec($sql);
- echo "<script>alert('New record inserted succesfully.')</script>";
- }
- }
- ?>
- <!DOCTYPE html>
- <html>
- <head>
- <title></title>
- </head>
- <body>
- <form method="post">
- <input type="text" placeHolder="Enter ID" name="id" />
- <input type="number" placeHolder="Quantity" name="qty" />
- <input name="submit" type="submit" value="submit" />
- </form>
- <form method="post">
- <input type="text" placeHolder="Enter ID" name="id2" />
- <input type="number" placeHolder="Number of sales" name="qty2" />
- <input name="sub" type="submit" value="submit" />
- </form>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement