Deployed Application to Live Server not working properly?

I recently deployed a new application to a local Ubuntu server, but the program is missing a lot of functionality that was originally coded in the scripts.

The program was originally built in XAMPP but was deployed to Linux.

This import functionality is a good example.

<?php

include('dbconnect.php');

if(isset($_POST['btn_upload'])){
	$filename = $_FILES['file']['tmp_name'];
	$file = fopen($filename,"r");


	// Read and throw away the header row
	fgetcsv($file);

	$stmt = $pdo->prepare("INSERT INTO invoices (id, Invoice_Number, Customer_Number, Customer_Name, Invoice_Date, Sales_Order_Number, Sales_Amount, Progress_Billing, Extra_Charge_Amount, Tax_Amount, Invoice_Amount, Writeoff_Amount, Paid_Amount, Balance_Due, Use_Tax, Tax_Stat, Salesperson_1, Comm_Rate_1, Salesperson_2, Comm_Rate_2, Salesperson_3, Comm_Rate_3, Deposit_Applied, Sales_Tax_Codes, Sales_Tax_Amounts, Customer_Address1, Customer_Address2, Customer_City, Customer_State, Customer_Zip, Customer_Country) VALUES (:id, :Invoice_Number, :Customer_Number, :Customer_Name, :Invoice_Date, :Sales_Order_Number, :Sales_Amount, :Progress_Billing, :Extra_Charge_Amount, :Tax_Amount, :Invoice_Amount, :Writeoff_Amount, :Paid_Amount, :Balance_Due, :Use_Tax, :Tax_Stat, :Salesperson_1, :Comm_Rate_1, :Salesperson_2, :Comm_Rate_2, :Salesperson_3, :Comm_Rate_3, :Deposit_Applied, :Sales_Tax_Codes, :Sales_Tax_Amounts, :Customer_Address1, :Customer_Address2, :Customer_City, :Customer_State, :Customer_Zip, :Customer_Country)");

	while (! feof($file)) {
  		$row = fgetcsv($file);
			if($row[0] == NULL) {
				continue;
			}
			$stmt->bindParam(':id', $row[0]);
			$stmt->bindParam(':Invoice_Number', $row[1]);
		$stmt->bindParam(':Customer_Number', $row[2]);
		$stmt->bindParam(':Customer_Name', $row[3]);
		$date = date_create($row[4]);
		$dfstr = date_format($date, 'Y-m-d');
		$stmt->bindParam(':Invoice_Date', $dfstr);
  		$stmt->bindParam(':Sales_Order_Number', $row[5]);
  		$stmt->bindParam(':Sales_Amount', $row[6]);
  		$stmt->bindParam(':Progress_Billing', $row[7]);
  		$stmt->bindParam(':Extra_Charge_Amount', $row[8]);
  		$stmt->bindParam(':Tax_Amount', $row[9]);
  		$stmt->bindParam(':Invoice_Amount', $row[10]);
  		$stmt->bindParam(':Writeoff_Amount', $row[11]);
  		$stmt->bindParam(':Paid_Amount', $row[12]);
  		$stmt->bindParam(':Balance_Due', $row[13]);
			$stmt->bindParam(':Use_Tax', $row[14]);
			$stmt->bindParam(':Tax_Stat', $row[15]);
			$stmt->bindParam(':Salesperson_1', $row[16]);
			$stmt->bindParam(':Comm_Rate_1', $row[17]);
			$stmt->bindParam(':Salesperson_2', $row[18]);
			$stmt->bindParam(':Comm_Rate_2', $row[19]);
			$stmt->bindParam(':Salesperson_3', $row[20]);
			$stmt->bindParam(':Comm_Rate_3', $row[21]);
			$stmt->bindParam(':Deposit_Applied', $row[22]);
			$stmt->bindParam(':Sales_Tax_Codes', $row[23]);
			$stmt->bindParam(':Sales_Tax_Amounts', $row[24]);
			$stmt->bindParam(':Customer_Address1', $row[25]);
			$stmt->bindParam(':Customer_Address2', $row[26]);
			$stmt->bindParam(':Customer_City', $row[27]);
			$stmt->bindParam(':Customer_State', $row[28]);
			$stmt->bindParam(':Customer_Zip', $row[29]);
			$stmt->bindParam(':Customer_Country', $row[30]);
  		$stmt->execute();

}

	fclose($file);
	header("Location: ../invoices_list.php");
}

