Html / Php:echo in value属性仅显示字符串

时间:2017-06-09 18:42:46

标签: php html

我检索了数据库中存储在$tab数组中的特定行的所有字段,$tab[4]对应于我数据库中的字段“address”(其类型为varchar(255) ))。 问题是,当我在输入的“value”属性中“回显”$tab[4]时,我只得到字符串的开头。

<!DOCTYPE html>
<html lang="en">
<head>
  <title>Bootstrap Example</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">

</head>
<body>

<?php

    try
    {
      $bdd = new PDO('mysql:host=monsitexfqadmin.mysql.db;dbname=m...min;charset=utf8','m...min', 'mypass',array(PDO::ATTR_ERRMODE => PDO::ERRMODE_SILENT));
    }
    catch(Exception $e)

    {
      die('Error : '.$e->getMessage());
    }


    $req = $bdd -> prepare("SELECT * FROM users2") or die(errorInfo()); //
    $req->execute();  

    $tab=$req->fetch();

    $rue=$tab[4];

    echo "$ rue = ".$rue."<br>";
    echo "type of $rue = ".gettype($rue)."<br>";
    echo "size of $rue = ".strlen($rue)."<br>";

    ?>
      <input type="text" value=<?php echo $rue;?>>

</body>
</html>

结果:

here

我真的不明白为什么我有这个结果,任何想法?

谢谢

3 个答案:

答案 0 :(得分:2)

  <input type="text" value="<?php echo htmlspecialchars($rue); ?>" />

不要忘记价值周围的报价。另外,请务必使用htmlspecialchars()来转义您在HTML中使用的任意文本数据。否则,您将面临创建无效HTML的风险。

答案 1 :(得分:1)

您错过了"属性周围的引号(value)。

实际上,您的代码输出是:

<input type="text" value=St John Street>

...对于HTML解析器,这意味着Stvalue属性的值,JohnStreet是布尔属性---代码语法高亮强调了这一点。

BTW,正如@cale_b和@Brad所说:你应该用htmlspecialchars()来逃避你的地址---注意:这句话只有在输出内容来自用户并且尚未被转义的情况下才有效当然。

答案 2 :(得分:0)

请为字段的值属性添加双引号,如下所示

  <input type="text" value="<?php echo $rue; ?>" >