I am writing PHP code where I am trying to insert user data from an HTML form into a MySQL database for recording attendance. The problem is, I am not getting an error when I run my code, but there is no data being inserted and I can’t figure out why?
Here is the code I have:
<?php
include('nav/head.php');
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<meta name="description" content="">
<meta name="author" content="">
<style type="text/css">
/* Formatting search box */
.search-box{
width: 1648px;
position: relative;
display: inline-block;
font-size: 16px;
}
.search-box input[type="text"]{
height: 32px;
padding: 5px 10px;
border: 1px solid #CCCCCC;
font-size: 14px;
}
.result{
position: absolute;
z-index: 999;
top: 100%;
left: 0;
}
.search-box input[type="text"], .result{
width: 100%;
box-sizing: border-box;
}
/* Formatting result items */
.result p{
margin: 0;
padding: 7px 10px;
border: 1px solid #CCCCCC;
border-top: none;
cursor: pointer;
background-color: #FFFFFF;
}
.result p:hover{
background: #f2f2f2;
}
</style>
<script src="https://code.jquery.com/jquery-1.12.4.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$('.search-box input[type="text"]').on("keyup input", function(){
/* Get input value on change */
var inputVal = $(this).val();
var resultDropdown = $(this).siblings(".result");
if(inputVal.length){
$.get("api/search/backend-search.php", {term: inputVal}).done(function(data){
// Display the returned data in browser
resultDropdown.html(data);
});
} else{
resultDropdown.empty();
}
});
// Set search input value on click of result item
$(document).on("click", ".result p", function(){
$(this).parents(".search-box").find('input[type="text"]').val($(this).text());
$(this).parent(".result").empty();
});
});
</script>
<title>CCRP | Home</title>
<?php include('nav/header.php'); ?>
<h1 class="h3 mb-2 text-gray-800">Record Attendance</h1>
<br>
<form action="api/attn_record.php" method="post">
<div class="form-group search-box">
<label for="member_name">Member Name</label>
<input type="text" class="form-control" id="member_name" placeholder="Search for Member..." maxlength="" autocomplete="off" />
<div class="result"></div>
</div>
<div class="form-group">
<label for="member_email">Member's Email Address</label>
<input type="text" class="form-control" id="member_email" placeholder="" maxlength="255" autocomplete="off" />
</div>
<div class="form-group">
<label for="member_phone">Member's Phone Number</label>
<input type="text" class="form-control" id="member_phone" placeholder="" maxlength="14" autocomplete="off" />
</div>
<div class="form-group">
<label for="present">Attended  </label>
<input type="checkbox" id="present" placeholder="" value="Attended" autocomplete="off" />
</div>
<input type="submit" id="btn_save" class="btn btn-primary" value="Save and Add New">
<input type="submit" id="btn_home" class="btn btn-danger" value="Save and Close">
</form>
</div>
<!-- /.container-fluid -->
</div>
<!-- End of Main Content -->
<?php include('nav/footer.php'); ?>
</html>
This is the PHP code where the insertion is supposed to be happening:
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "ccrp_db";
try {
$conn = new PDO("mysql:host=$servername;dbname=$dbname", $username,
$password);
// set the PDO error mode to exception
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
// prepare sql and bind parameters
$stmt = $conn->prepare("INSERT INTO attendance (member_name, member_email, member_phone, present) VALUES (:member_name, :member_email, :member_phone, :present)");
$stmt->bindParam(':member_name', $member_name);
$stmt->bindParam(':member_email', $member_email);
$stmt->bindParam(':member_phone', $member_phone);
$stmt->bindParam(':present', $present);
// insert a row
if(isset($_POST['btn_save'])) {
$member_name = $_POST["member_name"];
$member_email = $_POST["member_email"];
$member_phone = $_POST["member_phone"];
$present = $_POST["present"];
}
$stmt->execute();
echo "New records created successfully";
}
catch(PDOException $e)
{
echo "Error: " . $e->getMessage();
}
$conn = null;
?>
And this is the result from the MySQL Database: