【发布时间】:2013-11-04 17:42:54
【问题描述】:
我需要让大家知道我正处于学习网络编程的最初阶段。我足够了解阅读代码并知道它在做什么。我正在使用课程、在线培训和“现实世界”培训进行学习。
我在尝试创建的联系表单时遇到问题。我工作的公司有几个使用联系表格的网站。我使用了在其他网站上使用的联系表单代码,但我们已经从这个新主机开始更改了我们的主机,他们使用 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 = "joshua.delph@heartlandfpg.com";
$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 进行了搜索,但到目前为止还没有找到任何有用的信息,并且我尝试了在网上找到的几种不同的解决方案。我真的很想弄清楚这一点。
我也对通过电子邮件提交 Web 表单的其他解决方案持开放态度。
我非常感谢任何人可以提供的任何帮助。我愿意通过教程或其他任何可以帮助我学习的东西。
谢谢!
【问题讨论】:
-
它会抛出任何错误,或者根本没有做任何事情吗?您发布的代码缺少来自
if(isset($_POST['email'])) {的右大括号,但这可能只是在发布中遗漏了。 -
它根本没有抛出任何错误。只需转到“空白”contact.php 页面。它不会发布到数据库或重定向到“谢谢”页面。这真的很奇怪。我们已经使用'echo'进行了测试,但无济于事。 'echo' 在第一个文本之后起作用,但是当我们添加其他代码时它不起作用。
-
那是臭气熏天的右括号。现在可以了!!!!你太棒了!!!!如果您将其添加为答案,我会将其标记为您已回答。
-
完成。很高兴它有帮助!作为提示,找到一个带有错误检查功能的 IDE (Eclipse/Aptana/NetBeans)。我用 Notepad++ 好几年了,它有折叠但不检查,但我错过了很多像这样的错误。
标签: php forms email web mysqli