【发布时间】:2019-10-01 23:12:18
【问题描述】:
我正在尝试对 PHP 和 MySQL(i) 产生兴趣。
为了更直接,我会告诉你我愿意做什么: 我希望文本区域能够将文本上传到数据库中。
这很容易做到。但是在某行文本之后它只是没有出现在数据库中,并且在我制作的错误日志或php编辑文件中没有给出错误代码。
奇怪的是,在 PhpMyAdmin 中查询工作正常。
回答中需要考虑的事项:
- 我尝试扩大最大 PHP 上传大小;
- 我尝试将输入标签更改为 textarea,反之亦然;
- 我尝试向特定标签添加和/或更改最大字符类。
主要处理php:
if (isset($_POST['save'])) {
$id = $_POST['id'];
$author = $_POST['author'];
$gct = $_POST['content'];
mysqli_query($connect, "INSERT INTO get_content (id, content, author) VALUES ('$id', '$gct', '$author')");
$_SESSION['message'] = "content succesfully saved";
header('location: edit.php');
}
if (isset($_POST['update'])) {
$id = $_POST['id'];
$gct = $_POST['content'];
$author = $_POST['author'];
mysqli_query($connect, "UPDATE get_content SET content='$gct', author='$author' WHERE id=$id");
$_SESSION['message'] = "Author updated!";
header('location: edit.php');
}
if (isset($_GET['del'])) {
$id = $_GET['del'];
mysqli_query($connect, "DELETE FROM get_content WHERE id=$id");
$_SESSION['message'] = "Content deleted!";
header('location: edit.php');
}
文本区域的 HTML 文件:
<div class="input-group">
<label>Picture or text</label><br>
<textarea id="area4" rows="20" cols="50" autofocus="autofocus" name="content"></textarea>
</div>
error_log 文件或 PhpMyAdmin 中没有给出错误消息
【问题讨论】:
-
很可能您的列已达到 varchar 限制
-
您能否向我解释一下为什么当我将它直接 SQL 到数据库中时它会起作用?因为我尝试这样做并且效果很好
-
内容中的单引号会打断查询
-
警告:您对SQL Injections 持开放态度,应该真正使用参数化的prepared statements,而不是手动构建查询。它们由PDO 或MySQLi 提供。永远不要相信任何类型的输入,尤其是来自客户端的输入。即使您的查询仅由受信任的用户执行,you are still in risk of corrupting your data。