【问题标题】:Issue with curly brackets php大括号php的问题
【发布时间】:2013-10-04 13:39:45
【问题描述】:

我有一个注册页面来注册学生,但我的页面一直反映我的大括号有错误

解析错误:语法错误,第 65 行 C:\Program Files\EasyPHP-5.3.3\www\Enrollment_System-www.webdevelopplus.com\enrolsys\registerwrite.php 中出现意外的“}”

来源

<?php   
    $username =$_POST["newname"];
    $password =$_POST["newpass"];
    $cpassword =$_POST["conpass"];
    $firstname =$_POST["firstName"];
    $lastname =$_POST["lastName"];
    $others =$_POST["others"];
    $email =$_POST["email"];
    $phone =$_POST["phone"];
    $sex =$_POST["sex"];
    $bg =$_POST["bg"];
    $genotype =$_POST["genotype"];
    $dob =$_POST["dob"];
    $address =$_POST["address"];
    $state =$_POST["state"];
    $lga =$_POST["lga"];
    $nationality =$_POST["nationality"];
    $sq =$_POST["sq"];
    $sa =$_POST["sa"];
    $time =$_POST["time"];
    $day =$_POST["day"];
?>

<?php

if ($password !==$cpassword){
    echo "<div align='center'><img src='./images/progress_med2.gif'>";
    include ("./error1.php");
    echo "<center>Sorry, but the password you provided did not match.<p><a href='javascript:window.history.go(-1)'<img src='./images/goback.jpg'></a>";
  include("./error2.php");
exit;
}

if (eregi("^[a-zA-Z0-9_]+@[a-zA-Z0-9\-]+\.[a-zA-Z0-9\-\.]+$]", $email)){
  echo "<div align='center'><img src='./images/progress_med2.gif'>";
    include ("./error1.php");
    echo "<center>Sorry, but your email address is not valid.<p><a href='javascript:window.history.go(-1)'<img src='./images/goback.jpg'></a>";
  include("./error2.php");
exit;
}
?>

<?php
include("./DB/config.php");

        $query = "SELECT * FROM members WHERE Username='$username'";
        $results = mysql_query($query);

        if(mysql_num_rows($results) > 0) {
        include("./error1.php");
        echo "<center><img src ='./images/userexist.png'>Sorry, but the .$username. you have chosen is already in existence.<P><a href='javascript:window.history.go(-1)'><img src='./images/goback.jpg'></a>";
        include("./error2.php");
    } else {
        $query = "SELECT * FROM members WHERE email='$email'";
        $results = mysql_query($query);

        if(mysql_num_rows($results) > 0) {
        include("./error1.php");
        echo "<center><img src ='./images/userexist.png'>Sorry, but the .$email. you have chosen is already in existence.<P><a href='javascript:window.history.go(-1)'><img src='./images/goback.jpg'></a>";
        include("./error2.php");
    } else {    
        $SQL1 = "Insert into members(username,Firstname,Lastname,Others,Email,Phone,Sex,Blood_Group,Genotype,Date_Of_Birth,Address,State,LGA,Nationality,Security_Question,Security_Answer,createTime,createDate) Values ('','$username','$firstname','$lastname','$others','$email','$phone','$sex','$bg','$genotype','$dob','$address','$state','$lga','$nationality','$sq','$sa','$time','$day')";
        $results1 = mysql_query($SQL1) or die(mysql_error());
        header("Location: ./register_preview.php")
    }
       }

?>

我应该如何在 if 和 else 语句中对齐这些 {}

【问题讨论】:

  • 您的脚本容易受到 SQL 注入和跨站点脚本的攻击。
  • Gumbo 是对的,您必须确保正确消毒它。

标签: php


【解决方案1】:

把整个代码改成这个;

<?php   
    $username =$_POST["newname"];
    $password =$_POST["newpass"];
    $cpassword =$_POST["conpass"];
    $firstname =$_POST["firstName"];
    $lastname =$_POST["lastName"];
    $others =$_POST["others"];
    $email =$_POST["email"];
    $phone =$_POST["phone"];
    $sex =$_POST["sex"];
    $bg =$_POST["bg"];
    $genotype =$_POST["genotype"];
    $dob =$_POST["dob"];
    $address =$_POST["address"];
    $state =$_POST["state"];
    $lga =$_POST["lga"];
    $nationality =$_POST["nationality"];
    $sq =$_POST["sq"];
    $sa =$_POST["sa"];
    $time =$_POST["time"];
    $day =$_POST["day"];
?>

<?php

if ($password !==$cpassword)
{
    echo "<div align='center'><img src='./images/progress_med2.gif'>";
    include ("./error1.php");
    echo "<center>Sorry, but the password you provided did not match.<p><a href='javascript:window.history.go(-1)'<img src='./images/goback.jpg'></a>";
  include("./error2.php");
exit;
}

if (eregi("^[a-zA-Z0-9_]+@[a-zA-Z0-9\-]+\.[a-zA-Z0-9\-\.]+$]", $email))
{
  echo "<div align='center'><img src='./images/progress_med2.gif'>";
    include ("./error1.php");
    echo "<center>Sorry, but your email address is not valid.<p><a href='javascript:window.history.go(-1)'<img src='./images/goback.jpg'></a>";
  include("./error2.php");
exit;
}
?>

<?php
include("./DB/config.php");

    if () // define what to check?
    {
        $query = "SELECT * FROM members WHERE Username='$username'";
        $results = mysql_query($query);

        if(mysql_num_rows($results) > 0) 
        {
        include("./error1.php");
        echo "<center><img src ='./images/userexist.png'>Sorry, but the .$username. you have chosen is already in existence.<P><a href='javascript:window.history.go(-1)'><img src='./images/goback.jpg'></a>";
        include("./error2.php");
        } 
        else 
        {
        $query = "SELECT * FROM members WHERE email='$email'";
        $results = mysql_query($query);

            if(mysql_num_rows($results) > 0) 
            {
                include("./error1.php");
                echo "<center><img src ='./images/userexist.png'>Sorry, but the .$email. you have chosen is already in existence.<P><a href='javascript:window.history.go(-1)'><img src='./images/goback.jpg'></a>";
                include("./error2.php");
            } 
            else 
            {    
                $SQL1 = "Insert into members(username,Firstname,Lastname,Others,Email,Phone,Sex,Blood_Group,Genotype,Date_Of_Birth,Address,State,LGA,Nationality,Security_Question,Security_Answer,createTime,createDate) Values ('','$username','$firstname','$lastname','$others','$email','$phone','$sex','$bg','$genotype','$dob','$address','$state','$lga','$nationality','$sq','$sa','$time','$day')";
                $results1 = mysql_query($SQL1) or die(mysql_error());
                header("Location: ./register_preview.php")
            }
        }
    }
?>

确保你已经在 if() 检查中定义了变量

【讨论】:

  • 删除最后一个括号会使您的答案错误。除此之外,它和我的答案一样......
  • 感谢 imran 现在工作
【解决方案2】:

删除最后一个“}”。

在这些 php 标记中没有打开该块。当它告诉您在第 65 行有一个不需要的 } 时,请始终首先尝试转到第 65 行并将其删除:)

另外,我认为您的意思是实际上有一个与该括号匹配的“if”,因为它是缩进的。也许您不小心删除了它?

错误是有原因的。

【讨论】:

  • 不,最后一个括号是有原因的。格式不好,所以你看不到。
【解决方案3】:

这两行之间缺少一个 if 语句

include("./DB/config.php");

        $query = "SELECT * FROM members WHERE Username='$username'";

【讨论】:

    猜你喜欢
    • 2019-06-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多