【问题标题】:Using a textarea to modify a MySQL table field使用 textarea 修改 MySQL 表字段
【发布时间】:2013-04-11 04:07:27
【问题描述】:

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

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");
?>

这是一张图片:

【问题讨论】:

    标签: php html mysql pdo textarea


    【解决方案1】:

    不应该如下查询:

    "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>
    

    【讨论】:

    • 是的,我的 UPDATE 语句有误,谢谢。这样就解决了更新问题。至于第二个问题,当我添加一个note,文字显示出来的时候,文字两边都有各种我最初创建note时没有的空​​格。
    • 我已将更改添加到代码中。只有一个问题,那就是笔记中多余的空白,在笔记的前面。我原始帖子中的图片将显示这一点。
    • @JimRomeFan : 用&lt;ul&gt;&lt;li&gt; 填充记录的原因是什么?
    • @JimRomeFan :为此,您可以删除 &lt;textarea&gt;&lt;?php ?&gt; 代码之间的行。现在就试试吧..
    • 你知道吗?我刚刚读到 textarea 结束标签必须在所有一行上,它确实工作得很好。我想不出任何其他必须以这种方式工作的标签。谢谢你的用心。 ul li 只是我用于所有表单的格式,用于在当前项目中显示。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-22
    • 2014-10-03
    • 1970-01-01
    • 2011-04-25
    • 1970-01-01
    相关资源
    最近更新 更多