Home » Php » html – php form send data to wrong mysql column

html – php form send data to wrong mysql column

Posted by: admin February 25, 2020 Leave a comment

Questions:

I’m trying to use add student form from student.php file. But after I hit add button from the form, the data are being saved to wrong column. Only student name and roll no. are correct. student_dob in mysql table only save 0000-00-00. Others either wrong place or no data at all.

Here are the student.php codes:

<?php
//student.php
include('header.php');
?>
<div class="container" style="margin-top:30px">
<div class="card">
<div class="card-header">
<div class="row">
<div class="col-md-9">Student List</div>
<div class="col-md-3" align="right">
<button type="button" id="add_button" class="btn btn-info btn-sm">Add</button>
</div>
</div>
</div>
<div class="card-body">
<div class="table-responsive">
<span id="message_operation"></span>
<table class="table table-striped table-bordered" id="student_table">
<thead>
<tr>
<th>Student Name</th>
<th>Mykids</th>
<th>Gender</th>
<th>Class</th>
<th>Edit</th>
<th>Delete</th>
</tr>
</thead>
<tbody>
</tbody>
</table>
</div>
</div>
</div>
</div>
</body>
</html>
<script type="text/javascript" src="../js/bootstrap-datepicker.js"></script>
<link rel="stylesheet" href="../css/datepicker.css" />
<style>
.datepicker {
z-index: 1600 !important; /* has to be larger than 1050 */
}
</style>
<div class="modal" id="formModal">
<div class="modal-dialog">
<form method="post" id="student_form">
<div class="modal-content">
<!-- Modal Header -->
<div class="modal-header">
<h4 class="modal-title" id="modal_title"></h4>
<button type="button" class="close" data-dismiss="modal">&times;</button>
</div>
<!-- Modal body -->
<div class="modal-body">
<div class="form-group">
<div class="row">
<label class="col-md-4 text-right">Student Name <span class="text-danger">*</span></label>
<div class="col-md-8">
<input type="text" name="student_name" id="student_name" class="form-control" />
<span id="error_student_name" class="text-danger"></span>
</div>
</div>
</div>
<div class="form-group">
<div class="row">
<label class="col-md-4 text-right">Roll No. <span class="text-danger">*</span></label>
<div class="col-md-8">
<input type="text" name="student_roll_number" id="student_roll_number" class="form-control" />
<span id="error_student_roll_number" class="text-danger"></span>
</div>
</div>
</div>
<div class="form-group">
<div class="row">
<label class="col-md-4 text-right">Date of Birth <span class="text-danger">*</span></label>
<div class="col-md-8">
<input type="text" name="student_dob" id="student_dob" class="form-control" />
<span id="error_student_dob" class="text-danger"></span>
</div>
</div>
</div>
<div class="form-group">
<div class="row">
<label class="col-md-4 text-right">Gender <span class="text-danger">*</span></label>
<div class="col-md-8">
<input type="text" name="student_gender" id="student_gender" class="form-control" />
<span id="error_student_gender" class="text-danger"></span>
</div>
</div>
</div>
<div class="form-group">
<div class="row">
<label class="col-md-4 text-right">Grade<span class="text-danger">*</span></label>
<div class="col-md-8">
<select name="student_grade_id" id="student_grade_id" class="form-control">
<option value="">Select Grade</option>
<?php
echo load_grade_list($connect);
?>
</select>
<span id="error_student_grade_id" class="text-danger"></span>
</div>
</div>
</div>
<div class="form-group">
<div class="row">
<label class="col-md-4 text-right">Card No.<span class="text-danger">*</span></label>
<div class="col-md-8">
<input type="text" name="student_card" id="student_card" class="form-control" />
<span id="error_student_card" class="text-danger"></span>
</div>
</div>
</div>
</div>
<!-- Modal footer -->
<div class="modal-footer">
<input type="hidden" name="student_id" id="student_id" />
<input type="hidden" name="action" id="action" value="Add" />
<input type="submit" name="button_action" id="button_action" class="btn btn-success btn-sm" value="Add" />
<button type="button" class="btn btn-danger btn-sm" data-dismiss="modal">Close</button>
</div>
</div>
</form>
</div>
</div>
<div class="modal" id="deleteModal">
<div class="modal-dialog">
<div class="modal-content">
<!-- Modal Header -->
<div class="modal-header">
<h4 class="modal-title">Delete Confirmation</h4>
<button type="button" class="close" data-dismiss="modal">&times;</button>
</div>
<!-- Modal body -->
<div class="modal-body">
<h3 align="center">Are you sure you want to remove this?</h3>
</div>
<!-- Modal footer -->
<div class="modal-footer">
<button type="button" name="ok_button" id="ok_button" class="btn btn-primary btn-sm">OK</button>
<button type="button" class="btn btn-danger btn-sm" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
<script>
$(document).ready(function(){
var dataTable = $('#student_table').DataTable({
"processing":true,
"serverSide":true,
"order":[],
"ajax":{
url:"student_action.php",
method:"POST",
data:{action:'fetch'},
}
});
$('#student_dob').datepicker({
format:"yyyy-mm-dd",
autoclose: true,
container: '#formModal modal-body'
});
function clear_field()
{
$('#student_form')[0].reset();
$('#error_student_name').text('');
$('#error_student_roll_number').text('');
$('#error_student_dob').text('');
$('#error_student_gender').text('');
$('#error_student_grade_id').text('');
$('#error_student_card').text('');
}
$('#add_button').click(function(){
$('#modal_title').text('Add Student');
$('#button_action').val('Add');
$('#action').val('Add');
$('#formModal').modal('show');
clear_field();
});
$('#student_form').on('submit', function(event){
event.preventDefault();
$.ajax({
url:"student_action.php",
method:"POST",
data:$(this).serialize(),
dataType:"json",
beforeSend:function(){
$('#button_action').val('Validate...');
$('#button_action').attr('disabled', 'disabled');
},
success:function(data)
{
$('#button_action').attr('disabled', false);
$('#button_action').val($('#action').val());
if(data.success)
{
$('#message_operation').html('<div class="alert alert-success">'+data.success+'</div>');
clear_field();
$('#formModal').modal('hide');
dataTable.ajax.reload();
}
if(data.error)
{
if(data.error_student_name != '')
{
$('#error_student_name').text(data.error_student_name);
}
else
{
$('#error_student_name').text('');
}
if(data.error_student_roll_number != '')
{
$('#error_student_roll_number').text(data.error_student_roll_number);
}
else
{
$('#error_student_roll_number').text('');
}
if(data.error_student_dob != '')
{
$('#error_student_dob').text(data.error_student_dob);
}
else
{
$('#error_student_dob').text('');
}
if(data.error_student_gender != '')
{
$('#error_student_gender').text(data.error_student_gender);
}
else
{
$('#error_student_gender').text('');
}
if(data.error_student_grade_id != '')
{
$('#error_student_grade_id').text(data.error_student_grade_id);
}
else
{
$('#error_student_grade_id').text('');
}
if(data.error_student_card != '')
{
$('#error_student_card').text(data.error_student_card);
}
else
{
$('#error_student_card').text('');
}
}
}
})
});
var student_id = '';
$(document).on('click', '.edit_student', function(){
student_id = $(this).attr('id');
clear_field();
$.ajax({
url:"student_action.php",
method:"POST",
data:{action:'edit_fetch', student_id:student_id},
dataType:"json",
success:function(data)
{
$('#student_name').val(data.student_name);
$('#student_roll_number').val(data.student_roll_number);
$('#student_dob').val(data.student_dob);
$('#student_gender').val(data.student_gender);
$('#student_grade_id').val(data.student_grade_id);
$('#student_card').val(data.student_card);
$('#student_id').val(data.student_id);
$('#modal_title').text('Edit Student');
$('#button_action').val('Edit');
$('#action').val('Edit');
$('#formModal').modal('show');
}
})
});
$(document).on('click', '.delete_student', function(){
student_id = $(this).attr('id');
$('#deleteModal').modal('show');
});
$('#ok_button').click(function(){
$.ajax({
url:"student_action.php",
method:"POST",
data:{student_id:student_id, action:"delete"},
success:function(data)
{
$('#message_operation').html('<div class="alert alert-success">'+data+'</div>');
$('#deleteModal').modal('hide');
dataTable.ajax.reload();
}
})
});
});
</script>

