Store information into Database using PHP and MySQL




Index.php


<!DOCTYPE html>
<html>
<head>
<title>Student Information</title>


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



</head>
<body>
<div class="container">
<div class="reg_form">
<form class="form-horizontal" method="post" action="">
<fieldset>

<!-- Form Name -->
<legend>Register</legend>

<!-- Text input-->
<div class="form-group">
  <label class="col-md-4 control-label" for="fn">First name</label>
  <div class="col-md-4">
  <input id="fn" name="fn" type="text" placeholder="first name" class="form-control input-md" required="">
   
  </div>
</div>

<!-- Text input-->
<div class="form-group">
  <label class="col-md-4 control-label" for="ln">Last name</label>
  <div class="col-md-4">
  <input id="ln" name="ln" type="text" placeholder="last name" class="form-control input-md" required="">
   
  </div>
</div>

<div class="form-group">
  <label class="col-md-4 control-label" for="ln">Date of Birth</label>
  <div class="col-md-4">
  <input id="date" name="date" placeholder="YYYY/MM/DD" type="text" class="form-control input-md" required="">
   
</div>

</div>

<!-- Text input-->
<div class="form-group">
  <label class="col-md-4 control-label" for="email">Email</label>
  <div class="col-md-4">
  <input id="email" name="email" type="text" placeholder="email" class="form-control input-md" required="">
   
  </div>
</div>


<!-- Text input-->
<div class="form-group">
  <label class="col-md-4 control-label" for="zip">Zip Code</label>
  <div class="col-md-4">
  <input id="zip" name="zip" type="text" placeholder="Zip Code" class="form-control input-md" required="">
   
  </div>
</div>

<!-- Text input-->
<div class="form-group">
  <label class="col-md-4 control-label" for="ctry">Country</label>
  <div class="col-md-4">
  <input id="ctry" name="ctry" type="text" placeholder="Country" class="form-control input-md" required="">
   
  </div>
</div>

<!-- Text input-->
<div class="form-group">
  <label class="col-md-4 control-label" for="username">Username</label>
  <div class="col-md-4">
  <input id="username" name="username" id="username" type="text" placeholder="Username" onchange="checkname();" class="form-control input-md" required="">
    <span id="name_status"></span>
 <br>
  </div>
</div>

<!-- Text input-->
<div class="form-group">
  <label class="col-md-4 control-label" for="pass">Password</label>
  <div class="col-md-4">
  <input id="password" name="password" type="password" placeholder="Password" class="form-control input-md" required="">
   
  </div>
</div>

<!-- Text input-->
<div class="form-group">
  <label class="col-md-4 control-label" for="pass">Confirm Password</label>
  <div class="col-md-4">
 
  <input id="cpassword" name="cpassword" type="password"  onchange="checkPasswordMatch()" placeholder="Password" class="form-control input-md" required="">
  </div>
<div class="row registrationFormAlert" id="divCheckPasswordMatch">
</div>
   
</div>


<!-- Multiple Radios (inline) -->
<div class="form-group">
  <label class="col-md-4 control-label" for="Training">Are you interested to work with us ?</label>
  <div class="col-md-4">
    <label class="radio-inline" for="Training-0">
      <input type="radio" name="Training" id="Training-0" value="yes" checked="checked">
      Yes
    </label>
    <label class="radio-inline" for="Training-1">
      <input type="radio" name="Training" id="Training-1" value="no">
      No
    </label>
  </div>
</div>



<!-- Select Basic -->
<div class="form-group">
  <label class="col-md-4 control-label" for="selectbasic">Do you Agree</label>
  <div class="col-md-4">
    <select id="selectbasic" name="selectbasic" class="form-control input-md">
      <option>Yes</option>
      <option>No</option>
    </select>
  </div>
</div>


<div class="form-group">
  <label class="col-md-4 control-label" for="submit"></label>
  <div class="col-md-4">
    <button id="submit" name="submit" class="btn btn-primary">SUBMIT</button>
  </div>
</div>

</fieldset>
</form>
</div>

