如何修复 PHP 中的“语法错误,文件意外结束”错误?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28953362/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 00:09:46  来源:igfitidea点击:

How to fix "syntax error, unexpected end of file" error in PHP?

phpsyntax-error

提问by Abhilash Hande

I get this error and have tried all steps to eliminate it but can't, have also tried the steps demonstrated on stack overflow but to no avail: error:

我收到此错误并尝试了所有步骤来消除它但不能,还尝试了堆栈溢出中演示的步骤但无济于事:错误:

Parse error: syntax error, unexpected end of file in C:\wamp\www\project-Feedback form\project docs\login.php on line 80

解析错误:语法错误,第 80 行 C:\wamp\www\project-Feedback form\project docs\login.php 中的文件意外结束

I have attached the entire snippet of code:

我附上了整个代码片段:

<?php
session_start(); // Starting Session
$error=''; // Variable To Store Error Message
?>

<html>
 <head>
  <title>Login Page</title>
  <link rel="stylesheet" type="text/css" href="login.css"> 

 </head>
 <body>
  <div id="header">
  <center><p><img src="http://www.ruparel.edu/_/rsrc/1338008090424/config/customLogo.gif?revision=29" align="left" /> <img id="image" src="http://www.auplod.com/u/pudlao4d2f7.png"  /> </p></center>
  
  </div>
  <hr></hr>
  <center><h1> Welcome to the Login page for <img src="http://www.auplod.com/u/uoalpd4d31a.png" /> </h1></center>
  <center><h2>Please Login!</h2></center>
 ` <br />
  
  <form method="post" action=" ">
  <div id="radio"><b><img src="https://encrypted-tbn2.gstatic.com/images?q=tbn:ANd9GcQMfjN-cd00MPhHqoKOKnUHp-VP3HSU3FBuuNDJbJL2yQF0fyWR"/>Student<input type="radio" name="radio1" value="Student"></p>
  <b><img src="https://encrypted-tbn2.gstatic.com/images?q=tbn:ANd9GcSSnk6-xJ7aCZL8aODuWyRMAENzk7XIFitACfPJALdJOaTzQq1b-w" />Professor<input type="radio" name="radio1" value="Professor"></b> </div>
  <div id="fields"><b>ROll no:<input type="text" name="username" ></b>
  <b>Password:<input type="password" name="pwd"></b><b>&nbsp;<input type="submit" name="submit" value="Login"></b></div>
  
  </form>
  
 <?php
  if (isset($_POST['submit'])) 
  {
  if (empty($_POST['username']) || empty($_POST['password'])) 
  {
  $error = "Username or Password is invalid";
  }
  else
  {
   // Define $username and $password
  $username=$_POST['username'];
  $password=$_POST['password'];
  // Establishing Connection with Server by passing server_name, user_id and password as a parameter
  $connection = mysqli_connect("localhost", "root", "");
  // To protect MySQL injection for Security purpose
  $username = stripslashes($username);
  $password = stripslashes($password);
  $username = mysqli_real_escape_string($username);
  $password = mysqli_real_escape_string($password);
  // Selecting Database
  $db = mysqli_select_db($connection,"feedback data");

  // SQL query to fetch information of registerd users and finds user match.
  $query = mysqli_query($connection,"select * from login where password='$password' AND username='$username'");
  $rows = mysqli_num_rows($query);
  if ($rows == 1) {
  $_SESSION['login_user']=$username; // Initializing Session
  //now we write php code to decide if student or teacher

  $subject=$_SESSION['login_user'];
  $pattern="/[2$]/";
  if($_POST['radio1']=="Professor"&& preg_match($pattern,$subject)==1)//condition for teacher using BRE
  {
   header("location: http://localhost/project-Feedback%20form/project%20docs/selection_page_teacher.php");
  }
  elseif($_POST['radio1']=="Student"&& preg_match($pattern,$subject)==0)//condition for student using BRE
  {
   header("location: http://localhost/project-Feedback%20form/project%20docs/selection_page_student.php");
  }
  else
  {
   echo"The selection you have made is incorrect Mr.".$_SESSION['login_user'];
  }
  mysql_close($connection); // Closing Connection
  }
  }
  
 ?>
 </body>
</html>

回答by Micael Dias

You're missing a closing brace }for one of your conditional statements, being for this one:

}的其中一个条件语句缺少一个右大括号,对于这个条件语句:

if (isset($_POST['submit']))