I understand that this question is asked many times and I searched very much about that error but I didn't find anything useful on this site and in internet. Can you help me to find out my error?
"Parse error: syntax error, unexpected end of file in C:\wamp\www\Project2\members\download.php on line 31"
Here is my download.php file:
<html>
<body>
<title>Download your friend's uploads</title>
<?php
session_start();
$username =$_SESSION["uname"];
?>
<?php
$con = mysqli_connect("localhost", "root", "", "webpage");
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
$query = "SELECT imagesnotes,username From userdata where sharedpeople='$username";
$res = mysqli_query($con,$query);
$res or die('Error, query failed');
if(mysqli_num_rows($res) == 0){
echo "<br>Database is empty </br>";
}
else{
while(list($id,$name) = mysql_fetch_array($res)){
echo '<br><a href="download.php?id=<?php=$id;?>"><?php=$name;?></a></br>';
}
}
mysqli_close($con);
?>
</body>
</html>
I'm thinking about 1 hour and I couldn't find the solution for error.
In the following line
$query = "SELECT imagesnotes,username From userdata where sharedpeople='$username";
remove the '
or better use
$query = "SELECT imagesnotes,username From userdata where sharedpeople='$username'";
to escape it correctly.
You still have to read this: How can I prevent SQL injection in PHP?
In the following line
if (mysqli_connect_errno()) {
remove the {
or use
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
And maybe add an exit after the error message.
There is no matching }
for this line:
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
The entire body of your program then takes place inside the if
that is meant to handle errors.
If you indented your code, you would find these bugs easier to locate.
Missing Closing }
Modified Code:
<html>
<body>
<title>Download your friend's uploads</title>
<?php
session_start();
$username =$_SESSION["uname"];
?>
<?php
$con = mysqli_connect("localhost", "root", "", "webpage");
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
$query = "SELECT imagesnotes,username From userdata where sharedpeople='$username";
$res = mysqli_query($con,$query);
$res or die('Error, query failed');
if(mysqli_num_rows($res) == 0)
{
echo "<br>Database is empty </br>";
}
else
{
while(list($id,$name) = mysql_fetch_array($res))
{
echo '<br><a href="download.php?id=<?php=$id;?>"><?php=$name;?></a></br>';
}
}}
mysqli_close($con);
?>
</html>