带有邮件的PHP购物车-发布错误

时间:2018-07-09 10:20:03

标签: php html shopping-cart

美好的一天。

我为我的一个朋友设计了一个带有购物车的网站。我是通过w3schools和freecodecamp自学的。 (我受雇为网络管理员)

这是我在StackOverFlow上的第一篇文章,尽管我在自学过程中已经多次使用该网站。

我想发送一封电子邮件,显示我在购物车页面上选择的结果。我可以很好地发布文本(姓名,联系电话等),但是在发布SQL选择时遇到麻烦。 (在下面的部分)

我想通过邮件发送的代码:

<?php		
foreach ($_SESSION["cart_item"] as $item){
		?>
				<tr>
				<td style="text-align:left;border-bottom:#F0F0F0 1px solid;"><strong><?php echo $item["name"]; ?></strong></td>
				<td style="text-align:left;border-bottom:#F0F0F0 1px solid;"><?php echo $item["code"]; ?></td>
				<td style="text-align:right;border-bottom:#F0F0F0 1px solid;"><?php echo $item["quantity"]; ?></td>
				<td style="text-align:right;border-bottom:#F0F0F0 1px solid;"><?php echo "R".$item["price"]; ?></td>
				<td style="text-align:center;border-bottom:#F0F0F0 1px solid;"><a href="index.php?action=remove&code=<?php echo $item["code"]; ?>" class="btnRemoveAction">Remove Item</a></td>
				</tr>
				<?php
    $item_total += ($item["price"]*$item["quantity"]);
		}
		?>

代码:

PHP购物车:

<?php
session_start();
require_once("dbcontroller.php");
$db_handle = new DBController();
if(!empty($_GET["action"])) {
switch($_GET["action"]) {
	case "add":
		if(!empty($_POST["quantity"])) {
			$productByCode = $db_handle->runQuery("SELECT * FROM tblproduct WHERE code='" . $_GET["code"] . "'");
			$itemArray = array($productByCode[0]["code"]=>array('name'=>$productByCode[0]["name"], 'code'=>$productByCode[0]["code"], 'quantity'=>$_POST["quantity"], 'price'=>$productByCode[0]["price"]));
			
			if(!empty($_SESSION["cart_item"])) {
				if(in_array($productByCode[0]["code"],array_keys($_SESSION["cart_item"]))) {
					foreach($_SESSION["cart_item"] as $k => $v) {
							if($productByCode[0]["code"] == $k) {
								if(empty($_SESSION["cart_item"][$k]["quantity"])) {
									$_SESSION["cart_item"][$k]["quantity"] = 0;
								}
								$_SESSION["cart_item"][$k]["quantity"] += $_POST["quantity"];
							}
					}
				} else {
					$_SESSION["cart_item"] = array_merge($_SESSION["cart_item"],$itemArray);
				}
			} else {
				$_SESSION["cart_item"] = $itemArray;
			}
		}
	break;
	case "remove":
		if(!empty($_SESSION["cart_item"])) {
			foreach($_SESSION["cart_item"] as $k => $v) {
					if($_GET["code"] == $k)
						unset($_SESSION["cart_item"][$k]);				
					if(empty($_SESSION["cart_item"]))
						unset($_SESSION["cart_item"]);
			}
		}
	break;
	case "empty":
		unset($_SESSION["cart_item"]);
	break;	
}
}
?>

	<form  method="POST"  action="mailp2.php">
<div id="shopping-cart">
<div class="txt-heading">Shopping Cart <a id="btnEmpty" href="index.php?action=empty">Empty Cart</a></div>
<?php
if(isset($_SESSION["cart_item"])){
$item_total = 0;
?>	
	
	 
<table cellpadding="10" cellspacing="1">
<tbody>
<tr>
<th style="text-align:left;"><strong>Name</strong></th>
<th style="text-align:left;"><strong>Code</strong></th>
<th style="text-align:right;"><strong>Quantity</strong></th>
<th style="text-align:right;"><strong>Price</strong></th>
<th style="text-align:center;"><strong>Action</strong></th>
</tr>	
	
<?php		
foreach ($_SESSION["cart_item"] as $item){
		?>
				<tr>
				<td style="text-align:left;border-bottom:#F0F0F0 1px solid;"><strong><?php echo $item["name"]; ?></strong></td>
				<td style="text-align:left;border-bottom:#F0F0F0 1px solid;"><?php echo $item["code"]; ?></td>
				<td style="text-align:right;border-bottom:#F0F0F0 1px solid;"><?php echo $item["quantity"]; ?></td>
				<td style="text-align:right;border-bottom:#F0F0F0 1px solid;"><?php echo "R".$item["price"]; ?></td>
				<td style="text-align:center;border-bottom:#F0F0F0 1px solid;"><a href="index.php?action=remove&code=<?php echo $item["code"]; ?>" class="btnRemoveAction">Remove Item</a></td>
				</tr>
				<?php
    $item_total += ($item["price"]*$item["quantity"]);
		}
		?>

<tr>
	
<td colspan="5" align=right><strong>Total:</strong> <?php echo "R".$item_total; ?></td>
</tr>
</tbody>
</table>		
  <?php
}
?>
</div>

