0

I have a script I'm building around a PHP transaction to update a couple of tables and insert into another simultaneously and then forward some posted form data on an email and I'm really struggling to figure out the error here. It's a rotator to identify the next in a queue to receive a lead and then update the records and email details over to a user.

The function seems to work, it does update the various tables and insert a new record into the leads database, and it does email over the lead details, but it throws a 500 internal server error, I have php.ini reporting enabled and working (received plenty of error reports on other scripts for this site) but nothing is being logged. I suspect it is something to do with the Select statement that includes a JOIN and several WHERE arguments.


<?php 
// Include config file
require_once ($_SERVER["DOCUMENT_ROOT"] . "/partners/includes/config.php");
$foo = $_POST['test'];
$type = $_POST['enquiry_type'];
$firstname = $_POST['first_name'];
$secondname = $_POST['second_name'];
$age = $_POST['applicant_age'];
$number = $_POST['telephone_number'];
$email = $_POST['email_address'];
$value = $_POST['house_value'];
$loan = $_POST['loan_amount'];
$homeowner = $_POST['homeowner_status'];
$credit = $_POST['credit_history'];
$time = $_POST['enquiry_time'];
// Mail header removal
function remove_headers($string) { 
 $headers = array(
 "/to\:/i",
 "/from\:/i",
 "/bcc\:/i",
 "/cc\:/i",
 "/Content\-Transfer\-Encoding\:/i",
 "/Content\-Type\:/i",
 "/Mime\-Version\:/i" 
 ); 
 $string = preg_replace($headers, '', $string);
 return strip_tags($string);
} 
// Pick up the cleaned form data
$foo = remove_headers($_POST['test']);
$type = remove_headers($_POST['enquiry_type']);
$firstname = remove_headers($_POST['first_name']);
$secondname = remove_headers($_POST['second_name']);
$age = remove_headers($_POST['applicant_age']);
$number = remove_headers($_POST['telephone_number']);
$email = remove_headers($_POST['email_address']);
$value = remove_headers($_POST['house_value']);
$loan = remove_headers($_POST['loan_amount']);
$homeowner = remove_headers($_POST['homeowner_status']);
$credit = remove_headers($_POST['credit_history']);
$time = remove_headers($_POST['enquiry_time']);
$date = date('y-m-d h:i:s', time());
$to = "";
//Test hidden form field for content, assumed spam if completed. 
if (empty($foo)) {
 
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
// Find users that are switched on, and the firm switched on, then brings the one logged on the longest, and who has waited the most for a lead
$sql_find_email = "SELECT users.username, users.id, users.time_last_lead, users.firm_fca, firms.credit_number FROM users JOIN firms ON firms.main_fca = users.firm_fca WHERE users.switched_on='1' AND firms.credit_number >=10 AND firms.switched_on_firm='1' ORDER BY users.time_last_lead LIMIT 1"; 
//Find the next lead recipient
$find_email_result = mysqli_query($link, $sql_find_email);
 
$email_to_use = mysqli_fetch_array($find_email_result);
 
//FCA number of the users firm to enable us to link the charge to the firm 
 
$firm_fca = $email_to_use['firm_fca'];
 
//Amount of leads credit that firm has remaining 
 
$firm_credit = $email_to_use['credit_number'];
 
$username = $email_to_use['username'];
 
//Check if the email array has rows, and if not, forward to Rightmortgageadice.co.uk. Update user with timestamp for last lead issue
if(mysqli_num_rows($find_email_result) > 0){
 
 $user_id = $email_to_use['id'];
 
 //Timestamp user record to enable auto rotation
 
 $sql_add_lead_timestamp = "UPDATE users SET time_last_lead=? WHERE id =?";
 
 $sql_update_firm_credit = "UPDATE firms SET credit_number =? WHERE main_fca=?";
 
 //Create a lead record for invoices
 
 $sql_add_lead_record = "INSERT INTO leads (user_id, firm_fca, lead_type, first_name, second_name, age, tel_number, email_address, house_value, loan_amount, home_owner, credit_history, response_time, time_stamp) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
 mysqli_begin_transaction($link);
 try {
 $stmt = mysqli_prepare($link, $sql_add_lead_record);
 
 mysqli_stmt_bind_param($stmt, "iisssissiissss", $param_user_id, $param_firm_fca, $param_lead_type, $param_first_name, $param_second_name, $param_age, $param_tel_number, $param_email_address, $param_house_value, $param_loan_amount, $param_home_owner, $param_credit_history, $param_response_time, $param_time_stamp );
 
 // Set parameters
 
 $param_user_id = $user_id;
 $param_firm_fca = $firm_fca;
 $param_firm_credit = ($firm_credit)-10;
 $param_lead_type = trim($_POST["enquiry_type"]);
 $param_first_name = trim($_POST["first_name"]);
 $param_second_name = trim($_POST["second_name"]);
 $param_age = trim($_POST["applicant_age"]);
 $param_tel_number = trim($_POST["telephone_number"]);
 $param_email_address = trim($_POST["email_address"]);
 $param_house_value = trim($_POST["house_value"]);
 $param_loan_amount = trim($_POST["loan_amount"]);
 $param_home_owner = trim($_POST["homeowner_status"]);
 $param_credit_history = trim($_POST["credit_history"]);
 $param_response_time = trim($_POST["enquiry_time"]);
 $param_time_stamp = $date;
 $stmt2 = mysqli_prepare($link, $sql_add_lead_timestamp);
 mysqli_stmt_bind_param($stmt2, "ss", $param_time_stamp, $param_user_id);
 $stmt3 = mysqli_prepare($link, $sql_update_firm_credit);
 mysqli_stmt_bind_param($stmt3, "ii", $param_firm_credit, $param_firm_fca);
 // Execute
 mysqli_stmt_execute($stmt);
 mysqli_stmt_execute($stmt2);
 mysqli_stmt_execute($stmt3);
 // set the advisers email if statements execute correctly
 $to = '[email protected]';
 mysqli_commit($link);
 } catch (mysqli_sql_exception $exception) {
 mysqli_rollback($link);
 throw $exception;
 } 
}
else {
$to = '[email protected]' ;
}
 
// Build the email (replace the address in the $to section with your own) 
$subject = "New message AMENDED: $firstname $number";
$message = "
$type
$username
$firstname
$secondname
$number
$age
$email
$value
$loan
$homeowner
$credit
$time
$firm_fca
$firm_credit
";
$headers = "From:[email protected]";
// Send the mail using PHPs mail() function
mail($to, $subject, $message, $headers);
mysqli_free_result($find_email_result, $stmt);
mysqli_close($link);
// Redirect
header("location: https://www.google.com");
}
//Foo is not empty, must be spam 
else if (!empty($foo)) {
header("location: https://www.yahoo.com"); 
}
?>

