获取错误解析错误:语法错误,意外' $ _ GET' (T_VARIABLE)

时间:2015-08-24 04:44:16

标签: php mysql parse-error

我正在制作一个页面,在第1页我从mysql&获取数据点击名称我将重定向到另一页,即第2页,其中显示完整的详细信息。 请帮忙 ! 这是第1页的php代码     

            $host="localhost"; // Host name 
            $username="root"; // Mysql username 
            $password=""; // Mysql password 
            $db_name="testmra"; // Database name 
            // Connect to server and select databse.
            $conn=mysqli_connect($host,$username,$password) or die("cannot connect"); 
            mysqli_select_db($conn,$db_name);
            $result = mysqli_query($conn,"SELECT name,COUNT(status_id) AS Count from bookingdetails WHERE  YEAR(date) = YEAR(CURDATE()) AND MONTH(date) = MONTH(CURDATE()) AND status_id='2' GROUP BY name");
            echo "<table border='1' style='border:black;'>
                <tr>
                    <!--<th id='td'>Sr No.</th>-->
                    <th id='td'>Name</th>   
                    <th id='td'>No. of Bookings Done</th>
                </tr>";
            while($row = mysqli_fetch_array($result))
            {
                echo "<tr>";

                        echo "<td align='center' id='td'><a href='Details.php'>" . $row['name'] . "</a></td>";
                        echo "<td align='center' id='td'>" . $row['Count'] . "</td>";
                echo "</tr>";
            }
            echo "</table>";
            mysqli_close($conn);
        ?>

Page 2 
<?php

            $host="localhost"; // Host name 
            $username="root"; // Mysql username 
            $password=""; // Mysql password 
            $db_name="testmra"; // Database name 
            // Connect to server and select databse.
            $conn=mysqli_connect($host,$username,$password) or die("cannot connect"); 
            mysqli_select_db($conn,$db_name);
            $name=(varchar) $_GET['name'];
            $result = mysqli_query($conn,"SELECT * from bookingdetails WHERE  YEAR(date) = YEAR(CURDATE()) AND MONTH(date) = MONTH(CURDATE()) AND status_id='2' AND name='$name'");
            echo "<table border='1' style='border:black;'>
                <tr>
                    <th id='td'>Room</th>   
                    <th id='td'>Name</th>
                    <th id='td'>Purpose</th>
                    <th id='td'>Attendee</th>
                    <th id='td'>Date</th>
                    <th id='td'>Start Time</th>
                    <th id='td'>End Time</th>
                </tr>";
            while($row = mysqli_fetch_array($result))
            {
                echo "<tr>";

                        echo "<td align='center' id='td'>" . $row['room'] . "</td>";
                        echo "<td align='center' id='td'>" . $row['name'] . "</td>";
                        echo "<td align='center' id='td'>" . $row['purpose'] . "</td>";
                        echo "<td align='center' id='td'>" . $row['attendee'] . "</td>";
                        echo "<td align='center' id='td'>" . $row['date'] . "</td>";
                        echo "<td align='center' id='td'>" . $row['starttime'] . "</td>";
                        echo "<td align='center' id='td'>" . $row['endtime'] . "</td>";
                echo "</tr>";
            }
            echo "</table>";
            mysqli_close($conn);
        ?> 

4 个答案:

答案 0 :(得分:1)

没有必要进行类型转换$name=(varchar) $_GET['name'];

只需使用:$name= $_GET['name'];

您还需要在查询字符串中添加相同的参数

echo "<td align='center' id='td'><a href='Details.php?name=" . $row['name'] . "'>" . $row['name'] . "</a></td>";

答案 1 :(得分:1)

更改第1页中的以下行

 echo "<td align='center' id='td'><a href='Details.php'>" . $row['name'] . "</a></td>";

 echo "<td align='center' id='td'><a href='Details.php?name=" . $row['name'] . "'>" . $row['name'] . "</a></td>";

和第2页

(varchar) $_GET['name']

$_GET['name']

答案 2 :(得分:1)

您需要删除(varchar)类型转换。不需要这样做,varchar也是MySQL中的数据类型而不是PHP。

我会添加一些验证,以确保您的$_GET设置为防止未定义的索引错误,如果由于第一页中的URL错误而没有出现类型转换,则会抛出这些错误。

答案 3 :(得分:1)

您只需要检查是否设置了GET。你可以这样做

$name=isset($_GET['name'])? $_GET['name'] : '' ;
  

并检查您是否通过get方法在网址中传递name

如果您使用此链接进入第2页。

echo "<td align='center' id='td'><a href='Details.php'>" . $row['name'] . "</a></td>";

然后将其更改为。将名称传递为get方法

echo "<td align='center' id='td'><a href='Details.php?name=".$row['name']."'>" . $row['name'] . "</a></td>";

所以第2页的代码将

<?php

            $host="localhost"; // Host name 
            $username="root"; // Mysql username 
            $password=""; // Mysql password 
            $db_name="testmra"; // Database name 
            // Connect to server and select databse.
            $conn=mysqli_connect($host,$username,$password) or die("cannot connect"); 
            mysqli_select_db($conn,$db_name);
            $name=isset($_GET['name'])? $_GET['name'] : '' ;
            $result = mysqli_query($conn,"SELECT * from bookingdetails WHERE  YEAR(date) = YEAR(CURDATE()) AND MONTH(date) = MONTH(CURDATE()) AND status_id='2' AND name='$name'");
            echo "<table border='1' style='border:black;'>
                <tr>
                    <th id='td'>Room</th>   
                    <th id='td'>Name</th>
                    <th id='td'>Purpose</th>
                    <th id='td'>Attendee</th>
                    <th id='td'>Date</th>
                    <th id='td'>Start Time</th>
                    <th id='td'>End Time</th>
                </tr>";
            while($row = mysqli_fetch_array($result))
            {
                echo "<tr>";

                        echo "<td align='center' id='td'>" . $row['room'] . "</td>";
                        echo "<td align='center' id='td'>" . $row['name'] . "</td>";
                        echo "<td align='center' id='td'>" . $row['purpose'] . "</td>";
                        echo "<td align='center' id='td'>" . $row['attendee'] . "</td>";
                        echo "<td align='center' id='td'>" . $row['date'] . "</td>";
                        echo "<td align='center' id='td'>" . $row['starttime'] . "</td>";
                        echo "<td align='center' id='td'>" . $row['endtime'] . "</td>";
                echo "</tr>";
            }
            echo "</table>";
            mysqli_close($conn);
        ?> 

或者你应该把整个代码放在像

这样的条件下
if(isset($_GET['name']))
{
  // your code here 
}