我需要讓大家都知道我處於學習Web編程的最初階段。我足夠理解,閱讀代碼並知道它在做什麼。我正在學習課程,在線培訓和「現實世界」培訓。PHP MySQLi電子郵件表格
我遇到了一個我嘗試創建的聯繫表單的問題。我工作的公司有幾個使用聯繫表單的網站。我使用了在其他網站上工作過的聯繫人表單代碼,但是我們已經改變了我們的主機,從這個新主機開始,他們使用MySQLi而不是MySQL。當我使用下面的代碼時,它發佈到數據庫並按預期工作。
<?php
//hake yachts contact
//connection
$con = mysqli_connect("localhost","hakeyachtcontact","hakeyachtcontact","hakeyachtcontact");
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
//insert
$sql="INSERT INTO hakeyachtcontact (firstName, lastName, email, boatModel)
VALUES
('$_POST[firstName]','$_POST[lastName]','$_POST[email]','$_POST[boatModel]')";
if (!mysqli_query($con,$sql))
{
die('Error: ' . mysqli_error($con));
}
mysqli_close($con);
?>
現在...當我添加以下代碼,電子郵件不會發送和以前的功能,工作(發佈數據到數據庫)停止工作。
//email
if(isset($_POST['email'])) {
// EDIT THE 2 LINES BELOW AS REQUIRED
$email_to = "[email protected]";
$email_subject = "Hake Yachts Website Contact";
function died($error) {
// your error code can go here
echo "We are very sorry, but there were error(s) found with the form you submitted. ";
echo "These errors appear below.<br /><br />";
echo $error."<br /><br />";
echo "Please go back and fix these errors.<br /><br />";
die();
}
// validation expected data exists
if(!isset($_POST['firstName']) ||
!isset($_POST['lastName']) ||
!isset($_POST['email']) ||
!isset($_POST['boatModel'])) {
died('We are sorry, but there appears to be a problem with the form you submitted.');
}
$firstName = $_POST['firstName']; // required
$lastName = $_POST['lastName'];// required
$email = $_POST['email'];// required
$boatModel = $_POST['boatModel'];// required
$error_message = "";
if (!eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $email)){
$error_message .= 'The email you entered does not appear to be valid.<br />';
}
$string_exp = "^[a-z .'-]+$";
if(!eregi($string_exp,$firstName)) {
$error_message .= 'The name you entered does not appear to be valid.<br />';
}
$string_exp = "^[a-z .'-]+$";
if(!eregi($string_exp,$lastName)) {
$error_message .= 'The name you entered does not appear to be valid.<br />';
}
if(strlen($error_message) > 0) {
died($error_message);
}
$email_message = "Form details below.\n\n";
function clean_string($string) {
$bad = array("content-type","bcc:","to:","cc:","href");
return str_replace($bad,"",$string);
}
$email_message .= "Name: ".clean_string($firstName)."\n";
$email_message .= "email: ".clean_string($lastName)."\n";
$email_message .= "phone: ".clean_string($email)."\n";
$email_message .= "comments: ".clean_string($boatModel)."\n";
// create email headers
$headers = 'From: '.$email_from."\r\n".
'Reply-To: '.$email_from."\r\n" .
'X-Mailer: PHP/' . phpversion();
@mail($email_to, $email_subject, $email_message, $headers);
//send
header("Location: thankyou.html");
我暗自懷疑,這是由於MySQL和庫MySQLi之間的差異,但我不相信是這樣的。我使用Google進行了搜索,但沒有找到任何有助於達到這一點的內容,並嘗試了幾種我在網上找到的解決方案。試圖弄清楚這一點我真的很茫然。
我也接受其他通過電子郵件提交網頁表單的解決方案。
我真的很感謝任何人都可以提供的幫助。我願意接受教程或其他能夠幫助我學習的內容。
謝謝!
它會拋出任何錯誤,或者做任何事情嗎?您發佈的代碼缺少來自'if(isset($ _ POST ['email'])){'的結束大括號,但這可能剛剛在發佈中停止。 – aynber
這不是完全拋出任何錯誤。只需進入「空白」contact.php頁面即可。它不會發布到數據庫或重定向到「謝謝」頁面。這真的很奇怪。我們測試過使用'echo'無濟於事。 'echo'在第一位文本之後起作用,但當我們添加其他代碼時它不起作用。 –
這是發臭缺少的左括號。它現在正常運作!!!!!你真令人驚歎!如果您將其添加爲答案,我會將其標記爲由您回答。 –