我正在阅读数据库中的电子邮件.我希望将电子邮件作为超链接回应,但它不起作用.
<?php
$un = $_POST['username'];
$pw = $_POST['password'];
// connect to the db
$user = 'proc';
$pswd = 'passwd';
$db = 'school';
$conn = mysql_connect('localhost', $user, $pswd);
mysql_select_db($db, $conn);
// run the query to search for the username and password the match
$query = "SELECT email AS text FROM contact";
$result = mysql_query($query) or die("Unable to verify user because : " . mysql_error());
// this is where the actual verification happens
while ($row = mysql_fetch_assoc($result)) {
echo ("\n".$row['text']);
echo "<a href=.$row['text']>some text</a>";
}
?>
Run Code Online (Sandbox Code Playgroud)
知道我的代码有什么问题吗?
在所有情况下,您都不能将变量嵌入到双引号字符串中.这是你可以做的:
echo "<a href={$row['text']}>some text</a>";
Run Code Online (Sandbox Code Playgroud)
要么
echo "<a href=".$row['text'].">some text</a>";
Run Code Online (Sandbox Code Playgroud)
我个人更喜欢第二种形式,因为历史上一眼就能看出变量是通过编辑器的语法高亮显示嵌入的(尽管今天的编辑也可能会突出显示第一种形式).
请注意,您当前的代码存在其他问题:没有HTML属性值的引号,也没有正确转义嵌入HTML的值.解决这个问题
echo '<a href="'.htmlspecialchars($row['text']).'">some text</a>";
Run Code Online (Sandbox Code Playgroud)
确切的正确形式也取决于数据的编码; 看看htmlspecialchars细节.