0

I want the email message which contain the order details of customers.This code is ok for only one order but I don't know how to send message if customer select more than one products. Here is my code :

include('connect.php');
if(!isset($_SESSION["id"]))
{
header("location:order_login.php");
}
$id=$_SESSION["id"];
$emp_name=$_SESSION['name'];

$result=$mysqli->query("select order_id from orders where user_id='$id' ORDER BY order_id desc limit 1"); 
$obj=$result->fetch_object();
$order_id=$obj->order_id;

$results=$mysqli->query("select * from order_details where order_id=$order_id");
        while($obj=$results->fetch_object())
      {

        $product_name=$obj->product_name;
        $product_code=$obj->product_code;
        $qty=$obj->qty;
        $subtotal=$obj->sub_total;


        }


$to="[email protected]";
$bcc_mail="[email protected]";
$subject= "Order Confirmation";

$message ='

<table width="100%" border="1" cellpadding="0" cellspacing="0">
<tr>
<td>Name</td>
<td>Product Code</td>
<td>Quantity</td>
<td>Sub Total</td>
</tr>

<tr>
<td>'.$product_name.'</td>
<td>'.$product_code.'</td>
<td>'.$qty.'</td>
<td>'.$subtotal.'</td>
</tr>

</table>';

echo $message;

$headers  = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= 'From: [email protected]'."\r\n";
$headers .= "Bcc: $bcc_mail\r\n";
mail($to, $subject, $message, $headers);

unset($_SESSION['products']);

?>

please check my code thanks.

2
  • Can you please specify how you store multiple orders in database?.if i guess than you must be storing multiple orders in order_details with same order id and if you do that then you can get all orders from order_details by your order id and please to improve your result don't fire separate query you can do this by joins in single query. Commented Sep 10, 2015 at 5:57
  • yes i am storing multiple orders in order_details with same order id Commented Sep 10, 2015 at 5:58

2 Answers 2

1

You should create a new tr section for each product:

include('connect.php');
if(!isset($_SESSION["id"]))
{
    header("location:order_login.php");
}
$id=$_SESSION["id"];
$emp_name=$_SESSION['name'];

$result=$mysqli->query("select order_id from orders where user_id='$id' ORDER BY order_id desc limit 1"); 
$obj=$result->fetch_object();
$order_id=$obj->order_id;   

$to="[email protected]";
$bcc_mail="[email protected]";
$subject= "Order Confirmation";

$message ='

<table width="100%" border="1" cellpadding="0" cellspacing="0">
<tr>
    <td>Name</td>
    <td>Product Code</td>
    <td>Quantity</td>
    <td>Sub Total</td>
</tr>';

$results=$mysqli->query("select * from order_details where order_id=$order_id");
while($obj=$results->fetch_object())
{           
    $product_name=$obj->product_name;
    $product_code=$obj->product_code;
    $qty=$obj->qty;
    $subtotal=$obj->sub_total;          

    $message .='
    <tr>
        <td>'.$product_name.'</td>
        <td>'.$product_code.'</td>
        <td>'.$qty.'</td>
        <td>'.$subtotal.'</td>
    </tr>';
}

$message .='
</table>';

echo $message;

$headers  = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= 'From: [email protected]'."\r\n";
$headers .= "Bcc: $bcc_mail\r\n";
mail($to, $subject, $message, $headers);

unset($_SESSION['products']);

?>
Sign up to request clarification or add additional context in comments.

Comments

1

Please try this code hope this helps:

$result=$mysqli->query("select order_id from orders where user_id='$id' ORDER BY order_id desc limit 1"); 
$obj=$result->fetch_object();
$order_id=$obj->order_id;

$results=$mysqli->query("select * from order_details where order_id=$order_id");

$to="[email protected]";
$bcc_mail="[email protected]";
$subject= "Order Confirmation";

$message .='<table width="100%" border="1" cellpadding="0" cellspacing="0">

   <tr>
        <td>Name</td>
        <td>Product Code</td>
        <td>Quantity</td>
        <td>Sub Total</td>
   </tr>';

   while($obj=$results->fetch_object())
   {

    $product_name=$obj->product_name;
    $product_code=$obj->product_code;
    $qty=$obj->qty;
    $subtotal=$obj->sub_total;


$message .= '

    <tr>
        <td>'.$product_name.'</td>
        <td>'.$product_code.'</td>
        <td>'.$qty.'</td>
        <td>'.$subtotal.'</td>
    </tr>';
    }

$message .= '</table>';

echo $message;

$headers  = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= 'From: [email protected]'."\r\n";
$headers .= "Bcc: $bcc_mail\r\n";
mail($to, $subject, $message, $headers);

unset($_SESSION['products']);

?>

What I did here is just looping the tr which contains data from the db. I haven't tried it though but sure it'll work. Enjoy!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.