我有四个php页面:login.php,db_connection.php,admin.php和blog.php
我希望能够通过admin.php页面动态地将文章插入blog.php。
我的代码是:
的login.php
<?php
error_reporting(E_ALL & ~E_NOTICE);
session_start();
if($_POST['submit']) {
$dbUserName = "admin";
$dbPassword = "password";
$username = strip_tags($_POST['username']);
$username = strtolower($username);
$password = strip_tags($_POST['password']);
if ($username == $dbUserName && password == $dbPassword) {
$_SESSION['username'] = $username;
header('Location: admin.php');
} else {
echo "<h1 class='denied'>Access denied<h1>";
}
}
?>
<div id="login_box">
<h1 class="loreg">Log in</h1>
<form action='login.php' method='post' id='contact_form'>
<input type='text' name='username' placeholder='username... *' id='email' maxlength='60'><br>
<input type='password' name='password' placeholder='password... *' id='password' maxlength='30'><br>
<input type='submit' class='button' value='log in' id='login'>
<input type='reset' class='button' value='cancel' id='cancel'>
</form>
</div>
db_connection
<?php
$dbCon = mysqli_connect("localhost", "root", "passw0rd", "worldtour") or die(mysqli_error());
?>
admin.php的
<?php
error_reporting(E_ALL & ~E_NOTICE);
session_start();
if (isset($_SESSION['username'])) {
$username = ucfirst($_SESSION['username']);
if ($_POST['submit']) {
$title = $_POST['title'];
$submit = $_POST['subtitle'];
$content = $_POST['content'];
include_once("db_connection.php");
$sql = "INSERT INTO blog (title, subtitle, content)
VALUE ('$title', '$subtitle', '$content')";
mysqli_query($dbCon, $sql);
echo "<h1>Blog entry posted</h1>";
}
} else {
header('Location: login.php');
die();
}
?>
<h1>Welcome, <?php echo $username; ?>!</h1>
<form action="admin.php" method="post">
<h3>Title: </h3><input type="text" name="title"><br>
<h3>Subtitle: </h3><input type="text" name="subtitle"><br>
<h3>Content: </h3><textarea name="content" id="content" cols="30" rows="10"></textarea>
<input type="submit" name="submit" value="post entry">
</form>
<br>
<a href="/sites/worldtour/public/blog.php">View blog entries</a> | <a href="/sites/worldtour/public/logout.php">Log out</a>
blog.php的
<?php
include_once("db_connection.php");
$sql = "SELECT * FROM blog ORDER BY id DESC";
$result = mysqli_query($dbCon, $sql);
while($row = mysqli_fetch_array($result)) {
$title = $row['title'];
$subtitle = $row['subtitle'];
$content = $row['content'];
?>
<h1 class="headers"><?php echo $title; ?> <br> <small><?php echo $subtitle; ?></small></h1>
<article><?php echo $content; ?></article>
<hr class="artline">
<?php
}
?>
当我尝试通过login.php页面登录时,即使我插入正确的用户/密码,页面也会刷新,并且不会将我重定向到admin.php。当我插入错误的用户时,它不会提示我访问被拒绝的消息。我的代码中是否有任何错误或我遗漏的内容?感谢。
答案 0 :(得分:2)
POST方法使用标记中的name属性来标识其值。在$ _POST ['submit']中,您实际上在寻找名称='submit'的标记。
提交按钮没有定义名称属性。您需要在
中添加name ='submit'
<input type='submit' class='button' value='log in' id='login'>