2013-04-10 128 views
-1

我目前有這個行代碼的問題:PHP錨問題

echo "<td>"<a href="userdetails.php?".$row[username].">View Details</a></td>"; 

基本上上創建頁面,我希望鏈接成爲格式化爲userdetails.php?USERNAME但它一直在我拋出的錯誤原因是什麼我假設是一個語法錯誤。 任何幫助將不勝感激,我對PHP有點新。

補充說明:整個代碼塊是該(另一行工作):

while ($row = mysql_fetch_array($query)) { 
    echo "<tr>"; 
    echo "<td>".$row[username]."</td>"; 
    echo "<td>".$row[emailaddress]."</td>"; 
    echo "<td>"<a href="userdetails.php?".$row[username].">View Details</a></td>"; 
    echo "</tr>"; 
} 
+3

你的報價是不正確的 - 看在問題突出。 – andrewsi 2013-04-10 18:14:35

+0

是的,這是我懷疑的,你能告訴我它應該是什麼嗎? – user1791207 2013-04-10 18:16:02

回答

1

這是錯誤的,原因有二:錯誤報價和錯誤數組索引參考。

如此糟糕:

echo "<td>"<a href="userdetails.php?".$row[username].">View Details</a></td>"; 

應該是

echo "<td><a href=\"userdetails.php?".$row['username']."\">View Details</a></td>"; 

echo '<td><a href="userdetails.php?'.$row['username'].'">View Details</a></td>"; 

也可甚至更少凌亂的那樣:

printf('<td><a href="userdetails.php?%s">View Details</a></td>', $row['username']); 
0

基本的PHP語法。如果你打開一個字符串報價,使用該報價再次關閉字符串:

echo "<td>"<a href="userdetails.php?".$row[username].">View Details</a></td>"; 
    ^--open 
      ^--close 
      ^---huh? 

你需要逃避那些你輸出的一部分的內部報價:

echo "<td>\"<a href="userdetails.php?".$row[username].">View Details</a></td>"; 
      ^--- 
+0

雖然這會產生另一個錯誤和另一個不想要的行爲。 – 2013-04-10 18:22:30

+0

你的回答是錯誤的。你生成破碎的語法,仍然有錯誤的引用 – 2013-04-10 18:22:59

0

使用

while ($row = mysql_fetch_array($query)) { 
    echo '<tr>'; 
    echo '<td>'.$row["username"].'</td>'; 
    echo '<td>'.$row["emailaddress"].'</td>'; 
    echo '<td><a href="userdetails.php?'.$row["username"].'">View Details</a></td>'; 
    echo '</tr>'; 
} 
0
while ($row = mysql_fetch_array($query)) { 
?> 
<tr> 
<td><?= $row['username']; ?></td> 
<td><?= $row['emailaddress']; ?></td> 
<td><a href="userdetails.php?<?= $row['username']; ?>">View Details</a></td> 
</tr> 
<?php 
} 

只有在需要PHP時纔打開PHP標記。

0

試試這個

while ($row = mysql_fetch_array($query)) { 
    echo '<tr>'; 
    echo '<td>'.$row['username'].'</td>'; 
    echo '<td>'.$row['emailaddress'].'</td>'; 
    echo '<td><a href="userdetails.php?'.$row[username].'">View Details</a></td>'; 
    echo '</tr>'; 
}