从Html表单在sql表中记录信息的麻烦

时间:2017-05-07 21:10:19

标签: php html mysql phpmyadmin

我创建了一个非常基本的html注册页面,要求用户输入他们的名字,姓氏,电子邮件和密码。但是,只有名字和名字记录在phpmyadmin的数据库中,电子邮件和密码显示为空白cloumns。我试图删除并再次添加表和列没有任何运气,我已经更改了变量名称,也没有运气。不太清楚该怎么做。

Php代码

<?php

$servername = "localhost";
$username = "";
$password = "";
$dbname = "";

//Create connection
$conn = new mysqli($servername, $username, $password, $dbname);

// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 
//echo "Connected successfully";

$first_name = $_POST['first_name'];
$last_name = $_POST['last_name'];
$email = $_POST['email'];
$password_ = $_POST['password_'];

if (isset($_POST['register'])) {
    register($first_name,$last_name,$_email,$password,$conn);
}


$conn->close();


function register($first_name,$last_name,$email,$password,$conn) {
//    echo $first_name . " " . $last_name . " " . $student_id . " " . $email;

$sql = "INSERT INTO `register` (`first_name`, `last_name`, `email`, `password_`) VALUES ('$first_name', '$last_name', '$email', '$password_')";
if ($conn->query($sql) === TRUE) {
    echo "New record created successfully";
} else {
    echo "Error: " . $sql . "<br>" . $conn->error;
}

}
?>

html代码

<!DOCTYPE html>

<html lang="en">


<head>
  
<title>Bootstrap Case</title>
  
<meta charset="utf-8">
  
<meta name="viewport" content="width=device-width, initial-scale=1">
  
<link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
  
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
  
<script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>

</head>


<body>


<form role="form" action="register.php" method="POST">
  

<div class="form-group">
   
<label>First Name:</label>
   
<input type="text" class="form-control" id="first_name" name="first_name"required>

</div>
<div class="form-group">
   
<label>Last Name:</label>
   
<input type="text" class="form-control" id="last_name" name="last_name"required>

</div>
<div class="form-group">



<label>Email address:</label>
   
<input type="varchar" class="form-control" name="e_mail" id="email"required>


</div>
<div class="form-group">
    
<label>Password:</label>
   
<input type="password" class="form-control" id="password" name="password"required>

</div>
<div class="form-group">

<label>Confirm Password:</label>
   
<input type="password" class="form-control" id="confirm_password"required >



<script>
var password = document.getElementById("password")
  , confirm_password = document.getElementById("confirm_password");

function validatePassword(){
  if(password.value != confirm_password.value) {
    confirm_password.setCustomValidity("Passwords Don't Match");
  } else {
    confirm_password.setCustomValidity('');
  }
}

password.onchange = validatePassword;
confirm_password.onkeyup = validatePassword;

</script>


</div>

<button type="submit" class="btn btn-default" name="register">Register</button>

</form>




</body>

</html>

1 个答案:

答案 0 :(得分:1)

要获取_POST变量,您的输入表单必须具有名称属性。对于您的电子邮件表单,您只指定了ID而没有指定名称。返回并添加名称=&#39;电子邮件&#39;属性,它应该工作。密码相同。