0

I added a cart function to this website, before It would only send one email after purchase because there was only one item at a time being purchased, now that I have added the cart function it needs to send one email instead of multiple emails.

This is the code I am using for processing the items purchased. EDIT: Updated code

$item = array();
        $size = array();
        $color = array();
        $price = array();

        $querys = "SELECT * FROM ".$_SESSION["username"];
        $ress = mysqli_query($connection, $querys);
        if(mysqli_num_rows($ress) > 0){
            while($rows = $ress->fetch_assoc()){
                $bid = $rows["itemid"];
                $description = $_POST["description"];
                $bitem = $rows["itemname"];
                $bsize = $rows["size"];
                $bcolor = $rows["color"];
                $bprice = $rows["price"];
                $bcategory = $rows["category"];
                $firstname = $_POST["firstname"];
                $lastname = $_POST["lastname"];
                $company = $_POST["company"];
                $address = $_POST["address"];
                $suite = $_POST["suite"];
                $city = $_POST["city"];
                $state = $_POST["state"];
                $zip = $_POST["zip"];
                $country = $_POST["country"];
                $saddress = $_POST["saddress"];
                $ssuite = $_POST["ssuite"];
                $scity = $_POST["scity"];
                $sstate = $_POST["state"];
                $szip = $_POST["szip"];
                $scountry = $_POST["scountry"];
                $phone = $_POST["phone"];
                $fax = $_POST["fax"];
                $email = $_POST["email"];
                $website = $_POST["website"];
                $creditcard = $_POST["creditcard"];
                $exdate = $_POST["experationdate"];
                $cvv = $_POST["cvv"];
                $create_date = date( 'Y-m-d H:i:s' );
                $str_bitem = str_replace("'", "''", $bitem);
                array_push($item, $bitem);
                array_push($size, $bsize);
                array_push($color, $bcolor);
                array_push($price, $bprice);

                $gw = new gwapi();
                $gw->setLogin("demo", "password");
                $gw->setBilling($firstname, $lastname, $company, $address, $suite, $city, $state, $zip, $country, $phone, $fax, $email, $website);
                $gw->setShipping($firstname, $lastname, $company, $saddress, $ssuite, $scity, $sstate, $szip, $scountry, $email);
                $gw->setOrder($bid, $description, 0, 0, 0, $_SERVER["REMOTE_ADDR"]);
                $gw->doSale($bprice, $creditcard, $exdate, $cvv);

                if($gw->responses['response'] == 1){
                    $firstname = $_SESSION["firstname"];
                    $lastname = $_SESSION["lastname"];
                    $query = "INSERT INTO orders (item_name, item_id, item_size, item_color, payer_email, first_name, last_name, address_name, address_city, address_state, address_zip, amount, country, create_date, shipped)
                    VALUES ('$str_bitem', '$bid', '$bsize', '$bcolor', '$email', '$firstname', '$lastname', '$saddress', '$scity', '$sstate', '$szip', '$bprice', '$scountry', '$create_date', 0)";

                    if(mysqli_query($db, $query)){
                        if($bsize=="N/A" && $bcolor=="N/A"){
                            define('DB_SERVER1', 'localhost');
                            define('DB_USERNAME1', 'grampmkn_gramsandpops');
                            define('DB_PASSWORD1', 'Grams123');
                            define('DB_DATABASE1', 'grampmkn_shop');
                            $db1 = mysqli_connect(DB_SERVER1,DB_USERNAME1,DB_PASSWORD1,DB_DATABASE1);

                            $sqlquery = "UPDATE `".$bcategory."` SET sold='1' WHERE id='$bid'";
                            if(mysqli_query($db1, $sqlquery)){
                                echo "Worked!";
                            }else{
                                echo "Error! : ";
                                echo mysqli_error($db1);
                            }
                        }else{
                            echo $bsize;
                            echo ":::::::";
                            echo $bcolor;
                            $conn = mysqli_connect('localhost', 'grampmkn_gramsandpops', 'Grams123', 'grampmkn_shop_quantity');
                            $query = "SELECT quantity FROM `".$bitem."` WHERE size='$bsize' AND color='$bcolor'";
                            $result = $conn->query ($query) or die($conn->error);
                            if ($result->num_rows > 0) {
                                while($row = $result->fetch_assoc()){
                                    $q = --$row["quantity"];
                                    $query = "UPDATE `$bitem` SET quantity='$q' WHERE size='$bsize' AND color='$bcolor'";
                                    if(mysqli_query($conn, $query)){
                                    }else{
                                        echo "TESTHING1";
                                        echo mysqli_error($conn);
                                    }
                                }
                            }else{
                                $query = "SELECT quantity FROM `".$bitem."` WHERE size='$bsize'";
                                $result = $conn->query ($query) or die($db->error);
                                while($row = $result->fetch_assoc()){
                                    $q = --$row["quantity"];
                                    $query = "UPDATE `$bitem` SET quantity='$q' WHERE size='$bsize'";
                                    if(mysqli_query($conn, $query)){
                                    }else{
                                        echo "TESTING";
                                        echo mysqli_error($conn);
                                    }
                                }
                            }
                        }
                    }else{
                        echo mysqli_error($db);
                        echo " ::::: ";
                        echo mysqli_error($connection);
                    }
                    $masked = str_pad(substr($creditcard, -4), strlen($creditcard), '*', STR_PAD_LEFT);

                    $transid = $gw->responses['transactionid'];

                    $username = $_SESSION["username"];
                    $query = "DELETE FROM $username WHERE itemid='$bid'";
                    if(mysqli_query($connection, $query)){
                        echo "Worked";
                    }else{
                        echo "Error! ";
                        echo mysqli_error($connection);
                    }

                    $query = "show tables;";
                    $tres = $connection->query($query) or die($connection->error);
                    if($tres->num_rows > 0){
                        while($trow = $tres->fetch_assoc()){
                            $table = $trow["Tables_in_grampmkn_cart"];
                            $sql = "DELETE FROM `".$table."` WHERE category='$table' AND itemid='$bid'";
                            if(mysqli_query($connection, $sql)){
                                echo "Deleted items from others carts!";
                            }else{
                                echo "There were no similar items in others carts!";
                                echo $table." : ";
                                echo $tcategory." : ";
                                echo mysqli_error($connection);
                            }
                        }
                    }
                }else{
                    ?>
                    <div class="alert alert-warning">
                        <strong>Error!</strong> Your card was declined!
                    </div>
                    <?php
                }
            }
            $itemlist = implode(', ', $item);
            $sizelist = implode(', ', $size);
            $colorlist = implode(', ', $color);
            $pricelist = implode(', ', $price);
            if(!empty($bcolor)){
                $to = $email;
                $subject = "Purchase Confirmation";
                $txt = "Thank you for your purchase of <br> ".$sizelist." ".$itemlist." - ".$colorlist."<br>Price: ".$pricelist."<br>Transaction ID: ".$transid."<br>CC: ".$masked."<br> <img src='https://gramsandpops.com/images/Logo.png'>";
                $headers = "MIME-Version: 1.0" . "\r\n";
                $headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
                $headers .= 'From: info@gramsandpops.com' . "\r\n" .
                        'Reply-To: info@gramsandpops.com' . "\r\n" .
                        'X-Mailer: PHP/' . phpversion();
                if(mail($to,$subject,$txt,$headers)){
                }
            }else{
                $to = $email;
                $subject = "Purchase Confirmation";
                $txt = "Thank you for your purchase of <br> ".$sizelist." ".$itemlist."<br>Price: ".$pricelist."<br>Transaction ID: ".$transid."<br>CC: ".$masked."<br> <img src='https://gramsandpops.com/images/Logo.png'>";
                $headers = "MIME-Version: 1.0" . "\r\n";
                $headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
                $headers .= 'From: info@gramsandpops.com' . "\r\n" .
                        'Reply-To: info@gramsandpops.com' . "\r\n" .
                        'X-Mailer: PHP/' . phpversion();
                if(mail($to,$subject,$txt,$headers)){
                }
            }
            $to = "gramsandpopsblueridge@gmail.com";
            $subject = "Purchase Order";
            $txt = "Item: ".$itemlist."\n Size: ".$sizelist."\n Color: ".$colorlist."\n Price: ".$pricelist."\n Address: ".$saddress.
                " ".$scity.", ".$sstate." ".$szip."\n Name: ".$firstname." ".$lastname."\n CC: ".$masked;
            $headers = "From: admin@gramsandpops.com" . "\r\n" .
                    "CC: admin@gramsandpops.com";
            if(mail($to,$subject,$txt,$headers)){
            }
            ?>
            <div class="alert alert-success">
                <strong>Success!</strong> Your transaction was successfully processed! An email confirmation will be sent shortly.
            </div>
            <?php
        }else{
            echo mysqli_error($connection);
        }

0 Answers0