Click here to Skip to main content
15,886,518 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
HI, I have this error : Parse error: syntax error, unexpected '' (T_ENCAPSED_AND_WHITESPACE), expecting '-' or identifier (T_STRING) or variable (T_VARIABLE) or number (T_NUM_STRING) in C:\wamp64\www\... I don't how to fix . Please help me. Thanks

My code
<!-- download.php file -->
<?php

// Get the file ID from the URL
$filename = $_GET['id'];

// Query the database for the file record
$sql = "SELECT filename FROM pdf_data where id=$id;
$result = mysqli_query($conn, $sql);

// Check if a result was returned
if (mysqli_num_rows($result) > 0) {
  // Fetch the file name and path
  $row = mysqli_fetch_assoc($result);
  $filename = $row['filename']; //line15
  $file_path = "pdf/" . $filename;

  // Send the file to the user's browser for download
  header('Content-Type: application/octet-stream');
  header('Content-Disposition: attachment; filename="' . $filename . '"');
  readfile($file_path);
} else {
  // Display an error message if the file was not found
  echo "File not found.";
}
?>


What I have tried:

I want to create a users download pdf file
Posted
Updated 17-Feb-23 8:56am
Comments
Member 15627495 17-Feb-23 13:29pm    
$sql = "SELECT filename FROM pdf_data where id=$id;

need to be close string + var
$sql = "SELECT filename FROM pdf_data where id=" . $id;

1 solution

You don't close the double quotes:
PHP
$sql = "SELECT filename FROM pdf_data where id=$id;
Try:
PHP
$sql = "SELECT filename FROM pdf_data where id=$id";
But ... Never concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Always use Parameterized queries instead.

When you concatenate strings, you cause problems because SQL receives commands like:
SQL
SELECT * FROM MyTable WHERE StreetAddress = 'Baker's Wood'
The quote the user added terminates the string as far as SQL is concerned and you get problems. But it could be worse. If I come along and type this instead: "x';DROP TABLE MyTable;--" Then SQL receives a very different command:
SQL
SELECT * FROM MyTable WHERE StreetAddress = 'x';DROP TABLE MyTable;--'
Which SQL sees as three separate commands:
SQL
SELECT * FROM MyTable WHERE StreetAddress = 'x';
A perfectly valid SELECT
SQL
DROP TABLE MyTable;
A perfectly valid "delete the table" command
SQL
--'
And everything else is a comment.
So it does: selects any matching rows, deletes the table from the DB, and ignores anything else.

So ALWAYS use parameterized queries! Or be prepared to restore your DB from backup frequently. You do take backups regularly, don't you?
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900