解析错误:语法错误,意外的“其他”(T_ELSE)

时间:2014-12-17 07:36:05

标签: php mysql

我从表单中获取一个名称并在数据库中找到它然后将其分配给会话以在下一页上获取结果:

<?php //session
include "../private/session.php" ?>
<?php //database connection
include "../private/database.php" ?>
<?php //function
include "../private/function.php" ?>
<?php 
if (mysqli_connect_errno()) {
    # code...
    die ("database failed:" .
        mysqli_connect_error() . "(" . mysqli_connect_errno(). ")" 
    );
}

$searchname = $_POST['searchname'];
if(isset($searchname))
{
    $searchname = strtolower($searchname);

    //check username in db
    $results = mysqli_query($connection,"SELECT * FROM  doctors WHERE name='$searchname'");
    while ($idexsist = mysqli_fetch_assoc($results)) 
    {
        $_SESSION['s_name'] = $idexsist['name'];
        $_SESSION['s_splt'] = $idexsist['specilist'];
        $_SESSION['s_phone'] = $idexsist['phone'];
        $_SESSION['s_address'] = $idexsist['address'];
        redirect_to("admin_panel.php");
    } 
    else
    {
        die("cannot find the data " . mysqli_error());
        redirect_to("admin_panel.php");
    }
}
?>

3 个答案:

答案 0 :(得分:3)

在经过一段时间后,你遗失了}

更正后的代码:

...
while ($idexsist = mysqli_fetch_assoc($results)) 
{
 $_SESSION['s_name'] = $idexsist['name'];
  $_SESSION['s_splt'] = $idexsist['specilist'];
 $_SESSION['s_phone'] = $idexsist['phone'];
 $_SESSION['s_address'] = $idexsist['address'];
 redirect_to("admin_panel.php");
  } 
} // this was missing.
 else
 {
...

答案 1 :(得分:0)

如果您要正确格式化代码,可能会发现自己:

<?php
$searchname = $_POST['searchname'];
if(isset($searchname))
{
    $searchname = strtolower($searchname);
    //check username in db
    $results = mysqli_query($connection,"SELECT * FROM  doctors WHERE name='$searchname'");
    while ($idexsist = mysqli_fetch_assoc($results)) 
    {
        $_SESSION['s_name'] = $idexsist['name'];
        $_SESSION['s_splt'] = $idexsist['specilist'];
        $_SESSION['s_phone'] = $idexsist['phone'];
        $_SESSION['s_address'] = $idexsist['address'];
        redirect_to("admin_panel.php");
    } 
    else
    {
        die("cannot find the data " . mysqli_error());
        redirect_to("admin_panel.php");
    }
}
?>

应该是:

<?php
$searchname = $_POST['searchname'];
if(isset($searchname))
{
    $searchname = strtolower($searchname);
    //check username in db
    $results = mysqli_query($connection,"SELECT * FROM  doctors WHERE name='$searchname'");
    while ($idexsist = mysqli_fetch_assoc($results)) 
    {
        $_SESSION['s_name'] = $idexsist['name'];
        $_SESSION['s_splt'] = $idexsist['specilist'];
        $_SESSION['s_phone'] = $idexsist['phone'];
        $_SESSION['s_address'] = $idexsist['address'];
        redirect_to("admin_panel.php");
    }
}
else
{
    die("cannot find the data " . mysqli_error());
    redirect_to("admin_panel.php");
}
?>

答案 2 :(得分:0)

试试这个:

您将}放在错误的位置。

 $searchname = $_POST['searchname'];
 if(isset($searchname))
 {
$searchname = strtolower($searchname);

//check username in db
$results = mysqli_query($connection,"SELECT * FROM  doctors WHERE name='$searchname'");

 while ($idexsist = mysqli_fetch_assoc($results)) 
{
 $_SESSION['s_name'] = $idexsist['name'];
  $_SESSION['s_splt'] = $idexsist['specilist'];
 $_SESSION['s_phone'] = $idexsist['phone'];
 $_SESSION['s_address'] = $idexsist['address'];
 redirect_to("admin_panel.php");
  }
} 
 else
 {
 die("cannot find the data " . mysqli_error());
redirect_to("admin_panel.php");
}
 ?>