Reputation: 45
I want to load the data of a table in my SQL database in a HTML textarea (PHP page):
<form action="" method="post">
<textarea id="styled" name="content"></textarea>
<br/><input type="submit" name="Update">
</form>
So, I would like to load the text in my table into this textarea when the page loads. The query is simple: "SELECT * FROM about WHERE id=20"
..but I have no idea how to load the result in a textarea.
Upvotes: 0
Views: 8690
Reputation: 57729
Assuming you did mysqli_connect
etc.
<?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>
If you're not using mysqli
substitute those functions.
Upvotes: 1
Reputation: 157895
From PDO tag wiki:
<?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>
Upvotes: 0