我想在HTML textarea(PHP页面)中加载SQL数据库中表的数据:
<form action="" method="post">
<textarea id="styled" name="content"></textarea>
<br/><input type="submit" name="Update">
</form>
所以,我想在页面加载时将表格中的文本加载到此textarea中。查询很简单: “SELECT * FROM about WHERE id = 20”
..但我不知道如何在textarea中加载结果。
答案 0 :(得分:1)
假设你做了mysqli_connect
等等。
<?php
$res = mysqli_query("SELECT * FROM about WHERE id=20");
$row = mysqli_fetch_assoc($res);
$value = $row["the_column"];
?>
<form action="" method="post">
<textarea id="styled" name="content"><?php echo htmlentities($value);?></textarea>
<br/><input type="submit" name="Update">
</form>
如果您没有使用mysqli
替换这些功能。
答案 1 :(得分:0)
<?php
// connect. have to be moved into separate file to be included
$dsn = "mysql:host=localhost;dbname=test;charset=utf8";
$opt = array(
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC
);
$pdo = new PDO($dsn,'root','', $opt);
//getting data
$id = 20;
$stm = $pdo->prepare("SELECT text FROM about WHERE id=?");
$stm->execute(array($id));
$text = $stm->fetchColumn();
//echoing out
?>
<form method="post">
<textarea id="styled" name="content"><?=htmlspecialchars($text)?></textarea>
<br/>
<input type="submit" name="Update">
</form>
答案 2 :(得分:0)
将你的php检索代码放在textarea
之间