PHP代码提交按钮仅刷新页面
我正在使用PHP和SQL server,在进一步解释之前,数据库连接是使用PHP代码提交按钮仅刷新页面,php,html,mysql,Php,Html,Mysql,我正在使用PHP和SQL server,在进一步解释之前,数据库连接是使用require'databaseconnect.PHP'实现的该代码用于注册用户。目前,运行代码只会刷新页面,在花费数小时尝试排除故障后,我仍然找不到错误。任何帮助都将不胜感激。我不知道我是否正确编程,所以这也可能是问题的根源。我的代码如下: <?php session_start(); $message = ""; require 'databaseconnect.php'; if($_POST['reg
require'databaseconnect.PHP'实现的代码>该代码用于注册用户。目前,运行代码只会刷新页面,在花费数小时尝试排除故障后,我仍然找不到错误。任何帮助都将不胜感激。我不知道我是否正确编程,所以这也可能是问题的根源。我的代码如下:
<?php
session_start();
$message = "";
require 'databaseconnect.php';
if($_POST['register']):
if(!empty($_POST['username']) && !empty($_POST['password']) && !empty($_POST['confirmPassword']) && !empty($_POST['pin']) && !empty($_POST['preferredName'])):
if($_POST['password'] == $_POST['confirmPassword']):
$sql = "INSERT INTO Users (Email, Password, FirstName, LastName, Type) VALUES (:email, :password, :firstname, :lastname, :type)";
If($_POST['pin'] == "123456"):
$stmt = $conn->prepare($sql);
$stmt->bindParam(':email', $_POST['email']);
$stmt->bindParam(':password', md5($_POST['password']));
$stmt->bindParam(':firstname', $_POST['firstname']);
$stmt->bindParam(':lastname', $_POST['firstname']);
$stmt->bindParam(':type', $_POST['AcctType']);
if( $stmt->execute() ):
$message = ('<div class="inputlogin alert alert-success" role="alert"><b>You are Registered!</b> You have been registered as a member! <a href = portal.php>Log In!</a></div>');
else:
$message = ('<div class="inputlogin alert alert-danger" role="alert"><b>There has been an issue...</b> While attempting to register you, there was an error with the server,try again later.</a></div>');
endif;
else:
$message = ('<div class="inputlogin alert alert-danger alert-dissmissable" role="alert"><b>Oops!</b> Authentication pin not recognized!</div>');
endif;
else:
$message = ('<div class="inputlogin alert alert-danger alert-dissmissable" role="alert"><b>Uh Oh!</b> Passwords Do not Match!</div>');
endif;
endif;
endif;
$conn = null;
?>
<html>
<head>
</head>
<body>
<div class = "container_main">
<a name="top"></a>
<br><br>
<div class = "mainHeader">
<i class = "fa fa-bars fa-2x mobile_menu" height="100px" widht="100px"></i>
</div>
<div class = "containerDEF">
<center><img class = "fixed loginImg" src = "CSS/images/TroopLogo.png" alt = "DesignLogo" width = "300" height="300px"></center>
<form action = "register.php" method ="POST">
<div class="input-group inputLogIn" id = "FirstName">
<span class="input-group-addon" id="basic-addon1">First Name:</span>
<input type="text" class="form-control" placeholder="ex. Clara" aria-describedby="basic-addon1" name="firstname">
</div>
<div class="input-group inputLogIn" id = "LastName">
<span class="input-group-addon" id="basic-addon1">Last Name:</span>
<input type="text" class="form-control" placeholder="ex. Oswald" aria-describedby="basic-addon1" name="lastname">
</div>
<div class="input-group inputLogIn" id = "Email">
<span class="input-group-addon" id="basic-addon1">Email:</span>
<input type="text" class="form-control" placeholder="ex. cOswald@kyzlet.net" aria-describedby="basic-addon1" name="email">
</div>
<div class="input-group inputLogIn" id = "ConfirmEmail">
<span class="input-group-addon" id="basic-addon1">Confirm Email:</span>
<input type="text" class="form-control" placeholder="ex. cOswald@kyzlet.net" aria-describedby="basic-addon1" name="confirmemail">
</div>
<div class="input-group inputLogin" id = "Pass">
<span class="input-group-addon" id="basic-addon1">Password:</span>
<input type="password" class="form-control" placeholder="ex. RunYouCleverBoy" aria-describedby="basic-addon1" name="password">
</div>
<div class="input-group inputLogin" id = "ConfirmPass">
<span class="input-group-addon" id="basic-addon1">Confirm Password:</span>
<input type="password" class="form-control" placeholder="ex. RunYouCleverBoy" aria-describedby="basic-addon1" name="confirmpassword">
</div>
<div class="input-group inputLogin" id = "RegistrationPin">
<span class="input-group-addon" id="basic-addon1">Registration Pin:</span>
<input type="password" class="form-control" placeholder="******" aria-describedby="basic-addon1" name="pin">
</div>
<div class="input-group inputLogin" id = "RegistrationPin">
<span class="input-group-addon" id="basic-addon1">Account Type:</span>
<select type = "select" name="AcctType" class = "form-control" >
<option value="Scout">Scout</option>
<option value="Parent">Parent</option>
<option value="Leader">Leader</option>
</select>
</div>
<?php
if (!empty($message)):
echo ($message);
endif;
?>
<center><input type = "submit" class = "submitButton" name = "register" value = "Register"></center>
</form>
<center><p>Just want to login? <a href = "portal.php" class = "lightLink" >Login Here</a>. </p>
</center>
</div>
<br><br>
<a href = "#top"><i class = "fa fa-arrow-up up_button fa-2x"></i></a>
</div>
</div>
</div>
<script src = "Scripts/Script_Main.js"></script>
</body>
</html>
POST['register']
是否返回为真?我相信你需要这样设置:
if(isset($_POST['register')){
//do your thing
}
为了防止出现任何警告,首先也应如此,因为第一次加载时将不存在$\u POST['register']
而且,事实上你没有它可能意味着你关闭了错误和警告?
若要尝试此操作,请将ini\u集(“错误报告”,E\u ALL)代码>位于脚本顶部。如果您有任何错误,这应该说明清楚
此外,请确保在您的if语句中重复某些内容!您不知道页面是否只是刷新,因为如果数据库代码中出现错误,它可能永远无法设置$message
希望这有帮助
另外,我注意到您的表单操作被设置为静态页面(保留为空将自动使用当前页面)——当我还是初学者时,有时会犯错误,使用新文件测试某些内容,结果忘记更改操作url。这有时会导致我加载旧的(备份)文件,而不是带有新测试功能的新文件。只是说说而已。我想你能做的就是先显示错误消息(如果有),然后按常规方式尝试调用你的if语句。如果(){}然后继续往下走…除了未定义的索引之外,你真的有第一个吗,这个谢谢你,这似乎起作用了,我确实看了那个动作部分,这似乎与if语句检查有关,以确保字段已填充。我不可能看到这个答案如何完整地解决这个问题。显然,它遗漏了很多元素。@Fred ii-我告诉他要正确调试他的if语句。这就是问题所在。虽然一个简单的“你的代码应该是这样”是值得赞赏的,但这个社区是关于学习的,而不是让别人为他们工作。最后,他自己解决了这个问题,再次遇到同样问题的机会很小,因为他是通过这种方式从中吸取教训的。