Jump to content

Having problems with assignement


andrej13

Recommended Posts

How and where  do I assign  $_SESSION['name'] ?

 

I have made a cart where you can add drinks. But the output in the mail.php gives only the id of the drinks, but not the name . How can I make it happen to see also the names of the ordered drinks? my sql table is 'products' and 'name' is a column in this table.

 

thanks in advance

 

mail.php

<?php
// Include MySQL class
require_once('inc/mysql.class.php');
// Include database connection
require_once('inc/global.inc.php');
session_start();



if($_POST['sendemail'] == 'Email')
{

  $qry = 'SELECT name FROM products WHERE id = ' .$_SESSION['cart'];
  $result = mysql_query($qry);
  $row = mysql_fetch_assoc($result);
  $_SESSION['name'] = $row['name'];
  
  
  $info =( $_SESSION['cart'] . ' :test ' . $_SESSION['name']);
  $headers = 'From: Sender <[email protected]>';
  mail('[email protected]', 'Subject', $info, $headers);
  echo "DEBdsUG".$_POST["cart"]; 
  echo 'Your mail has been sent';
}
else
{
  echo 'Your mail was not sent';
}

?>

 

cart.php

<?php
// Include MySQL class
require_once('inc/mysql.class.php');
// Include database connection
require_once('inc/global.inc.php');
// Include functions
require_once('inc/functions.inc.php');
// Start the session

session_start();

// Process actions
$cart = $_SESSION['cart'];
$action = $_GET['action'];
switch ($action) {
case 'add':
	if ($cart) {
		$cart .= ','.$_GET['id'];
	} else {
		$cart = $_GET['id'];
	}
	break;
case 'delete':
	if ($cart) {
		$items = explode(',',$cart);
		$newcart = '';
		foreach ($items as $item) {
			if ($_GET['id'] != $item) {
				if ($newcart != '') {
					$newcart .= ','.$item;
				} else {
					$newcart = $item;
				}
			}
		}
		$cart = $newcart;
	}
	break;
case 'update':
if ($cart) {
	$newcart = '';
	foreach ($_POST as $key=>$value) {
		if (stristr($key,'qty')) {
			$id = str_replace('qty','',$key);
			$items = ($newcart != '') ? explode(',',$newcart) : explode(',',$cart);
			$newcart = '';
			foreach ($items as $item) {
				if ($id != $item) {
					if ($newcart != '') {
						$newcart .= ','.$item;
					} else {
						$newcart = $item;
					}
				}
			}
			for ($i=1;$i<=$value;$i++) {
				if ($newcart != '') {
					$newcart .= ','.$id;
				} else {
					$newcart = $id;
				}
			}
		}
	}
}
$cart = $newcart;
break;
}
$_SESSION['cart'] = $cart;
?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>PHP Shopping Cart Demo &#0183; Cart</title>
<link rel="stylesheet" href="css/styles.css" />
</head>

<body>

<div id="shoppingcart">

<h1>Your Shopping Cart</h1>


<?php
echo writeShoppingCart();

?>

</div>

<div id="contents">

<h1>Please check quantities...</h1>

<?php
echo showCart();
?>

<p><a href="index.php">Back to bookshop...</a></p>

<form action="mail.php" method="post">
<input type="submit" name="sendemail" value="Email" />
<input type="hidden" name="cart" value="<?= $cart; ?>" />
</form>




</div>

</body>
</html>

 

 

Link to comment
https://forums.phpfreaks.com/topic/225667-having-problems-with-assignement/
Share on other sites

try changing your query to this as you are not selecting the id from the database to compare to, and you did not close the mysql query properly

 

$cart = $_SESSION['cart'];
$qry = "SELECT name, id FROM products WHERE id = '$cart'";

try changing your query to this as you are not selecting the id from the database to compare to, and you did not close the mysql query properly

 

$cart = $_SESSION['cart'];
$qry = "SELECT name, id FROM products WHERE id = '$cart'";

 

it works, but only when I order 1 drink; when I order more drinks, my mail is empty.

 

Some info: when I order 3 coca colas, my output is 3,3,3 (id of coca cola is 3) and when I order 1 coca cola, my output is 3 ;

 

 

try changing your query to this as you are not selecting the id from the database to compare to, and you did not close the mysql query properly

 

$cart = $_SESSION['cart'];
$qry = "SELECT name, id FROM products WHERE id = '$cart'";

 

 

check it out on the website

 

http://fhcs.be/cart-demo4/

$cartID=$_SESSION['cart'];
$carts=explode(",", $cartID);
foreach ($carts as $cart) {
$qry = "SELECT name, id FROM products WHERE id = '$cart'";
$result = mysql_query($qry);
$row = mysql_fetch_assoc($result);
$name=$row['name'];
$info.="$cart test: $name<br/>\n";
}
$headers = 'From: Sender <[email protected]>';
mail('[email protected]', 'Subject', $info, $headers);

etc etc

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.