我在向数据库添加一些数据时遇到问题。到目前为止,我将在下面发布我的代码,虽然我可能遗漏了一些东西,但我不知道到底是什么。
我正在使用PHP 7.0.9在本地主机上运行它,但是我认为这并不重要。
这是在“个人资料”页面上:
<form method="post">
<div class="row-form">
<div class="col-lg-14"><strong>Title:</strong></div>
<div class="col-md-18"><input type="text" class="form-control" name="title"/></div>
</div>
<br>
<div class="row-form">
<div class="col-lg-14"><strong>Content:</strong></div>
<div class="col-md-18"><input type="text" class="form-control" name="content"/></div>
</div>
<br>
<div class="row-form">
<div class="col-md-14"><strong>Time:</strong></div>
<div class="col-md-18"><input type="text" class="form-control" name="time1"/></div>
</div>
<br>
<div class="row-form">
<div class="col-md-14"><strong>Date:</strong></div>
<div class="col-md-18"><input type="text" class="form-control" name="date1"/></div>
</div>
<div class="row-form">
<div class="col-md-18"><input type="hidden" class="form-control" name="author" value="<?php echo $_SESSION['username']; ?>" /></div>
</div>
<br>
<button name="addDates" class="btn btn-success">Submit</button>
</form>
这是我的PHP代码:
<form method="POST">
<?php
require '@/config.php';
if (isset($_POST['addDates']))
{
if (empty($_POST['title']) || empty($_POST['content']) || empty($_POST['time1']) || empty($_POST['date1']) || empty($_POST['author']))
{
$error = 'Please verify all fields';
}
if (empty($error))
{
$SQLinsert = $odb -> prepare("INSERT INTO `dates` VALUES(NULL, :title, :content, :time1, :date1, UNIX_TIMESTAMP(), :author)");
$SQLinsert -> execute(array(':title' => $_POST['title'], ':content' => $_POST['content'], ':time1' => $_POST['time1'], ':date1' => $_POST['date1'], ':author' => $_POST['author']));
echo success('Date has been added');
}
else
{
echo error($error);
}
}
?>
</form>
填写表格并提交页面后,只需刷新即可,但我这一边什么都没添加。