在php中发布数组数据foreach循环

时间:2013-07-12 06:54:08

标签: php mysql database email

我正在尝试将数组数据插入数据库并且它正在运行,但问题是在向admin发送包含插入数据详细信息的邮件时。

我的代码:

if (isset($_POST['Submit']))
{
$var1 = $_POST['usr'];
$var2 = $_POST['category'];
$var3 = $_POST['prdname'];
$var4 = array_values(array_filter($_POST['prdid']));
$var5 = $_POST['price'];
$var6 = $_POST['qty'];
$var7 = $_POST['totals'];
}
$n = count($var4);

for ($i = 0; $i < $n; $i++)
{
$query = "INSERT INTO table (field1, field2, field3, field4, field5, field6, field7) 
VALUES ('$var1[$i]', '$var2[$i]', '$var3[$i]', '$var4[$i]', '$var5[$i]', '$var6[$i]', '$var7')"; 
$result = mysql_query($query) or die('Failed to connect to server: ' . mysql_error());
}

    foreach($var4 as $key => $v)
    {
    $v1 = $v['category'];
    $v2 = $v['prdid'];
    $v3 = $v['prdname'];
    $v4 =  $v['qty'];
    $v5 =  $v['price'];
$html = "
Specimen Copy Request Submitted By: <strong>$user</strong>
<p>Category       : $v1</p>
<p>Product Id     : $v2 </p>
<p>Products       : $v3 </p>
<p>Quantity       : v4 </p>
<p>Price      : $v5 </p>
";  
$crlf = "\n";
$mime = new Mail_mime($crlf);
$mime->setHTMLBody($html);
$body = $mime->get();
$headers = $mime->headers($headers);

$mail = $smtp->send($recipients, $headers, $body);
}

1 个答案:

答案 0 :(得分:0)

像这样尝试这个块

foreach($var4 as $v)
    {
    $v1 = $v['category'];
    $v2 = $v['prdid'];
    $v3 = $v['prdname'];
    $v4 =  $v['qty'];
    $v5 =  $v['price'];
$html = "
Specimen Copy Request Submitted By: <strong>$user</strong>
<p>Category       : $v1</p>
<p>Product Id     : $v2 </p>
<p>Products       : $v3 </p>
<p>Quantity       : v4 </p>
<p>Price      : $v5 </p>
";