syntax error, unexpected token "<"

HyperOsmar

Member
Dear colleagues,

I'm inserting the code below into a form, using the PHP plugin with the "End of form submission (onAfterProcess) option:

<?php
require_once 'dompdf/autoload.inc.php';
use Dompdf\Dompdf;

// Connection to the MySQL database
$conn = new mysqli('localhost', 'xxxxxx', 'xxxxxx', 'xxxxxx');

// Check if the connection was successful
if ($conn->connect_error) {
die("Failed to connect to database: " . $conn->connect_error);
}

// Retrieve user data from database
$sql = "SELECT * FROM users WHERE id = 1";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
$row = $result->fetch_assoc();

// Define user information in variables
$name = $row['cod_cnes'];
$email = $row['establishment_name'];
$phone = $row['mun_name'];

// Create a new Dompdf object
$dompdf = new Dompdf();

// Defines the HTML content of the PDF file
$html = '<h1>User Data</h1>';
$html .= '<p><strong>Municipality Code:</strong> ' . $name . '</p>';
$html .= '<p><strong>Establishment Name:</strong> ' . $email . '</p>';
$html .= '<p><strong>County Name:</strong> ' . $phone . '</p>';

// Load HTML content into the Dompdf object
$dompdf->loadHtml($html);

// Render the HTML content in a PDF file
$dompdf->render();

// Set password protection for the PDF file
$password = '123456';

// Protect the PDF file with the defined password
$dompdf->getCanvas()->get_cpdf()->setEncryption($password, $password);

// Defines the name of the generated PDF file
$filename = 'user_data.pdf';

// Send the generated PDF file to the browser
$dompdf->stream($filename, array('Attachment' => 0));
} else {
echo "No user found in database.";
}

// Close the database connection
$conn->close();
?>

and the following error appears:

0
syntax error, unexpected token "<"

Can someone help me???...
 
Check in your browser console in the network tab for the underlying warning/error etc.

If it's coming from a warning or notice it may be gone if you set J! error reporting to none.
 
@troester, thanks for the feedback...

I accessed the browser console... and, honestly, I didn't quite understand your explanation...
An observation, the form is filled out by a visitor and not by a registered user... could that be the problem???...

"$sql = "SELECT * FROM users WHERE id = 1";
$result = $conn->query($sql);

.
 
I'm not a coding specialist and may be I'm wrong, but I assumed the Joomla users table has usually some kind of prefix: xyz_users. So the query $sql = "SELECT * FROM users WHERE id = 1"; wouldn't work (except Joomla would handle this kind of query).

Is the id always 1??

In addition I can't see a kind of error handling in this piece of code:
if ($result->num_rows > 0) {
$row = $result->fetch_assoc();
What is happening if the result->num_rows is 0 ?

Edit: just realized that the pdf-creation is within curly brackets, so only for a registered user...basically this code shouldn't be run for visitors...(and therefore no unexpected token should be thrown)
 
Last edited:
We are in need of some funding.
More details.

Thank you.

Members online

Back
Top