-4

I have a working PHP form that is taking data from a form in the backend of the website and inserting it into the SQL database applicable. My issue is, whenever I try and set it up so that it also send the data to an email address ( email captured in form & email defined ) I recieve Error 500.

Code is below:

<?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);
?>

Does anyone know why I receive the error?

V5Nathan
  • 21
  • 1
  • 9

2 Answers2

0

How to Fix the 500 Internal Server Error

500 Internal Server Error is a server-side error, meaning the problem probably isn't with your computer or internet connection but instead with the website's server.

While not probable, it is possible that there's something wrong on your end, in which case we'll look at some things you can try:

  1. Reload the web page. You can do that by clicking the refresh/reload button, pressing F5 or Ctrl-R, or trying the URL again from the address bar.

  2. Even if the 500 Internal Server Error is a problem on the web server, the issue might just be temporary. Trying the page again will often be successful.

Note: If the 500 Internal Server Error message appears during the checkout process at an online merchant, be aware that duplicate attempts to checkout may end up creating multiple orders - and multiple charges! Most merchants have automatic protections from these kinds of actions, but it's still something to keep in mind.

  1. Clear your browser's cache. If there's a problem with the cached version of the page you're viewing, it could be causing HTTP 500 issues.

Note: Internal Server Errors are not often caused by caching issues, but I have, on occasion, seen the error go away after clearing the cache. It's such an easy and harmless thing to try, so don't skip it.

  1. Delete your browser's cookies. Some 500 Internal Server Error issues can be corrected by deleting the cookies associated with the site you're getting the error on. After removing the cookie(s), restart the browser and try again. Troubleshoot as a 504 Gateway Timeout error instead. It's not very common, but some servers produce a 500 Internal Server Error when in reality a more appropriate message based on the cause of the problem, is 504 Gateway Timeout.
Avid
  • 171
  • 16
0

Internal server error means there's an error in your server side script, in this case :

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

The missing semicolon above triggers Unexpected '$subject' (T_VARIABLE)

also here :

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

this is the code you should use :

<?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);
?>
Masivuye Cokile
  • 4,754
  • 3
  • 19
  • 34
  • Thankyou for the great help, that now works to a degree, the only issue I get is where I add $email for the clients email address to be in the to field too. It doesn't send to their email aswell as the one specified – V5Nathan Aug 18 '17 at 13:17
  • if the answer did help with you problem plz vote and accept the answer @V5Nathan – Masivuye Cokile Aug 18 '17 at 13:48