if(isset($_POST['btn_back'])) {
	header("Location: ../invoices_list.php");
}

?>


When I run the code through XAMPP, it works properly and imports the spreadsheet. However, when I do the same thing on the local server, I get an HTTP 500 error which tells me nothing.

I tried enabling PHP errors in the php.ini file, and they do not work. Iā€™ve changed the dbconnect file to have the correct password to connect to the database. I know it is working (somehow) because I can create and remove new users when I need to on the local server, but I canā€™t make reports or import new data files.

What do I do to get the code working again?

Are you using the same versions of PHP and MySQL on both systems?

Here is what XAMPP shows:

JMyrtle@JEREMY-DELL c:\xampp
# httpd -v
Server version: Apache/2.4.41 (Win64)
Apache Lounge VC15 Server built:   Aug 11 2019 12:20:04

JMyrtle@JEREMY-DELL c:\xampp
# php -v
PHP 7.4.3 (cli) (built: Feb 18 2020 17:29:46) ( ZTS Visual C++ 2017 x64 )
Copyright (c) The PHP Group
Zend Engine v3.4.0, Copyright (c) Zend Technologies

JMyrtle@JEREMY-DELL c:\xampp
# mysql --version
mysql  Ver 15.1 Distrib 10.4.11-MariaDB, for Win64 (AMD64), source revision 7c2c420b70b19cc02b5281127205e876f3919dad

and here is what the server shows:

image

Try ensuring that error reporting has not been set to zero and also display errors to the screen:

<?php 
error_reporting(-1);
ini_set(ā€œdisplay_errorsā€, ā€œtrueā€);

// your script goes here

It still comes back as an HTTP 500 error in Chrome. Firefox will show a blank white page.

If you resort to adding

echo "here";
exit();

in your code in appropriate places, at what point does it start giving the 500 error? Once you can see how far the code gets before it dies, that might point to why itā€™s working differently.

A quick google suggests it also might be a permissions thing on the php script itself, does it have permission to run?

This is where it stops after trying this:

<?php

include('dbconnect.php');


if(isset($_POST['btn_upload'])){
	$filename = $_FILES['file']['tmp_name'];
	$file = fopen($filename,"r");

	// Read and throw away the header row
	fgetcsv($file);

	$stmt = $pdo->prepare("INSERT INTO invoices (id, Invoice_Number, Customer_Number, Customer_Name, Invoice_Date, Sales_Order_Number, Sales_Amount, Progress_Billing, Extra_Charge_Amount, Tax_Amount, Invoice_Amount, Writeoff_Amount, Paid_Amount, Balance_Due, Use_Tax, Tax_Stat, Salesperson_1, Comm_Rate_1, Salesperson_2, Comm_Rate_2, Salesperson_3, Comm_Rate_3, Deposit_Applied, Sales_Tax_Codes, Sales_Tax_Amounts, Customer_Address1, Customer_Address2, Customer_City, Customer_State, Customer_Zip, Customer_Country) VALUES (:id, :Invoice_Number, :Customer_Number, :Customer_Name, :Invoice_Date, :Sales_Order_Number, :Sales_Amount, :Progress_Billing, :Extra_Charge_Amount, :Tax_Amount, :Invoice_Amount, :Writeoff_Amount, :Paid_Amount, :Balance_Due, :Use_Tax, :Tax_Stat, :Salesperson_1, :Comm_Rate_1, :Salesperson_2, :Comm_Rate_2, :Salesperson_3, :Comm_Rate_3, :Deposit_Applied, :Sales_Tax_Codes, :Sales_Tax_Amounts, :Customer_Address1, :Customer_Address2, :Customer_City, :Customer_State, :Customer_Zip, :Customer_Country)");

	while (! feof($file)) {
  		$row = fgetcsv($file);
			if($row[0] == NULL) {
				continue;
			}
			$stmt->bindParam(':id', $row[0]);
			$stmt->bindParam(':Invoice_Number', $row[1]);
		$stmt->bindParam(':Customer_Number', $row[2]);
		$stmt->bindParam(':Customer_Name', $row[3]);
		$date = date_create($row[4]);
		$dfstr = date_format($date, 'Y-m-d');
		$stmt->bindParam(':Invoice_Date', $dfstr);
  		$stmt->bindParam(':Sales_Order_Number', $row[5]);
  		$stmt->bindParam(':Sales_Amount', $row[6]);
  		$stmt->bindParam(':Progress_Billing', $row[7]);
  		$stmt->bindParam(':Extra_Charge_Amount', $row[8]);
  		$stmt->bindParam(':Tax_Amount', $row[9]);
  		$stmt->bindParam(':Invoice_Amount', $row[10]);
  		$stmt->bindParam(':Writeoff_Amount', $row[11]);
  		$stmt->bindParam(':Paid_Amount', $row[12]);
  		$stmt->bindParam(':Balance_Due', $row[13]);
			$stmt->bindParam(':Use_Tax', $row[14]);
			$stmt->bindParam(':Tax_Stat', $row[15]);
			$stmt->bindParam(':Salesperson_1', $row[16]);
			$stmt->bindParam(':Comm_Rate_1', $row[17]);
			$stmt->bindParam(':Salesperson_2', $row[18]);
			$stmt->bindParam(':Comm_Rate_2', $row[19]);
			$stmt->bindParam(':Salesperson_3', $row[20]);
			$stmt->bindParam(':Comm_Rate_3', $row[21]);
			$stmt->bindParam(':Deposit_Applied', $row[22]);
			$stmt->bindParam(':Sales_Tax_Codes', $row[23]);
			$stmt->bindParam(':Sales_Tax_Amounts', $row[24]);
			$stmt->bindParam(':Customer_Address1', $row[25]);
			$stmt->bindParam(':Customer_Address2', $row[26]);
			$stmt->bindParam(':Customer_City', $row[27]);
			$stmt->bindParam(':Customer_State', $row[28]);
			$stmt->bindParam(':Customer_Zip', $row[29]);
			$stmt->bindParam(':Customer_Country', $row[30]);
  		$stmt->execute();

}
echo 'here';
exit();

	fclose($file);
	header("Location: ../invoices_list.php");
}

