2015-04-07 100 views
-1

我正在提交一個表格,將記錄插入到數據庫中,然後將電子郵件發送到指定的地址。下面的代碼不適合我。我收到錯誤。HTML電子郵件不起作用

if (isset($_POST['submit'])) { 

//if (
// !empty($_POST['item_cid']) && 
// !empty($_POST['item_code']) && 
// !empty($_POST['item_name']) && 
// !empty($_POST['item_price']) && 
// !empty($_POST['item_qty']) && 

// is_array($_POST['item_cid']) && 
// is_array($_POST['item_code']) && 
// is_array($_POST['item_name']) && 
// is_array($_POST['item_price']) && 
// is_array($_POST['item_qty']) && 
// count($_POST['item_cid']) === count($_POST['item_code']) 
// ) 

//{ 

foreach($_POST['item_cid'] as $key => $value) { 
//Data for Orders Table 
    $cid = mysqli_real_escape_string($connection,$value); 
    $pcode = mysqli_real_escape_string($connection,$_POST['item_code'][$key]); 
    $pname = mysqli_real_escape_string($connection,$_POST['item_name'][$key]); 
    $pprice = mysqli_real_escape_string($connection,$_POST['item_price'][$key]); 
    $pqty = mysqli_real_escape_string($connection,$_POST['item_qty'][$key]); 

//Data for Customers Table 
    $cname = mysqli_real_escape_string($connection,$_POST['item_cname'][$key]); 
    $cemail = mysqli_real_escape_string($connection,$_POST['item_cemail'][$key]); 
    $cphone = mysqli_real_escape_string($connection,$_POST['item_cphone'][$key]); 
    $caddress = mysqli_real_escape_string($connection,$_POST['item_caddress'][$key]); 
    $ctotal = mysqli_real_escape_string($connection,$_POST['item_ctotal'][$key]); 


// $sql = "INSERT INTO orders (cid, ordprod_code, ordprod_name, ordprod_price, ordprod_qty) VALUES ('$value', '$pcode', '$pname', '$pprice', '$pqty')"; 
// $sql2 = "INSERT INTO customers (cid, cname, cemail, cphone, caddress, ctotal) VALUES ('$value','$cname','$cemail','$cphone','$caddress','$ctotal')"; 
    if ($connection->query($sql) === TRUE) { 
     echo "Orders record created successfully \n"; 
    } 
//  } else { 
//  echo "Error: " . $sql . "<br>" . $connection->error; 
//  } 

    if ($connection->query($sql2) === TRUE) { 
     echo "Customers record created successfully \n"; 
    } 
// } else { 
//  echo "Error: " . $sql2 . "<br>" . $connection->error; 
    } // close the loop 

//******************************** 
// START EMAIL FUNCTION 
//******************************** 

// PREPARE THE BODY OF THE MESSAGE 

$message = '<html><body>'; 
$message .= '<img src="http://example.com/static/images/emailhead.jpg" alt="OMREL JEWELRY" />'; 
$message .= '<h3>Customer Information:</h3>'; 
$message .= '<table rules="all" style="border-color: #666;" cellpadding="10">'; 
$message .= '<tr><td><strong>Name:</strong></td><td>'. strip_tags($_POST['item_cname']) .'</td></tr>'; 
$message .= '<tr><td><strong>Email:</strong></td><td>'. strip_tags($_POST['item_cemail']) .'</td></tr>'; 
$message .= '<tr><td><strong>Phone:</strong></td><td>'. strip_tags($_POST['item_cphone']) .'</td></tr>'; 
$message .= '<tr><td><strong>Address:</strong> </td><td>'. strip_tags($_POST['item_caddress']) .'</td></tr>'; 
$message .= '</table>'; 
$message .= '</body></html>'; 

// MAKE SURE THE "FROM" EMAIL ADDRESS DOESN'T HAVE ANY NASTY STUFF IN IT 
$pattern = "/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/i"; 
    if (preg_match($pattern, $_POST['item_cemail'])) { 
     $cleanedFrom = $_POST['item_cemail']; 
    } else { 
    return "The email address you entered was invalid. Please try again!"; 
    } 

// CHANGE THE BELOW VARIABLES TO YOUR NEEDS 
$to = '[email protected]'; 
$subject = 'New order Arrived CustomerID #'.$cid.' '; 
$headers = "From: " . $cleanedFrom . "\r\n"; 
$headers .= "Reply-To: ".strip_tags($_POST['item_ceamil']) ."\r\n"; 
$headers .= "MIME-Version: 1.0\r\n"; 
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n"; 

if (mail($to, $subject, $message, $headers)) { 
echo 'Your order has been sent. Our sales department will contact you soon...'; 
} else { 
echo 'There was a problem sending the email.'; 
} 

print_r($_POST['item_cname']); 


} // Data Inserted & Emailed Close IF Statement 

session_destroy(); 

的HTML

<p class="form-row"> 
     <label class="" for="item_cname[]">Your Name <span class="required">*</span></lable> 
     <input class="input-text" type="text" name="item_cname[]" placeholder="Your Name" /> 
     </p> 
<p class="form-row"> 
     <label class="" for="item_cemail[]">Email Address <span class="required">*</span></lable> 
     <input type="text" name="item_cemail[]" placeholder="Your Email Address"/> 
     </p> 
