Gatura
Gatura

Reputation: 605

Trying to get the id to link to take to the next page

I am trying to get data from the link from this code and get an error as show below, what could be the problem with line 3 of the code?

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

        echo "<br />";
    }

The error is

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 

Upvotes: 3

Views: 128

Answers (3)

Toto
Toto

Reputation: 91375

AFAIK, you have 2 ways to resolve your request :

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

or

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

or using single quote to avoid escaping double quote

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

Using $row[employee_id_passport] will produce a notice in error-log because employee_id_passport is interpreted as a constant.

Have a look at the manual , first Note.

Upvotes: 3

Andreas
Andreas

Reputation: 5335

You can remove the single quotes or use { } braces e.g.

{$row['employee_id_passport']}

Check the manual on variable parsing:

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

Upvotes: 2

Saul
Saul

Reputation: 18041

Remove single quotes:

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

Upvotes: 5

Related Questions