if(isset($_POST['btn_back'])) {
	header("Location: ../invoices_list.php");
}

?>

Iā€™m not sure what other permissions I need to grant. The invoice directory has 775 permissions on it.

image

So it executes the query, or doesnā€™t execute the query?

I know virtually nothing about Unixy permissions so I canā€™t help there. In any case, if itā€™s running part of the script, then it canā€™t be a permissions issue.

I donā€™t think the query is running.

After the query is listed, it works.

$stmt = $pdo->prepare("INSERT INTO invoices (id, Invoice_Number, Customer_Number,
 Customer_Name, Invoice_Date, Sales_Order_Number, Sales_Amount, Progress_Billing,
 Extra_Charge_Amount, Tax_Amount, Invoice_Amount, Writeoff_Amount, Paid_Amount,
 Balance_Due, Use_Tax, Tax_Stat, Salesperson_1, Comm_Rate_1, Salesperson_2, 
Comm_Rate_2, Salesperson_3, Comm_Rate_3, Deposit_Applied, Sales_Tax_Codes, 
Sales_Tax_Amounts, Customer_Address1, Customer_Address2, Customer_City, 
Customer_State, Customer_Zip, Customer_Country) 
VALUES (:id, :Invoice_Number, :Customer_Number, :Customer_Name, :Invoice_Date, 
:Sales_Order_Number, :Sales_Amount, :Progress_Billing, :Extra_Charge_Amount, 
:Tax_Amount, :Invoice_Amount, :Writeoff_Amount, :Paid_Amount, :Balance_Due, 
:Use_Tax, :Tax_Stat, :Salesperson_1, :Comm_Rate_1, :Salesperson_2, :Comm_Rate_2, 
:Salesperson_3, :Comm_Rate_3, :Deposit_Applied, :Sales_Tax_Codes, 
:Sales_Tax_Amounts, :Customer_Address1, :Customer_Address2, :Customer_City, 
:Customer_State, :Customer_Zip, :Customer_Country)");

