gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 08:33:23 25 4
gpt4 key购买 nike

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

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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com