Jump to content

How do you send several values using a FOR loop in an email


kpetsche20

Recommended Posts

Hello, I'm trying to make a shopping cart system where a persons adds a bunch of products to their cart and when they can to checkout the info is deleted from the database and then is sent in an email.

 

Right now it only sends 1 value in the email, regardless of how many products a person has in a table.

 

 

Here is a snippet of the mail code

 

<?php
if(isset($_POST['submit']))
{

///GET EMAIL
$sh = "SELECT * FROM users WHERE login = 'admin'";
$sh2 = mysql_query($sh);

$sql = "SELECT * FROM checkout WHERE username = '".$_SESSION['id']."'";
$run = mysql_query($sql);
for($x=0; $x < mysql_num_rows($run); $x++)
{
$data = mysql_fetch_array($run);
$data2 = mysql_fetch_array($sh2);


$body .= "
Name: ".$data['name']."
QTY: ".$data['qty']."
"; 

$to = $data2['email'];
mail($to, 'Subject', $body) or die(mysql_error());
echo "Quote Sent Successfully";



$delete = "DELETE FROM checkout WHERE username = '".$_SESSION['id']."'";
mysql_query($delete) or die(mysql_error);
} } 
else {
?>

Link to comment
Share on other sites

<?php
if(isset($_POST['submit']))
{

///GET EMAIL
$sh = "SELECT * FROM users WHERE login = 'admin'";
$sh2 = mysql_query($sh);
$body="";

$sql = "SELECT * FROM checkout WHERE username = '".$_SESSION['id']."'";
$run = mysql_query($sql);
$data2 = mysql_fetch_array($sh2);
$to = $data2['email'];

while ($_rows=mysql_fetch_array($_run)) {
$body .= "
Name: ".$data['name']."
QTY: ".$data['qty']."
";
}

mail($to, 'Subject', $body) or die(mysql_error());
echo "Quote Sent Successfully";

$delete = "DELETE FROM checkout WHERE username = '".$_SESSION['id']."'";
mysql_query($delete) or die(mysql_error);
}
?>

 

Maybe this will work?

Btw, I don't know why you have "or die(mysql_error);" next to the mail function, because I don't think it would cause a mysql error even if it had an error. D:

Link to comment
Share on other sites

I edited my post, try that again.

 

Lmfao sorry I'm kinda tired right now. Forgot toedit the variables. Try this:

 

<?php
if(isset($_POST['submit']))
{

///GET EMAIL
$sh = "SELECT * FROM users WHERE login = 'admin'";
$sh2 = mysql_query($sh);
$body="";

$sql = "SELECT * FROM checkout WHERE username = '".$_SESSION['id']."'";
$run = mysql_query($sql);
$data2 = mysql_fetch_array($sh2);
$to = $data2['email'];

while ($_rows=mysql_fetch_array($run)) {
$body .= "
Name: ".$_rows['name']."
QTY: ".$_rows['qty']."
";
}

mail($to, 'Subject', $body) or die(mysql_error());
echo "Quote Sent Successfully";

$delete = "DELETE FROM checkout WHERE username = '".$_SESSION['id']."'";
mysql_query($delete) or die(mysql_error);
}
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.