php - 使用文本区域修改 MySQL 表字段

标签 php html mysql pdo textarea

基本上我希望用户能够创建一个如下所示的列表:

Please give me all books by:

James Arnold
Tom Seaver

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

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

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

这是一张图片:

enter image description here

最佳答案

不应该执行以下查询:

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

关于php - 使用文本区域修改 MySQL 表字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15940601/

相关文章:

php - PHP 中的 session 超时 : best practices

mysql - 使用 subselect 连接两个表

php - Kohana 中的全局 View 变量

php - 如何在父类构造函数中使用命名空间

php - 在 Azure 应用服务 (WebApp) 中启用 PHP 日志记录

html - 根据设备/屏幕大小水平/垂直堆叠 div

javascript - 如何覆盖 iframe 对象中的图像和链接路径?

JavaScript Canvas 动画。移动、生长和消失的物体

javascript - 如何使javascript中的排序函数表现得像mysql order by

mysql - 在mysql中创建和删除用户时 ERROR 1396 (HY000) : Operation CREATE USER