<p class="form-row"> 
     <label class="" for="item_cphone[]">Phone Number <span class="required">*</span></lable> 
     <input type="text" name="item_cphone[]" placeholder="Your Phone Number"/> 
     </p> 
<p class="form-row"> 
     <label class="" for="item_caddress[]">Address <span class="required">*</span></lable> 
     <textarea name="item_caddress[]" placeholder="Your Address" class="input-text" rows="2" cols="2" maxlength="140"></textarea> 
     </p> 

的錯誤

說明:未定義變量:SQL在/home/public_html/dev/process.php 在線路48上

警告:mysqli: :查詢():在 /home/public_html/dev/process.php空查詢在線路48上

說明:未定義變量:在SQL2 /home/public_html/dev/process.php 上線55

警告:mysqli的::查詢():在線路中 /home/public_html/dev/process.php空查詢55

警告:用strip_tags()預計參數1是字符串, /家庭給定陣列/第72行的public_html/dev/process.php

警告:strip_tags()期望p arameter 1是串,陣列中 /home/public_html/dev/process.php給出上線73

警告:用strip_tags()預計參數1是字符串, /家庭給定陣列/的public_html的/ dev/process.php上線74

警告:用strip_tags()預計參數1是串,陣列中 /home/public_html/dev/process.php給出上線75

警告:的preg_match()預計參數2爲字符串,數組在 /home/public_html/dev/process.php在線81

所有那些指向strip_tags($_POST['item_*'])的錯誤(*表示項目的名稱)。

如何解決這個問題?

+1

如果你告訴我們,這將有助於什麼線/它用strip_tags()的是造成這個錯誤,因爲你沒有向我們展示了什麼/你在哪裏設置$ _POST數據,我們不知道它們包含什麼。 – Epodax

+1

當數組的字符串不是字符串時,您正在使用'strip_tags()'這種方式將'$ _POST'添加到'strip_tags($ _ POST ['item_cname'])''中。 –

+1

你只需要檢查張貼的值是否爲空並且是字符串,因爲錯誤清楚地表明strip_tags函數僅需要字符串 – Puneet

回答

1

您是否正在尋找。如果是的話,這應該爲你工作:

<?php 
if (isset($_POST['submit'])) { 
foreach($_POST['item_cid'] as $key => $value) { 
//Data for Orders Table 
$cid = mysqli_real_escape_string($connection,$value); 
$pcode = mysqli_real_escape_string($connection,$_POST['item_code'][$key]); 
$pname = mysqli_real_escape_string($connection,$_POST['item_name'][$key]); 
$pprice = mysqli_real_escape_string($connection,$_POST['item_price'][$key]); 
$pqty = mysqli_real_escape_string($connection,$_POST['item_qty'][$key]); 

//Data for Customers Table 
$cname = mysqli_real_escape_string($connection,$_POST['item_cname'][$key]); 
$cemail = mysqli_real_escape_string($connection,$_POST['item_cemail'][$key]); 
$cphone = mysqli_real_escape_string($connection,$_POST['item_cphone'][$key]); 
$caddress = mysqli_real_escape_string($connection,$_POST['item_caddress'][$key]); 
$ctotal = mysqli_real_escape_string($connection,$_POST['item_ctotal'][$key]); 

if ($connection->query($sql) === TRUE) { 
echo "Orders record created successfully \n"; 
} 

if ($connection->query($sql2) === TRUE) { 
echo "Customers record created successfully \n"; 
} 
} 

$message = '<html><body>'; 
$message .= '<img src="http://example.com/static/images/emailhead.jpg" alt="OMREL JEWELRY" />'; 
$message .= '<h3>Customer Information:</h3>'; 
$message .= '<table rules="all" style="border-color: #666;" cellpadding="10">'; 
$message .= '<tr><td><strong>Name:</strong></td><td>'. $cname .'</td></tr>'; 
$message .= '<tr><td><strong>Email:</strong></td><td>'. $cemail .'</td></tr>'; 
$message .= '<tr><td><strong>Phone:</strong></td><td>'. $cphone .'</td></tr>'; 
$message .= '<tr><td><strong>Address:</strong></td><td>'. $ctotal .'</td></tr>'; 
$message .= '</table>'; 
$message .= '</body></html>'; 

$pattern = "/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/i"; 
if (preg_match($pattern, $cemail)) { 
$cleanedFrom = $cemail; 
} else { 
return "The email address you entered was invalid. Please try again!"; 
} 

$to = '[email protected]'; 
$subject = 'New order Arrived CustomerID #'.$cid.' '; 
$headers = "From: " . $cleanedFrom . "\r\n"; 
$headers .= "Reply-To: ".strip_tags($_POST['item_ceamil']) ."\r\n"; 
$headers .= "MIME-Version: 1.0\r\n"; 
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n"; 

if (mail($to, $subject, $message, $headers)) { 
echo 'Your order has been sent. Our sales department will contact you soon...'; 
} else { 
echo 'There was a problem sending the email.'; 
} 

print_r($_POST['item_cname']); 


} // Data Inserted & Emailed Close IF Statement 

session_destroy(); 
?> 
+1

這已經解決了我的問題:) – yaqoob

+1

非常感謝你... – yaqoob

+1

你非常歡迎yaqoob。很高興我能幫助你。 :) @yaqoob –

0

可能是您的發佈數據之一是數組對象。 嘗試使print_r($ _ POST [xxx])檢查您的POST值。 Strip Tags函數參數不應該是一個數組對象。