I was expecting to get an error report that would give me a clue as to where this is going wrong, but I just get an internal server error warning and cannot figure out why.

PHP ini is set up and working. The address for the error report is obviously correct on the actual file itself

error_reporting = E_ALL;
log_errors = On;
display_errors = On;
error_log = /php-errors.log; 
Dharman
34k27 gold badges106 silver badges158 bronze badges
asked Mar 7, 2025 at 17:44
16
  • 1
    A 500 could also be due to bad server config. Check if you can execute this code on CLI without any issues. Also put a breakpoint on the first line and see if the debugger starts at all. Commented Mar 7, 2025 at 18:09
  • 1
    Are you sure you were editing the right INI file? Did you restart the server after changing the file? Commented Mar 7, 2025 at 18:13
  • 1
    "I suspect it is something to do with the Select statement that includes a JOIN and several WHERE arguments." Why not check it? You could remove it and see if you get a different output. Commented Mar 7, 2025 at 18:14
  • 1
    Try the line $date = date('y-m-d h:i:s', time()); wrapped this way: error_log($date = date('y-m-d h:i:s', time())); If it logs, the error is afterwards, if it does not log, the error is earlier. Rinse and repeat. Right now I don't see you log anything in your script, despite you say you alreay managed to activate logging. But then you actually need to log as well (and apropos 500: The webservers log should also have some diagnostics,) Commented Mar 7, 2025 at 18:52
  • 1
    The error log should probably be in /tmp or /var/log or something similar. Starting from / means it will try to write to the root directory, which the server may not be allowed to do Commented Mar 7, 2025 at 18:53

0

Know someone who can answer? Share a link to this question via email, Twitter, or Facebook.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.