本文介绍了向数据库添加文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用PHP和$ _POST方法将此文本添加到MySQL数据库中:

I tried adding this text to the MySQL database with PHP and the $_POST method:

HTML:

<form name="addBook" action="?action=save" method="post" enctype="multipart/form-data">
        <table>
        <tr>
            <td>Boek naam</td>
            <td><input type='text' name='book_name'  required></td>
        </tr>
        <tr>
            <td>Genre</td>
            <td><input type='text' name='book_genre'  required></td>
        </tr>
        <tr>
            <td>Cover</td>
            <td><input type='file' name='cover' id='cover'></td>
        </tr>
        <tr>
            <td>Text</td>
            <td><textarea cols='50' rows='20' name='book_text'>Schrijf hier</textarea></td>
        </tr>
        <tr>
            <td colspan="2"><input type="reset" name="reset" value="Remove text">
            <input type="submit" name="submit" value="Save"></td>
        </tr>
        </table>
        </form>

PHP:

$query = $db->prepare("
                INSERT INTO `books` (`book_name`, `book_text`, `book_genre`, `book_cover`) VALUES (:name, :text, :genre, :fulldir)");
    $query->bindParam(':name', $book_name);
    $query->bindParam(':text', $book_text);
    $query->bindParam(':genre', $book_genre);
    $query->bindParam(':fulldir', $fulldir);
    $query->execute();

这是文本:

Etiam ac enim non nisl elementum dapibus vel quis augue Sed Sed,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,在商品nisi hendrerit ut。 Quisque quis tincidunt odio。 Ut。。。。。。。前庭直径,egestas vitae scelerisque sed,pulvinar a turpis。 Curizur odio justo,ornare eget tortor eu,commodo condimentum est。Nam dictum,augue temporal dictum porttitor,mi turpis volutpat eros,a tristique metus ipsum nec enim。

Etiam ac enim non nisl elementum dapibus vel quis augue. Sed vestibulum, lacus vitae ultricies dictum, lacus augue sodales justo, sit amet efficitur purus neque vitae magna. Duis malesuada sagittis tortor, in commodo nisi hendrerit ut. Quisque quis tincidunt odio. Ut cursus enim nec venenatis laoreet. Vestibulum erat diam, egestas vitae scelerisque sed, pulvinar a turpis. Curabitur odio justo, ornare eget tortor eu, commodo condimentum est. Nam dictum, augue tempor dictum porttitor, mi turpis volutpat eros, a tristique metus ipsum nec enim."

但是当我添加文本时,它会删除两段之间的空格:

But when I add the text, it removes the whitespace between the two paragraphs:

如何将文本添加到数据库中,同时保持段落之间的空格?

How can I add the text to the database, while keeping the whitespace between paragraphs?

推荐答案

新行字符 \\\
不会在浏览器中显示,您需要将其转换为< br> 这可以使用在数据插入或输出上完成例如:

The new line character \n won't display in a browser. You need to convert them to <br>s. This can be done using nl2br either on the data insert or on the output. For example:

$query = $db->prepare("
                INSERT INTO `books` (`book_name`, `book_text`, `book_genre`, `book_cover`) VALUES (:name, :text, :genre, :fulldir)");
    $query->bindParam(':name', $book_name);
    $query->bindParam(':text', nl2br($book_text));
    $query->bindParam(':genre', $book_genre);
    $query->bindParam(':fulldir', $fulldir);
    $query->execute();

演示:

或输出法...

echo nl2br($row['book_text']);

这篇关于向数据库添加文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-13 22:03