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 {
?>

<?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:

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);
}
?>

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.