Jayesh Khullar
Jayesh Khullar

Reputation: 35

Trouble with recording information in sql tables from Html Forms

I have created a html register page which is really basic and requires the user to enter their First name, Last name, email, and password. However only the first and last names are being recorded in the database in phpmyadmin and the email and passwords are showing as blank cloumns. I have tried to drop and add the tables and columns again without any luck, i have changed variable names and no luck as well. Not too sure what to do.

Php code

<?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 code

<!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>

Upvotes: 1

Views: 50

Answers (2)

Jayesh Khullar
Jayesh Khullar

Reputation: 35

It looks like i was missing an underscore in the php register code where it states the function. i have added it and now my code seems to be working, thanks for all the feedback!

Upvotes: 0

Oliwol
Oliwol

Reputation: 300

In order to grab the _POST variables, your input forms must have a name attribute. For your Email form, you have only specified an ID and no name. Go back and add in the name='email' attribute and it should work. Same for password.

Upvotes: 1

Related Questions