here is student_action.php (responsible to send data to mysql)

<?php
//student_action.php
include('database_connection.php');
session_start();
if(isset($_POST["action"]))
{
if($_POST["action"] == "fetch")
{
$query = "
SELECT * FROM tbl_student 
INNER JOIN tbl_grade 
ON tbl_grade.grade_id = tbl_student.student_grade_id 
";
if(isset($_POST["search"]["value"]))
{
$query .= '
WHERE tbl_student.student_name LIKE "%'.$_POST["search"]["value"].'%" 
OR tbl_student.student_roll_number LIKE "%'.$_POST["search"]["value"].'%"
OR tbl_student.student_dob LIKE "%'.$_POST["search"]["value"].'%" 
OR tbl_grade.grade_name LIKE "%'.$_POST["search"]["value"].'%" 
';
}
if(isset($_POST["order"]))
{
$query .= '
ORDER BY '.$_POST['order']['0']['column'].' '.$_POST['order']['0']['dir'].'
';
}
else
{
$query .= '
ORDER BY tbl_student.student_id DESC 
';
}
if($_POST["length"] != -1)
{
$query .= 'LIMIT ' . $_POST['start'] . ', ' . $_POST['length'];
}
$statement = $connect->prepare($query);
$statement->execute();
$result = $statement->fetchAll();
$data = array();
$filtered_rows = $statement->rowCount();
foreach($result as $row)
{
$sub_array = array();
$sub_array[] = $row["student_name"];
$sub_array[] = $row["student_roll_number"];
$sub_array[] = $row["student_gender"];
$sub_array[] = $row["grade_name"];
$sub_array[] = '<button type="button" name="edit_student" class="btn btn-primary btn-sm edit_student" id="'.$row["student_id"].'">Edit</button>';
$sub_array[] = '<button type="button" name="delete_student" class="btn btn-danger btn-sm delete_student" id="'.$row["student_id"].'">Delete</button>';
$data[] = $sub_array;
}
$output = array(
"draw"              =>  intval($_POST["draw"]),
"recordsTotal"      =>  $filtered_rows,
"recordsFiltered"   =>  get_total_records($connect, 'tbl_student'),
"data"              =>  $data
);
echo json_encode($output);
}
if($_POST["action"] == 'Add' || $_POST["action"] == "Edit")
{
$student_name = '';
$student_roll_number = '';
$student_dob = '';
$student_gender = '';
$student_grade_id = '';
$student_card = '';
$error_student_name = '';
$error_student_roll_number = '';
$error_student_dob = '';
$error_student_gender = '';
$error_student_grade_id = '';
$error_student_card = '';
$error = 0;
if(empty($_POST["student_name"]))
{
$error_student_name = 'Student Name is required';
$error++;
}
else
{
$student_name = $_POST["student_name"];
}
if(empty($_POST["student_roll_number"]))
{
$error_student_roll_number = 'Student Roll Number is required';
$error++;
}
else
{
$student_roll_number = $_POST["student_roll_number"];
}
if(empty($_POST["student_dob"]))
{
$error_student_dob = 'Student Date of Birth is required';
$error++;
}
else
{
$student_dob = $_POST["student_dob"];
}
if(empty($_POST["student_gender"]))
{
$error_student_dob = 'Student Gender is required';
$error++;
}
else
{
$student_dob = $_POST["student_gender"];
}
if(empty($_POST["student_grade_id"]))
{
$error_student_grade_id = "Grade is required";
$error++;
}
else
{
$student_grade_id = $_POST["student_grade_id"];
}
if(empty($_POST["student_card"]))
{
$error_student_card = "Card No. is required";
$error++;
}
else
{
$student_grade_id = $_POST["student_card"];
}
if($error > 0)
{
$output = array(
'error'                         =>  true,
'error_student_name'            =>  $error_student_name,
'error_student_roll_number'     =>  $error_student_roll_number,
'error_student_dob'             =>  $error_student_dob,
'error_student_gender'          =>  $error_student_gender,
'error_student_grade_id'        =>  $error_student_grade_id,
'error_student_card'            =>  $error_student_card
);
}
else
{
if($_POST["action"] == 'Add')
{
$data = array(
':student_name'         =>  $student_name,
':student_roll_number'  =>  $student_roll_number,
':student_dob'          =>  $student_dob,
':student_gender'       =>  $student_gender,
':student_grade_id'     =>  $student_grade_id,
':student_card'         =>  $student_card
);
$query = "
INSERT INTO tbl_student 
(student_name, student_roll_number, student_dob, student_gender, student_grade_id, student_card) 
VALUES (:student_name, :student_roll_number, :student_dob, :student_gender, :student_grade_id, :student_card)
";
$statement = $connect->prepare($query);
if($statement->execute($data))
{
$output = array(
'success'       =>  'Data Added Successfully',
);
}
}
if($_POST["action"] == "Edit")
{
$data = array(
':student_name'         =>  $student_name,  
':student_roll_number'  =>  $student_roll_number,
':student_dob'          =>  $student_dob,
':student_gender'       =>  $student_gender,
':student_grade_id'     =>  $student_grade_id,
':student_card'         =>  $student_card,
':student_id'           =>  $_POST["student_id"]
);
$query = "
UPDATE tbl_student 
SET student_name = :student_name, 
student_roll_number = :student_roll_number, 
student_dob = :student_dob,
student_gender = :student_gender,
student_grade_id = :student_grade_id, 
student_card = :student_card
WHERE student_id = :student_id
";
$statement = $connect->prepare($query);
if($statement->execute($data))
{
$output = array(
'success'       =>  'Data Edited Successfully',
);
}
}
}
echo json_encode($output);
}
if($_POST["action"] == "edit_fetch")
{
$query = "
SELECT * FROM tbl_student 
WHERE student_id = '".$_POST["student_id"]."'
";
$statement = $connect->prepare($query);
if($statement->execute())
{
$result = $statement->fetchAll();
foreach($result as $row)
{
$output["student_name"] = $row["student_name"];
$output["student_roll_number"] = $row["student_roll_number"];
$output["student_dob"] = $row["student_dob"];
$output["student_gender"] = $row["student_gender"];
$output["student_grade_id"] = $row["student_grade_id"];
$output["student_card"] = $row["student_card"];
$output["student_id"] = $row["student_id"];
}
echo json_encode($output);
}
}
if($_POST["action"] == "delete")
{
$query = "
DELETE FROM tbl_student 
WHERE student_id = '".$_POST["student_id"]."'
";
$statement = $connect->prepare($query);
if($statement->execute())
{
echo 'Data Delete Successfully';
}
}
}
?>

please help..

How to&Answers: