PHP到SQL和电子邮件表单提交

时间:2017-08-18 08:41:04

标签: php sql forms email

我有一个可用的PHP表单,它从网站后端的表单中获取数据并将其插入适用的SQL数据库中。我的问题是,每当我尝试设置它以便它也将数据发送到电子邮件地址(以表格和电子邮件定义的电子邮件)时,我收到错误500.

代码如下:

<?php
/* Attempt MySQL server connection. Assuming you are running MySQL
server with default setting (user 'root' with no password) */
$link = mysqli_connect("#", "#", "#", "#");

// Check connection
if($link === false){
die("ERROR: Could not connect. " . mysqli_connect_error());
}

// Escape user inputs for security
$name = mysqli_real_escape_string($link, $_POST['name']);
$initials = mysqli_real_escape_string($link, $_POST['initials']);
$item = mysqli_real_escape_string($link, $_POST['item']);
$issue = mysqli_real_escape_string($link, $_POST['issue']);
$tel = mysqli_real_escape_string($link, $_POST['tel']);
$email = mysqli_real_escape_string($link, $_POST['email']);
$cost = mysqli_real_escape_string($link, $_POST['cost']);
$loggedby = mysqli_real_escape_string($link, $_POST['loggedby']);
$bag = mysqli_real_escape_string($link, $_POST['bag']);
$charger = mysqli_real_escape_string($link, $_POST['charger']);


// attempt insert query execution
$sql = "INSERT INTO `repairs` (`name`,`initials`,`item`, `issue`,     `telephone`,`email`, `cost`, `loggedby`, `bag`, `charger`) VALUES ('$name', '$initials', '$item', '$issue', '$tel', '$email', '$cost', '$loggedby', '$bag', '$charger')";
if(mysqli_query($link, $sql)){
header('location:repairs.php');
} else{
echo "ERROR: Could not execute $sql. " . mysqli_error($link);
}

$to = "emailaddress@emails.co.uk,$email"
$subject= "Thankyou $name - Your repair has been logged"
$body= "<h2>Thankyou $name</h2> /n
This is a confirmation email regarding your $item /n
Your repair log is below: /n
$item /n
$issue /n/n
<h3>Accessories</h3> /n
bag - $bag /n
charger - $charger /n/n
Your repair was logged by $loggedby on echo date("d-m-Y"); /n/n
We will contact you on $tel &amp; $email when your $item is ready to 
collect. 
/n/n
Loud Crowd IT - 01302 965482 /n
repairs@loudcrowdit.co.uk /n
www.loudcrowd.agency /n"

$mail ($to,$subject,$body);
// close connection
mysqli_close($link);
?>

有谁知道我收到错误的原因?

2 个答案:

答案 0 :(得分:0)

如何修复500内部服务器错误

500内部服务器错误是服务器端错误,这意味着问题可能与您的计算机或互联网连接无关,而是与网站的服务器有关。

虽然不太可能,但您可能会出现问题,在这种情况下,我们会看一些您可以尝试的事情:

  1. 重新加载网页。您可以通过单击刷新/重新加载按钮,按F5或Ctrl-R,或从地址栏再次尝试URL来执行此操作。

  2. 即使Web服务器出现500内部服务器错误,问题可能只是暂时的。再次尝试该页面通常会成功。

  3. 注意:如果在在线商家的结帐过程中出现500内部服务器错误消息,请注意重复尝试结帐可能最终会产生多个订单 - 以及多次收费!大多数商家都会从这些行为中获得自动保护,但仍需要牢记这一点。

    1. 清除浏览器的缓存。如果您正在查看的网页的缓存版本存在问题,则可能会导致HTTP 500问题。
    2. 注意:内部服务器错误通常不是由缓存问题引起的,但我偶尔会看到清除缓存后错误消失。尝试这是一件容易且无害的事情,所以不要跳过它。

      1. 删除浏览器的Cookie。通过删除与您收到错误的网站相关联的Cookie,可以纠正大约500个内部服务器错误问题。 删除cookie后,重新启动浏览器并再试一次。 而是作为504网关超时错误进行故障排除。 它不是很常见,但是有些服务器产生500内部服务器错误,而实际上基于问题原因的更合适的消息是504网关超时。

答案 1 :(得分:0)

内部服务器错误意味着服务器端脚本出现错误,在这种情况下:

$to = "emailaddress@emails.co.uk,$email" // missing a semicolon

上面缺少的分号会触发意外的&#39; $ subject&#39; (T_VARIABLE)

也在这里:

$subject= "Thankyou $name - Your repair has been logged"

这是你应该使用的代码:

<?php
/* Attempt MySQL server connection. Assuming you are running MySQL
server with default setting (user 'root' with no password) */
$link = mysqli_connect("#", "#", "#", "#");

// Check connection
if ($link === false) {
    die("ERROR: Could not connect. " . mysqli_connect_error());
}

// Escape user inputs for security
$name     = $_POST['name'];
$initials = $_POST['initials'];
$item     = $_POST['item'];
$issue    = $_POST['issue'];
$tel      = $_POST['tel'];
$email    = $_POST['email'];
$cost     = $_POST['cost'];
$loggedby = $_POST['loggedby'];
$bag      = $_POST['bag'];
$charger  = $_POST['charger'];


// attempt insert query execution
$sql = "INSERT INTO `repairs` (`name`,`initials`,`item`, `issue`, `telephone`,`email`, `cost`, `loggedby`, `bag`, `charger`) VALUES (?,?,?,?,?,?,?,?,?,?)";

$stmt = mysqli_prepare($link, $sql);
mysqli_stmt_bind_param($stmt, 'ssssssssss', $name, $initials, $item, $issue, $tel, $email, $cost, $loggedby, $bag, $charger);

if (mysqli_stmt_execute($stmt)) {

    $to      = "emailaddress@emails.co.uk,$email";
    $subject = "Thankyou $name - Your repair has been logged";
    $body    = "<h2>Thankyou $name</h2> /n
                This is a confirmation email regarding your $item /n
                Your repair log is below: /n
                $item /n
                $issue /n/n
                <h3>Accessories</h3> /n
                bag - $bag /n
                charger - $charger /n/n
                Your repair was logged by $loggedby on " . date("d-m-Y") . "/n/n
                We will contact you on $tel &amp; $email when your $item is ready to 
                collect. 
                /n/n
                Loud Crowd IT - 01302 965482 /n
                repairs@loudcrowdit.co.uk /n
                www.loudcrowd.agency /n";

    if (mail($to, $subject, $body)) {
        header('location:repairs.php');
    }
} else {
    printf("Error: %s.\n", mysqli_stmt_error($stmt));
}

mysqli_close($stmt);
// close connection
mysqli_close($link);
?>