0

基本上我希望用户能够创建一个看起来像这样的列表:

Please give me all books by:

James Arnold
Tom Seaver

我只需要保留换行符...

我的代码给了我一个问题:当我将信息保存到数据库中时,格式会被保留,但是当我将信息放入 textarea 时,会有很多额外的空格。我附上了一张图片,所以你可以看到它的样子。

<?php
    session_start();

    if (!isset($_SESSION['user']))
        header('Location: ../index.php');

    require ("../login.php");

    include ("header.php");
    include ("subnav.php");

    $user_id = $_SESSION['user'];
    $form_show = true;

    if (isset($_POST['submit'])) {
        if (empty($_POST['notes'])) {
            $stmt = $db->prepare("SELECT * FROM notes WHERE user=:id");
            $stmt->bindValue(':id', $user_id, PDO::PARAM_INT);
            $stmt->execute();
            $rows = $stmt->fetchAll();
            $num_rows = count($rows);
            if ($num_rows == 0) {
                $form_show = false;
                $errors[] = 'You do not currently have any notes to edit, therefore nothing to delete!.';
            }
            else {
                $stmt = $db->prepare("DELETE FROM notes WHERE user=:id");
                $stmt->bindValue(':id', $user_id, PDO::PARAM_INT);
                $stmt->execute();
                $errors[] = 'Your notes have been successfully deleted.';
                $form_show = false;
            }
        }
        else {
            $stmt = $db->prepare("SELECT * FROM notes WHERE user=:id");
            $stmt->bindValue(':id', $user_id, PDO::PARAM_INT);
            $stmt->execute();
            $rows = $stmt->fetchAll();
            $num_rows = count($rows);
            if ($num_rows == 0) {
                $stmt = $db->prepare("INSERT INTO notes (user, notes) VALUES (:user, :notes)");
                $stmt->bindValue(':user', $user_id, PDO::PARAM_INT);
                $stmt->bindValue(':notes', trim($_POST['notes']), PDO::PARAM_STR);
                $stmt->execute();
                echo 'Your notes have been successfully added!';
                $form_show = false;
            }
            else {
                $stmt = $db->prepare("UPDATE notes SET notes=:notes WHERE user=:id");
                $stmt->bindValue(':notes',trim($_POST['notes']), PDO::PARAM_STR);
                $stmt->bindValue(':id', $user_id, PDO::PARAM_INT);
                $stmt->execute();
                echo 'Your notes have been successfully updated!';
                $form_show = false;
            }
        }
    }

?>

    <?php
        if (!empty($errors))
            foreach($errors as $error)
                echo $error;
    ?>

    <?php
        if ($form_show == true) { ?>
            <p>Use this page to add any notes you would like to list for your subscription. For example, if you want all books by a particular artist, here is the place to add that information!</p>

            <form action="" method="post">
                    <ul>
                        <li>
                            Notes: <br>
                            <textarea name="notes" rows="30" cols="70">

                            <?php
                            $stmt = $db->prepare("SELECT * FROM notes WHERE user=:id");
                            $stmt->bindValue(':id', $user_id, PDO::PARAM_INT);
                            $stmt->execute();
                            $rows = $stmt->fetchAll();
                            $num_rows = count($rows);
                            if ($num_rows != 0)
                                foreach ($rows as $row)
                                    echo $row['notes'];
                            ?>

                            </textarea>
                        </li>
                        <li>
                            <input type="submit" value="Modify Notes" name ="submit" id="submit">
                        </li>
                    </ul>
            </form>
<?php   } ?>



<?php   
    include ("footer.php");
?>

这是一张图片:

在此处输入图像描述

4

1 回答 1

2

不应该以下查询:

"UPDATE notes SET notes=:notes WHERE id=:id"

是:

"UPDATE notes SET notes=:notes WHERE user=:id"

对于第二个,您可以尝试替换nl2br($_POST['notes'])

nl2br( trim($_POST['notes']) )

我还是不明白第二个问题是什么?


第二个问题是由于文本区域内的可用空白空间而产生的。以下看起来很难看,但会为您提供您想要的。

<textarea name="notes" rows="30" cols="70"><?php $stmt=$ db->prepare("SELECT * FROM notes WHERE user=:id"); $stmt->bindValue(':id', $user_id, PDO::PARAM_INT); $stmt->execute(); $rows = $stmt->fetchAll(); $num_rows = count($rows); if ($num_rows != 0) foreach ($rows as $row) echo $row['notes']; ?></textarea>
于 2013-04-11T04:19:12.883 回答