试图获取要链接的ID以进入下一页

时间:2011-05-27 12:07:30

标签: php

我正在尝试从此代码中获取链接中的数据并收到错误,如下所示,代码第3行可能出现什么问题?

while ($row = mysql_fetch_assoc($result))
    {
        echo "<a href=\"edit_employee.php?$row['employee_id_passport']\">" . $row['first_name'] ." " . $row['surname'] . "</a>";

        echo "<br />";
    }

错误是

Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in /Library/WebServer/Documents/practice/employee/view_employee.php on line 14 

3 个答案:

答案 0 :(得分:5)

删除单引号:

echo "<a href=\"edit_employee.php?$row[employee_id_passport]\">" . $row['first_name'] ." " . $row['surname'] . "</a>";

答案 1 :(得分:3)

AFAIK,您有2种方法可以解决您的请求:

echo "<a href=\"edit_employee.php?{$row['employee_id_passport']}\">" . $row['first_name'] ." " . $row['surname'] . "</a>";

echo "<a href=\"edit_employee.php?" . $row['employee_id_passport'] . "\">" . $row['first_name'] ." " . $row['surname'] . "</a>";

或使用单引号以避免转义双引号

echo '<a href="edit_employee.php?' . $row['employee_id_passport'] . '">' . $row['first_name'] . ' ' . $row['surname'] . '</a>';

使用$row[employee_id_passport]会在错误日志中生成通知,因为employee_id_passport被解释为常量。

首先看一下manual

答案 2 :(得分:2)

您可以删除单引号或使用{}大括号,例如

{$row['employee_id_passport']}

查看变量解析手册:

http://www.php.net/manual/en/language.types.string.php#language.types.string.parsing.complex