0

基本的に、ユーザーが次のようなリストを作成できるようにしたいと考えています。

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

ここに写真があります:

ここに画像の説明を入力

4

1 に答える 1

2

次のクエリはすべきではありません:

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

なれ:

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

nl2br($_POST['notes'])2番目のものについては、次のように置き換えてみることができます

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

2番目の質問が何であるかまだわかりませんか?


2 番目の問題は、テキスト領域内に空きスペースがあるために発生します。以下は見苦しく見えますが、あなたが望むものを提供します。

<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 に答える