基本上我希望用户能够创建一个看起来像这样的列表:
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");
?>
这是一张图片: