嗨,大家好,我需要一些帮助。 lm设置用户注册注册表单。但我无法将用户信息插入数据库。并且代码没有显示任何错误消息,这意味着一切都很好。 但当我试图注册时,它会给出表单的错误消息"无法注册用户"。
这是代码:(并在底部检查connect.php代码)
<?php
require_once('connect.php');
//print_r($_POST);
if(isset($_POST) & !empty($_POST)) {
$username = $_POST['username'];
$email = $_POST['email'];
$password = $_POST['password'];
// storing the sign up info in to the database
$sql = "INSERT INTO usermanagement (username, email, password) VALUES ('$username', '$email', '$password')";
//executing the the query
$result = mysqli_query($connection, $sql);
if($result){
echo "User Registered Successfully";
}
else{
echo "Failed to Register User";
}
}
?>
<div id="form-signup">
<form id="signup-form" name="sign-up" action="sign.php" method="POST">
<h1>Create your profile</h1>
<p>
<input type="text" name="username" id="username" class="signup-input" required="required" placeholder="Full name*" >
</p>
<p>
<input type="email" name="email" class="signup-input" required="required" placeholder="Email*" >
</p>
<p>
<input type="password" name="password" class="signup-input" required="required" placeholder="Mot de passe*">
</p>
<!-- <p>
<input type="password" name="confirmpassword" class="signup-input" required="required" placeholder="Confirmez mot de passe*">
</p> -->
<p class="agree"> By signing up, you agree to Tout-Passe's <br><a href="condition.php"> Terms of use</a><br> and <a href="politic.php">Privacy Policy</a>.
</p>
<p>
<input type="submit" class="signup-btn" name="btn-signup" value="Create Account">
</p>
<p class="already">Already on Tout-Passe? <a href="log.php"> Log in</a></p>
</div><!--END OF PHASE-1-->
</form> <!--END OF SIGN-UP-->
</div><!--END FO ALLFORM-->
连接代码
<?php
$connection = mysqli_connect('localhost', 'root', '');
if(!$connection){
die("Database Connection Failed" . mysqli_error($connection));
}
$select_db = mysqli_select_db($connection, 'listing');//listing = database
if(!$select_db){
die("Failed to select database" . mysqli_error($connection));
}
?>
答案 0 :(得分:2)
你做错了第4行,if(isset($_POST) & !empty($_POST)) {
你只有一个&
你应该有两个这样的:if(isset($_POST) && !empty($_POST)) {
除此之外,当你的消息告诉你它没有注册用户时,它没有解释为什么,为了解决这个问题,你必须添加一个像这样的mysqli_error():
require_once('connect.php');
//print_r($_POST);
if(isset($_POST) && !empty($_POST)) {
$username = $_POST['username'];
$email = $_POST['email'];
$password = $_POST['password'];
// storing the sign up info in to the database
$sql = "INSERT INTO usermanagement (username, email, password) VALUES ('$username', '$email', '$password')";
//executing the the query
$result = mysqli_query($connection, $sql);
if($result){
echo "User Registered Successfully";
}
else{
echo "Failed to Register User, reason: " . mysqli_error($connection);
}
}
您的代码也存在两个问题:
if(isset($_POST['username']) && !empty($_POST['username']))
而不是这样:if(isset($_POST) && !empty($_POST))