//Item Selection

	<br>
<div id="product-grid">
	<div class="txt-heading">Products</div>
	<?php
	$product_array = $db_handle->runQuery("SELECT * FROM tblproduct ORDER BY id ASC");
	if (!empty($product_array)) { 
		foreach($product_array as $key=>$value){
	?>
		<div class="product-item" style="height: 250px; width: 220px;">
			<form method="post" action="index.php?action=add&code=<?php echo $product_array[$key]["code"]; ?>">
			<div class="product-image"><img src="<?php echo $product_array[$key]["image"]; ?>" width="50px" height="auto"></div>
			<div><strong><?php echo $product_array[$key]["name"]; ?></strong></div>
			<div class="product-price"><?php echo "R".$product_array[$key]["price"]; ?></div>
			<div><input type="text" name="quantity" value="1" size="2" /><input type="submit" value="Add to cart" class="btnAddAction" /></div>
			</form>
		</div>
	<?php
			}
	}
	?>

PHP Mailer:

<?php
//SMTP through PEAR PAckage.
error_reporting(E_ALL ^ E_NOTICE ^ E_DEPRECATED ^ E_STRICT);

set_include_path("." . PATH_SEPARATOR . ($UserDir = dirname($_SERVER['DOCUMENT_ROOT'])) . "/pear/php" . PATH_SEPARATOR . get_include_path());
require_once "Mail.php";

$host = "s"; 	//SMTP Auth Start
$username = "";
$password = "";
$port = "465";								//SMTP Auth End

//Mail Details
$to = "";
$email_from = "";
$email_subject = "Online Order" ;

//Request from HTML page
if($_SERVER['REQUEST_METHOD'] == "POST"){
$name_field =  $_POST['name'];
$email_field =  $_POST['email'];
$message  =  $_POST['message'];
$contactnumber = $_POST['contactnumber'];

	//Problem Section
  
	foreach($_POST["cart-item"] as $item)
 {
$quantity1 = 'name'.$item. 'Test';
$quantity2 = $_GET [$item['code']];
//$quantity3 = $item['quantity'];
//$quantity4 = $item['price'];
//$quantity5 = 
//$quantity6 = 
//$quantity7 = 
//$quantity8 = 
//$quantity9 = 

 }
// End problem section.	
 
//foreach($_POST['check'] as $value) { $check_msg .= "Checked: $value\n";}
$email_body =  "\n From: $name_field \n E-Mail: $email_field\n Contact: $contactnumber\n \n $check_msg\n \n Growth: $quantity1\n Wheat Germ: $quantity2\n Staple: $quantity3\n Hi-Growth: $quantity4\n Prime: $quantity5\n Color Luster: $quantity6\n Red Parrot Food: $quantity7\n Flower Parrot: $quantity8\n Cichlid: $quantity9\n Color Enhancer: $quantity10  \n \n Message:\n $message";}
$email_address = "";


//Mail Build
$headers = array ('From' => $email_from, 'To' => $to, 'Subject' => $email_subject, 'Reply-To' => $email_address);
$smtp = Mail::factory('smtp', array ('host' => $host, 'port' => $port, 'auth' => true, 'username' => $username, 'password' => $password));
$mail = $smtp->send($to, $headers, $email_body);

//Messages.
if (PEAR::isError($mail)) {
echo("<p>" . $mail->getMessage() . "</p>");
} else {
	echo ("<p>Message successfully sent!</p>");
}
?>

这是我的第一个实时项目,非常感谢您提供的任何帮助。

谢谢。

0 个答案:

没有答案