mysqlからデータを取り出して、form・textareaに挿入する

複数行のデータは、explode(“\n”, $hoge);で、1行ずつ配列に変換
inputはvalueに変数を代入、textareaはタグ内に挿入
textareaの改行は”&#13″を使う。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
<?php
    $dsn = "mysql:dbname=mail;host=localhost";
    $user = "hoge";
    $password = "hogehoge";
    $id = 5;
    try {
        $dbh = new PDO($dsn, $user, $password);
    } catch (PDOException $e){
        print('connection failed:'.$e->getMessage());
    }
        $sql = "select * from sends where id = $id";
    $stmt = $dbh->query($sql);
 
    $result = $stmt->fetch(PDO::FETCH_ASSOC);
    $to = $result['username'];
    $subject = $result['subject'];
    $body = $result['body'];
    $body = explode("\n", $body);
    $sendtime = $result['sendtime'];
?>
<span id="fs-s">※必須</span><br>
<input type="text" name="to" size="90%" value="<?php echo $to ?>" required><br>
件名<br>
<input type="text" name="subject" value="><?php echo $subject; ?>" size="90%"><br>
本文
<span id="fs-s">※必須</span><br>
<textarea name="body" rows="12" cols="100%" required>
&#13;&#13;<?php
            echo ">".$sendtime ."&#13;";
        foreach($body as $value){
            echo ">" .$value;
        }
    ?>
</textarea>
</p>
<input type="file" id="files" name="files&#91;&#93;" multiple />
<div id="caution">
※送信できるファイルの拡張子は"gif","jpg","jpeg","png"の何れかです。<br>
※ファイル送信は最大2つまでです。3つ以上選択しても、3つ目以降は送れません。また、二つのファイルを送信する場合は、キーボードの"ctl"ボタンなどで二つ選択した状態で開いてください。</div>
<output id="list"></output>
<p>
<input type="submit" value="送信">
</form>

返信っぽい表示になっていますね。

では、代入していきます。

メール詳細

返信押下

返信

送信済メール

割と簡単でしたね。