<?php
//print_r($date); die();
include 'db.php';
if(isset($_POST['submit']))
{
//print_r($_POST);

$fn = $_POST['fn'];
$ln = $_POST['ln'];
  $dob = $_POST['date'];
$email = $_POST['email'];
$pin = $_POST['zip'];
$ctry = $_POST['ctry'];
  $username = $_POST['username'];
  $password = $_POST['password'];
$training = $_POST['Training'];
$agree = $_POST['selectbasic'];

 
$query = "insert into register(fn, ln, dob, email, pin, ctry,username,password, training, agree) values('$fn','$ln','$dob','$email','$pin','$ctry', '$username','$password','$training','$agree')";
$result = mysqli_query($con,$query);
if(!$result)
{
    die('Error: ' . mysqli_error($con));
}

else
{

    echo '<script language="javascript">';
    echo 'alert("Successfully Registered");';
    echo '</script>';
}      
}

?>

<?php
$result = mysqli_query($con,"SELECT * FROM register");

echo "<table class='table'>
<tr>
<th>Id</th>
<th>First Name</th>
<th>Last Name</th>
<th>Username</th>
</tr>";

while($row = mysqli_fetch_array($result))
  {
  echo "<tr>";
  echo "<td>" . @$row['id'] . "</td>";
  echo "<td>" . @$row['fn'] . "</td>";
  echo "<td>" . @$row['ln'] . "</td>";
  echo "<td>" . @$row['username'] . "</td>";
  echo "</tr>";
  }
echo "</table>";

mysqli_close($con);

?>
</div>




<script src="https://code.jquery.com/jquery-3.2.1.js"
  integrity="sha256-DZAnKJ/6XZ9si04Hgrsxu/8s717jcIzLy3oi35EouyE="
  crossorigin="anonymous"></script>



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


<!-- Include Date Range Picker -->
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datepicker/1.4.1/js/bootstrap-datepicker.min.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datepicker/1.4.1/css/bootstrap-datepicker3.css"/>


<script>
  $(document).ready(function(){
    var date_input=$('input[name="date"]'); //our date input has the name "date"
    var container=$('.bootstrap-iso form').length>0 ? $('.bootstrap-iso form').parent() : "body";
    date_input.datepicker({
      format: 'yyyy/mm/dd',
      container: container,
      todayHighlight: true,
      autoclose: true,
    })
  })

//   $('#cpassword').on('keyup', function () {
//     if ($(this).val() == $('#password').val()) {
//         $('#message').html('matching').css('color', 'green');
//     } else $('#message').html('not matching').css('color', 'red');
// });

function checkPasswordMatch() {
    var password = $("#password").val();
    var confirmPassword = $("#cpassword").val();

    if (password != "") {
      if (password != confirmPassword)
        $("#divCheckPasswordMatch").html("Passwords do not match!");
    else
        $("#divCheckPasswordMatch").html("Passwords Match.");
    }
   
}

//ajax
function checkname()
{
 var name=document.getElementById( "username" ).value;
 

 if(name)
 {
  $.ajax({
  type: 'post',
  url: 'checkdata.php',
  data: {
   user_name:name,
  },
  success: function (response) {
   $( '#name_status' ).html(response);
   if(response=="OK")
   {
    return true;
   }
   else
   {
    return false;
   }
  }
  });
 }
 else
 {
  $( '#name_status' ).html("");
  return false;
 }
}

function check_length(){
 
  if (name < 4 ) {
  $("#name_status").html("Username is greater than 4 characters");
  }

}
</script>
<script type="text/javascript" src="validation.js"></script>
</body>
</html>


checkdata.php


<?php
include 'db.php';

if(isset($_POST['user_name']))
{
 $name =$_POST['user_name'];

 $query = "SELECT username FROM register WHERE username='$name' ";

 $result= mysqli_query($con,$query);

 if(mysqli_num_rows($result)>0)
 {
  echo "User Name Already Exist";
 }
 else
 {
  echo "OK";
 }
 exit();
}
?>
Newest
Previous
Next Post »

Disqus Shortname

Comments system