我目前正在尝试在PHP和MySQL中设置登录和注册页面。我有一个外部connection.php页面,该页面连接到DB并打印成功图像(该图像发布在每个网页的顶部,因此可以正常工作)。在当前代码中,PHP标记中的内容根本没有任何输出。按下寄存器后,我不会收到任何错误消息(如果尝试执行简单的echo语句,也不会打印任何内容)。页面只是重新加载而已。当我检查数据库时,未插入数据。我认为如果可以得到某种输出,就可以自己调试其余的输出,但是我什至没有得到。我尝试添加“全局$ conn;”。看看范围是否是问题所在,但事实并非如此。
<?php
include "connection.php";
if($_POST['register']) {
#Check to see which button was pressed
$username = $_POST['username'];
$password = $_POST['password'];
echo $username . " " . $password;
#First check to see if username is already selected
$query = $conn->prepare("SELECT * FROM accounts WHERE username=$username");
$query->execute();
if(mysqli_num_rows($query) >= 1) {
echo "Account already exists. Please try again.";
} else {
$insert = $conn->prepare('INSERT INTO accounts(username, password) VALUES (?, ?)');
$insert->bind_param("ss", $username, $password);
$insert->execute() or die (mysqli_error());
echo "Successfuly Registered";
}
}
?>
<!DOCTYPE html>
<html>
<head>
<title>UND Bookstore Login</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0"></meta>
<meta name="author" content="Sai Peri"></meta>
<meta name="description" content="CSCI 457 Assignment 1"></meta>
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
<!-- Optional theme -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap-theme.min.css" integrity="sha384-rHyoN1iRsVXV4nD0JutlnGaslCJuC7uwjduW9SVrLvRYooPp2bWYgmgJQIXwl/Sp" crossorigin="anonymous">
<!-- Latest compiled and minified JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>
<!-- Latest jQuery libraries -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
</head>
<body>
<div class="container">
<div class="row">
<div class="col-md-12">
<nav id="nav" class="navbar navbar-inverse text-center">
<ul class="nav navbar-nav">
<li><a href="index.php">Home</a></li>
<li class="active"><a href="login.php">Login</a></li>
<li><a href="search.php">Search</a></li>
<li><a href="#">Add/Delete/Edit</a></li>
</ul>
</nav>
</div> <!-- End col-md-12 -->
</div> <!-- End row (nav)-->
<div class="row">
<div class="col-md-12">
<div class="page-header">
<h3 class="text-center">Login below <small>Or register below</small></h3>
</div> <!-- End of page-header -->
</div> <!-- End col-md-12 -->
</div> <!-- End row (nav)-->
<div class="row" id="body">
<!-- Add text boxes and buttons -->
<form action="login.php" method="post">
<div class="form-group">
<label>Username</label>
<input type="text" class="form-control" id="inputUsername" placeholder="Username" name="username">
<label>Password</label>
<input type="password" class="form-control" id="inputPassword" placeholder="Password" name="password">
<button type="submit" class="btn btn-primary" name="login">Login</button>
<button type="submit" class="btn btn-primary" name="register">Register</button>
</div> <!-- End form-group -->
</form>
</div> <!-- End of row -->
<div class="row" id="footer">
<div class="col-md-12 text-center">
<footer>Created 2019 by Sai Peri</footer>
</div> <!-- End col-md-12 -->
</div> <!-- End row (nav)-->
</div> <!-- End container -->
</body>
</html>
解决:使用以下语句isset($ _ POST ['register'])或通过执行!empty($ _ POST ...)并将值添加至输入标签已解决了该问题。