But after the while loop and the execute statement, it fails.

	while (! feof($file)) {
  		$row = fgetcsv($file);
			if($row[0] == NULL) {
				continue;
			}
			$stmt->bindParam(':id', $row[0]);
			$stmt->bindParam(':Invoice_Number', $row[1]);
		$stmt->bindParam(':Customer_Number', $row[2]);
		$stmt->bindParam(':Customer_Name', $row[3]);
		$date = date_create($row[4]);
		$dfstr = date_format($date, 'Y-m-d');
		$stmt->bindParam(':Invoice_Date', $dfstr);
  		$stmt->bindParam(':Sales_Order_Number', $row[5]);
  		$stmt->bindParam(':Sales_Amount', $row[6]);
  		$stmt->bindParam(':Progress_Billing', $row[7]);
  		$stmt->bindParam(':Extra_Charge_Amount', $row[8]);
  		$stmt->bindParam(':Tax_Amount', $row[9]);
  		$stmt->bindParam(':Invoice_Amount', $row[10]);
  		$stmt->bindParam(':Writeoff_Amount', $row[11]);
  		$stmt->bindParam(':Paid_Amount', $row[12]);
  		$stmt->bindParam(':Balance_Due', $row[13]);
			$stmt->bindParam(':Use_Tax', $row[14]);
			$stmt->bindParam(':Tax_Stat', $row[15]);
			$stmt->bindParam(':Salesperson_1', $row[16]);
			$stmt->bindParam(':Comm_Rate_1', $row[17]);
			$stmt->bindParam(':Salesperson_2', $row[18]);
			$stmt->bindParam(':Comm_Rate_2', $row[19]);
			$stmt->bindParam(':Salesperson_3', $row[20]);
			$stmt->bindParam(':Comm_Rate_3', $row[21]);
			$stmt->bindParam(':Deposit_Applied', $row[22]);
			$stmt->bindParam(':Sales_Tax_Codes', $row[23]);
			$stmt->bindParam(':Sales_Tax_Amounts', $row[24]);
			$stmt->bindParam(':Customer_Address1', $row[25]);
			$stmt->bindParam(':Customer_Address2', $row[26]);
			$stmt->bindParam(':Customer_City', $row[27]);
			$stmt->bindParam(':Customer_State', $row[28]);
			$stmt->bindParam(':Customer_Zip', $row[29]);
			$stmt->bindParam(':Customer_Country', $row[30]);
  		$stmt->execute();

}

Does it make a difference if the server is running MariaDB or MySQL? I think XAMPP now uses MariaDB instead of MySQL and maybe thatā€™s what Iā€™ve been coding to all alongā€¦

I donā€™t know what the differences are between the two. When I last installed WAMP, the default port is connecting to MariaDB, and I have to specify a port to connect to MySQL. If I donā€™t do that, I get all sorts of permission problems when trying to access the database, because I havenā€™t set any of that up on Maria. What does execute() return?

I am not sure. Does var_dump() help with that?

A 500 error means the server itself is misconfigured. Not directly related to PHP. Doubtful your PHP files are executing at all. Might want to go step by step through a ubuntu guide and see what it says about permissions and such. Be aware that your web user is not going to be the same as your ssh user.

At the very least, start small and get:

# info.php
<?php phpinfo(); ?>

Working from a web browser.

I did that. phpinfo(); is working within the directory.

So that is a start. Now check the permssions on the directory to where files are upload to. You can determine the directory by looking at $filename. The just do a `chmod 777 tmpā€™ where tmp is the temporary directlry.

Perhaps wrap ā€˜$stmt->execute();ā€˜ inside a try/catch block and echo the failed query.

If you change this line

$stmt->execute();

to read

$ret = $stmt->execute();

you can echo or var_dump $ret and see what it says. Or as @John_Betong said, enable PDO exceptions and stick it in a try/catch block.

I wondered about that as all sorts of things can cause a 500, but then the OP added some debugging echo statements and said that they worked until they were moved to after the query execution line.

PHP code can definitely generate 500 errors, especially if itā€™s reaching for an external resource and failing to get it (repeated timeouts on outbound requests can cause a 500 from the server).

All of these options display no results. It will display an HTTP 500 error in Chrome or a blank white page in Firefox.

But how could it fail? Iā€™ve imported the database file in the database and the PHP code has the queries in it. I can make users and login, but I canā€™t import files or make reports and Iā€™ve edited every database connection file in my code that I can think of.

Does the user that you use to connect to the database have permission to run inserts? I would have expected a different error, but then I donā€™t know MariaDB.

I ran this code when I installed MariaDB:

GRANT ALL ON *.* TO 'user'@'localhost' IDENTIFIED BY 'password'

I donā€™t see why it wouldnā€™t have permissions to run queries? Itā€™s using the root credentials in the dbconnect files