我在将表单中的内容提交到数据库时遇到问题。使用isset
函数时,它显示未设置变量。
这是我的PHP代码和html代码。
.PHP
<?PHP
$title = $_POST['title']['name'];
if(isset($title)) {
echo 'all is well';
} else {
echo 'all is not well';
}
?>
.HTML
<div class="container">
<form action="newEmptyPHP.php" method="post" enctype="multipart/form-data">
<input type="text" name="title" placeholder="title">
<input type="text" name="artist" placeholder="artist">
<input type="file" name="cover" placeholder="Upload Picture">
<input type="file" name="song" placeholder="Upload Song">
</form>
</div>
当我刷新浏览器时,我收到"一切都不好"。我错过了什么?
在 PHP 中,$_POST['title']
应该用于获取标题输入的值。
此外,在将变量分配给变量之前,应始终检查是否设置了$_POST
中的变量。
<?php
if(isset($_POST['title'])) {
$title = $_POST['title'];
}
if($title) {
echo 'all is well';
}else{
echo 'all is not well';
}
?>
此行导致错误:$title = $_POST['title']['name'];
。
$_POST
后应跟 HTML name
属性,在您的情况下为 name="title"
。
此外,在将表单分配给变量之前,您应该检查表单是否已发布。
因此,它应该是:
if(isset($_POST['title'])) {
$title = $_POST['title'];