I have a form on my school web application where teachers are able to add scores for students and at the end of each row, the total, which is the sum of input values is automatically calculated per student.
The problem is that it output the total of all the students instead of the total of individual students on all the rows even ones with no scores added.
How do I get the unique total value for each row?
Below is my code
<?php if($class_id >= 1 && $class_id <= 4 && $student['is_secmid'] == 1){?>
<form method="post" action="<?php echo site_url('admin/examgroup/entrymarks') ?>" id="assign_form1111">
<input type="hidden" id="max_mark" value="<?php echo $subject_detail->max_marks; ?>">
<?php
if (isset($resultlist) && !empty($resultlist)) {
?>
<div class="row">
<div class="col-md-12">
<input type="hidden" name="exam_group_class_batch_exam_subject_id" value="<?php echo $exam_group_class_batch_exam_subject_id; ?>">
<div class="table-responsive">
<table class="table table-striped">
<thead>
<tr>
<th>C.A 1 (10.0)</th>
<th>C.A 2 (10.0)</th>
</tr>
</thead>
<tbody>
<?php if (empty($resultlist)) {
?>
<tr>
<td colspan="7" class="text-danger text-center"><?php echo $this->lang->line('no_record_found'); ?></td>
</tr>
<?php
} else {
foreach ($resultlist as $student) {
?>
<tr class="std_adm_<?php echo $student['admission_no']; ?>">
<input type="hidden" name="prev_id[<?php echo $student['exam_group_class_batch_exam_students_id'] ?>]" value="<?php echo $student['exam_group_exam_result_id'] ?>">
<input type="hidden" name="exam_group_student_id[]" value="<?php echo $student['exam_group_class_batch_exam_students_id'] ?>">
<td><?php echo $student['admission_no']; ?></td>
<td style="white-space: nowrap;"><?php echo $student['lastname'] . " " . $student['firstname']; ?></td>
<td> <input type="number" class="marksssss2 form-control" min="0" max="10" name="exam_group_student_ca1_<?php echo $student['exam_group_class_batch_exam_students_id']; ?>" value="<?php echo $student['exam_group_exam_result_get_ca1']; ?>" step="any"></td>
<td> <input type="number" class="marksssss3 form-control" min="0" max="10" name="exam_group_student_ca2_<?php echo $student['exam_group_class_batch_exam_students_id']; ?>" value="<?php echo $student['exam_group_exam_result_get_ca2']; ?>" step="any"></td>
<td> <output class="result"></output></td>
</tr>
<?php
}
}
?>
</tbody>
</table>
</div>
<?php if ($this->rbac->hasPrivilege('exam_marks', 'can_edit')) { ?>
<button type="submit" class="allot-fees btn btn-primary btn-sm pull-right" id="load" data-loading-text="<i class='fa fa-spinner fa-spin '></i> Please Wait.."><?php echo $this->lang->line('save'); ?>
</button>
<?php } ?>
</div>
</div>
<?php } ?>
<script>
const $inputs = $('input[type="number"]')
$inputs.change(function() {
var total = 0;
var parent = $(this).closest('.row');
parent.find('input[type="number"]').each(function() {
if ($(this).val() != '') {
total += parseInt($(this).val());
}
});
parent.find('.result').html(total);
});
</script>
you missed class row on <tr> and you are trying to find it on closest div from javascript, and you are getting this (<div class="row">), it have all the (<input type="number").
you forget to add class to TR. Add class row on <tr> so you can get all (<input type="number") inside the tr.
because of not added class on TR you are facing issue that all total have same numbers.
I want to change the background color of a specific clicked table row. It also should change it back to the original color. Back and forth from red to green. And it should also check the checkbox. Green = checkbox checked/ Red = checkbox unchecked.
The rows look like this
<tr id="<?php echo $row['id'] ?>" value="<?php echo $row['id'] ?>">
and for JQUERY I have this:
$(document).ready(function() {
$('.table tr').click(function(event) {
var id = $(this).attr('id');
var bcolor = $('.task').css('background-color');
console.log(id);
console.log(bcolor);
if (event.target.type !== 'checkbox') {
$(':checkbox', this).trigger('click');
if (bcolor == 'rgb(205, 92, 92)') {
$('.task').css('background-color', 'green');
} else {
$('.task').css('background-color', 'indianred');
}
};
});
});
How can I use the var id = $(this).attr('id'); to change the background color only for the specific clicked row?
Right now it makes every row green by clicking on any of them.
Whole main.php:
Header
<?php include 'taskpdo.php';?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>To Do List</title>
<link rel="stylesheet" type="text/css" href="style.css">
<script src="jquery-3.4.1.min.js"></script>
<script src="script.js"></script>
</head>
Body
<body>
<!-- CONFIRM MESSAGE -->
<?php if (isset($_SESSION['message'])): ?>
<div class="msg">
<?php
echo $_SESSION['message'];
unset($_SESSION['message']);
?>
</div>
<?php endif?>
<div class="spacer">
<!-- Heading -->
<div class="heading">
<h2>To Do List</h2>
</div>
<!-- Userinformation -->
<div class="logout">
<?php if (isset($_SESSION['username'])): ?>
<p>Welcome<strong><?php echo $_SESSION['username']; ?></strong></p>
<p>logout</p>
<?php endif?>
</div>
</div>
Table
<table class="table">
<thead>
<tr>
<th>Check</th>
<th>UID</th>
<th>Username</th>
<th>Num</th>
<th>Tasks</th>
<th>Edit</th>
<th>Delete</th>
</tr>
</thead>
<tbody>
<?php
$tasks = sqlsrv_query($db,
"SELECT * FROM todo
join registrieren
on username = ersteller
where username = '" . $_SESSION['username'] . "'");
Rows
$i = 1;
while ($zeile = sqlsrv_fetch_array($tasks)) {
?>
<tr id="<?php echo $zeile['id'] ?>" type="checkbox" name="check[]" value="<?php echo $zeile['id'] ?>">
<td class="task"><input type="checkbox" id="check--<?php echo $zeile['id'] ?>"></td>
<input type="hidden" name="id" value="<?php echo $zeile['id']; ?>">
<td class="task" ><?php echo $zeile['uid'] ?></td>
<td class="task" ><?php echo $zeile['username'] ?></td>
<td class="task" ><?php echo $zeile['id'] ?></td>
<td class="task" id="<?php echo $zeile['id'] ?>" ><?php echo $zeile['task'] ?></td>
<td class="task" onclick="myFunction(<?php echo $zeile['id'] ?>)">
<form method="post" action="main.php">
<button type="submit" name="edit">
Edit
</button>
<input type="hidden" name="id" value="<?php echo $zeile['id']; ?>">
<input type="hidden" name="task" value="<?php echo $zeile['task']; ?>">
</form>
</td>
<td class="task" onclick="myFunction(<?php echo $zeile['id'] ?>)">
<form method="post" action="main.php" >
<button type="submit" name="delete">
Delete
</button>
<input type="hidden" name="id" value="<?php echo $zeile['id']; ?>">
</form>
</td>
</tr>
<?php
$i++;}
?>
</tbody>
</table>
Buttons
<form method="post" action="main.php" class="input_form">
<input type="hidden" name="ersteller" value="<?php echo $_SESSION['username']; ?>">
<?php
if ($update == true): ?>
<input type="text" name="task" class="task_input" value="<?php echo $task ?>">
<button type="submit" name="update" id="add_btn" class="add_btn">Update</button>
<input type="text" name="id" value="<?php echo $id ?>">
<?php else: ?>
<input type="text" name="task" class="task_input">
<button type="submit" name="addtask" id="add_btn" class="add_btn">Add Task</button>
<input type="hidden" name="id" value="<?php echo $id ?>">
<?php endif ?>
</form>
</body>
</html>
You can use toggleClass of jQuery:
// css for put in your element
.common-class {
background-color: #ff0000; // replace for your color
}
.highlight-class {
background-color: #0000ff !important; // replace for your color
}
$('your-selector').toggleClass('highlight-class'); // jQuery
Bensilson's answer helped me and i want to clarify what i have done:
I delete the class='task'inside the <td> and put it into the <tr> - Tag.
I've done a .highlighttask{background-color: green !important} inside the css.
And inside the js i've done
$(document).ready(function() {
$(".table tr").on('click', function() {
var checked = $(this).find('input[type="checkbox"]');
checked.prop('checked', !checked.is(':checked'));
$(this).toggleClass('highlighttask');
});
});
Now it overrights the background-color on a specific row AND it checks the checkbox of the specific row.
Thanks!
I have a form contain looping input tag that is used for adding data. the looping form is work, and the problem is i didn't know how to retrieve that looping data in controller code igniter. please help!
this is my view (form)
<form action="#" id="ap_data">
<div class="table-responsive">
<table class="table table-striped table-hover" id="mkt">
<thead>
<tr>
<th>KR</th>
<th>NP</th>
<th>KP</th>
</tr>
</thead>
<tbody>
<?php $number =0;
$id = "id";
$idkr = "idkr";
$np = "np";
$idkp = "idkp";?>
<?php foreach ($perb as $value) {
$number++; ?>
<tr>
<input type="hidden" name="<?php echo $id.$number; ?>" value="<?php echo $value->idp ?>"/>
<td>
<input type="text" placeholder="" name="<?php echo $idkr.$number; ?>" value="<?php echo $value->idkp ?>" class="form-control">
</td>
<td>
<select name="<?php echo $np.$number; ?>" class="form-control">
<option value="1">1 (AS)</option>
<option value="2">2 (Bold)</option>
<option value="3">3 (Seed)</option>
</select>
</td>
<td>
<input type="text" placeholder="" name="<?php echo $idkp.$number; ?>" value="<?php echo $value->idkp ?>" id="class="form-control">
</td>
</tr>
<?php } ?>
</tbody>
</table>
</div>
<div class="form-group">
<button type="button" onclick="test_kond()" class="btn btn-success col-lg-12">Test Kond</button>
</div>
</form>
here' is my view (Ajax function)
<script type="text/javascript">
var test_kond;
$( document ).ready(function() {
test_kond = function () {
var data_ap;
data_ap = $("#ap_data").serialize();
$.ajax({
type: "POST",
url: "<?php echo site_url('HoldBotCtrl/kond_ajax')?>",
data: data_app,
success: function() {
alert(data_ap);
}
});
};
});
and here's my controller
function kond_ajax(){
$k_t = $this->Hb_model->select_kr();
$j_kr = count($kr_total);
$idkr = "idkr";
$np = "np";
$idkp ="idkp";
for ($i=1; $i <= $jj_kr ; $i++) {
$idkrr = $idkr.$i;
$npp = $np.$i;
$idkpp = $idkp.$i;
$data_ap = array(
$idkrr => $this->input->post($idkrr),
$npp => $this->input->post($npp),
$idkpp => $this->input->post($idkpp)
);
print_r($data_ap);
}
and this is result of print_r
idkr1Array ( [idkr1] => [np1] => [idkp1] => ) idkr2Array ( [idkr2] => [np2] => [idkp2] => )
the value still null, i need to get the data from $this->input->post();.
how to fix this??
I think serilize() method creates a URL encoded string where as you want to send data via POST method in ajax. You could try this way.
var test_kond;
$( document ).ready(function() {
test_kond = function () {
var data_ap;
data_ap = new FormData($("#ap_data"));
$.ajax({
type: "POST",
url: "<?php echo site_url('HoldBotCtrl/kond_ajax')?>",
data: data_app,
success: function() {
alert(data_ap);
}
});
};
});
Just try by changing field names like below:
<form action="#" id="ap_data">
<div class="table-responsive">
<table class="table table-striped table-hover" id="mkt">
<thead>
<tr>
<th>KR</th>
<th>NP</th>
<th>KP</th>
</tr>
</thead>
<tbody>
<?php $number =0;
$id = "id";
$idkr = "idkr";
$np = "np";
$idkp = "idkp";?>
<?php foreach ($perb as $value) {
$number++; ?>
<tr>
<input type="hidden" name="data[<?= $number ?>]['id']" value="<?php echo $value->idp ?>"/>
<td>
<input type="text" placeholder="" name="data[<?= $number ?>]['idkr']" value="<?php echo $value->idkp ?>" class="form-control">
</td>
<td>
<select name="data[<?= $number ?>]['np']" class="form-control">
<option value="1">1 (AS)</option>
<option value="2">2 (Bold)</option>
<option value="3">3 (Seed)</option>
</select>
</td>
<td>
<input type="text" placeholder="" name="data[<?= $number ?>]['idkp']" value="<?php echo $value->idkp ?>" id="" class="form-control">
</td>
</tr>
<?php } ?>
</tbody>
</table>
</div>
<div class="form-group">
<button type="button" onclick="test_kond()" class="btn btn-success col-lg-12">Test Kond</button>
</div>
</form>
and in your controller :
function kond_ajax(){
print_r($this->input->post());
}
I hope this will help :)
I have an account form in which the all account are being pulled from my mysql database into a dropdown menu. The user selects the account from the dropdown menu then the account code field is automatically populated via the data-acc attribute. I have added a button to add more account.
It works at first set of input and select box, but not in the next one
sample screenshot
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script>
<script>
$('select[name="account_name[]').change(function()
{
$('.account_num').val($('select[name="account_name[]"] option:selected').data('acc'));
});
</script>
<script>
$(document).ready(function(){
var i=1;
var chart_add = '<tr id="row_chart'+i+'"><td align="center"> <input type="text" name="account_code[]" class="form-control account_num" readonly></td><td><select class="form-control selectpicker" data-live-search="true" name="account_name[]" required> <option></option><?php $account = mysqli_query($conn,"SELECT chart_of_account.acoount_no, chart_of_account.account_title from chart_of_account inner join account_group_tbl on chart_of_account.account_group = account_group_tbl.account_name where account_group_tbl.account_type = 'Expense'");while($acc = mysqli_fetch_assoc($account)){$account_no = $acc['acoount_no'];$account_title = $acc['account_title'];?><option value="<?php echo $account_title; ?>" data-price="<?php echo $account_no; ?>"><?php echo $account_title ; ?> </option><?php } ?></select></td><td><button type="button" name="remove_chart" id="'+i+'" class="btn btn-danger remove_chart"><i class="fa fa-minus"></i></button></td></tr>';
$('#add').click(function(){
i++;
$('#dynamic_field').append(chart_add);
$('.selectpicker').selectpicker('render');
});
$(document).on('click', '.remove_chart', function(){
var button_id = $(this).attr("id");
$('#row_chart'+button_id+'').remove();
});
});
</script>
<div class="row">
<div class="col-md-12">
<div class="box box-default">
<div class="box-header with-border">
<h3 class="box-title">Report Per Office</h3>
</div>
<div class="box-body">
<div class="col-md-12">
<h4>Project Procurement Management Plan (PPMP) <hr >
</div>
<div class="col-md-1">
Office:
</div>
<div class="col-md-4">
<select class="selectpicker" data-live-search="true" name="office_from" data-width="auto" required >
<option>--Select Reponsible Office--</option>
<?php
$q = "SELECT * FROM `office_code_dept`";
$r = mysqli_query($conn,$q);
while($row = mysqli_fetch_assoc($r)){
$off_desc = $row['off_name'];
?>
<option value="<?php echo $off_desc;?>"><?php echo $off_desc; ?></option>
<?php
}
?>
</select>
</div>
<div class="col-md-1">Year:</div>
<div class="col-md-4"><input type="number" min="1990" max="3000" name="tax_year" class="form-control" style="text-align: center;" value="<?php echo date('Y'); ?>"></div>
<div class="col-md-2"><input type="submit" name="search" value="Search" class="btn btn-primary btn-block">
</div>
<div class="col-md-12"><br></div>
</h4>
<table class="table table-bordered" id="dynamic_field">
<thead>
<tr>
<th width="7%" rowspan="2" style="text-align: center;">Code</th>
<th width="27%" rowspan="2" style="text-align: center;">General Description</th>
<th rowspan="2" width="5%"><button type="button" name="add" id="add" class="btn btn-success"><i class="fa fa-plus"></i></button></th>
</tr>
</thead>
<tbody>
<tr>
<td align="center">
<input type="text" name="account_code[]" class="form-control account_num" readonly>
</td>
<td>
<select class="form-control selectpicker" data-live-search="true" name="account_name[]" required>
<option></option>
<?php
$account = mysqli_query($conn,"SELECT chart_of_account.acoount_no, chart_of_account.account_title from chart_of_account inner join account_group_tbl on chart_of_account.account_group = account_group_tbl.account_name where account_group_tbl.account_type = 'Expense'");
while($acc = mysqli_fetch_assoc($account)){
$account_no = $acc['acoount_no'];
$account_title = $acc['account_title'];
?>
<option value="<?php echo $account_title; ?>" data-acc="<?php echo $account_no; ?>">
<?php echo $account_title ; ?>
</option>
<?php } ?>
</select>
</td>
<td></td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
</div>
your .change function is not bound to the new input and select box
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script>
<script>
$('select[name="account_name[]').change(function()
{
$('.account_num').val($('select[name="account_name[]"] option:selected').data('acc'));
});
</script>
<script>
$(document).ready(function(){
var i=1;
var chart_add = '<tr id="row_chart'+i+'"><td align="center"> <input type="text" name="account_code[]" class="form-control account_num" readonly></td><td><select class="form-control selectpicker" data-live-search="true" name="account_name[]" required> <option></option><?php $account = mysqli_query($conn,"SELECT chart_of_account.acoount_no, chart_of_account.account_title from chart_of_account inner join account_group_tbl on chart_of_account.account_group = account_group_tbl.account_name where account_group_tbl.account_type = 'Expense'");while($acc = mysqli_fetch_assoc($account)){$account_no = $acc['acoount_no'];$account_title = $acc['account_title'];?><option value="<?php echo $account_title; ?>" data-price="<?php echo $account_no; ?>"><?php echo $account_title ; ?> </option><?php } ?></select></td><td><button type="button" name="remove_chart" id="'+i+'" class="btn btn-danger remove_chart"><i class="fa fa-minus"></i></button></td></tr>';
$('#add').click(function(){
i++;
$('#dynamic_field').append(chart_add);
$('.selectpicker').selectpicker('render');
$('select[name="account_name[]').change(function()
{
$('.account_num').val($('select[name="account_name[]"] option:selected').data('acc'));
});
});
$(document).on('click', '.remove_chart', function(){
var button_id = $(this).attr("id");
$('#row_chart'+button_id+'').remove();
});
});
</script>
Something like this should work.
I am doing Add, Update and Delete in Php.
Everything is working fine but My Update query is not working.
Can I know, where i am going wrong in Update query?
Here is my update.php file code..
<?php
include('connection.php');
$ID = $_REQUEST['Student_Id'];
$result = mysql_query("select* from tblstudent where Student_Id='".$ID."'");
while($oldvalue= mysql_fetch_array($result))
{
$oldname=$oldvalue['Student_Name'];
$oldgender=$oldvalue['Gender'];
$olddob=$oldvalue['DOB'];
$oldaddress=$oldvalue['Address'];
$oldmobileno=$oldvalue['Phone'];
$olddivision=$oldvalue['Division'];
$oldclass=$oldvalue['Class'];
$oldemail=$oldvalue['Email_Id'];
}
if(isset ($_POST['submit']))
{
$update = $_POST['submit'];
if($update)
{
$newname=$_POST['Student_Name'];
$newgender=$_POST['Gender'];
$newdob=$_POST['DOB'];
$newaddress=$_POST['Address'];
$newmobileno=$_POST['Phone'];
$newdivision=$_POST['Division'];
$newclass=$_POST['Class'];
$newemail=$_POST['Email_Id'];
/* UPDATE QUERY */
mysql_query("UPDATE tblstuent SET Student_Name='$newname', Gender='$newgender', DOB='$newdob', Address='$newaddress', Phone='$newmobileno', Division='$newdivision', Class='$newclass', Email_Id='$newemail'
WHERE id='$ID'");
header('location:index.php');
}
}
?>
<body>
<form action="update.php" method="post">
<fieldset>
<legend>Personal Information</legend><br/>
<div class="studentname"><label>Student Name :</label><input type="text" name="newstudentname" id="studentnameId" value="<?php echo $oldname ?>" placeholder="Enter Name" size="30px" /></div><br/>
<div class="gender">
<label>Gender :</label>
<input type="radio" name="type" value="Male" <?php echo ($oldgender == 'Male') ? 'checked' : ''; ?> /> Male
<input type="radio" name="type" value="Female" <?php echo ($oldgender == 'Female') ? 'checked' : ''; ?>/> Female<br />
</div> <br/>
<div class="dob"><label>Date of Birth :</label><input type="text" name="dob" id="dobId" value="<?php echo $olddob ?>" placeholder="Enter DOB format Year-Month-DaY" size="30px" /></div><br/>
<div class="address"><label class="addresschild">Address : </label><textarea rows="4" cols="21" name="address" id="addressId" value="" placeholder="Enter Your Address"><?php echo $oldaddress ?></textarea></div><br/>
<div class="mobileno"><label>Parent's Mobile No : </label><input type="text" name="mobileno" id="mobilenoId" value="<?php echo $oldmobileno ?>" placeholder="Enter Parent's Mobile No" size="30px" /></div><br/>
<div class="selectdivision">
<label>Divison :</label>
<select id="divisiondropdownId" name="divisiondropdown">
<option value="0">Select Division</option>
<option value="A"<?php if($olddivision=="A") echo 'selected="selected"'; ?> >A</option>
<option value="B"<?php if($olddivision=="B") echo 'selected="selected"'; ?> >B</option>
<option value="C"<?php if($olddivision=="C") echo 'selected="selected"'; ?> >C</option>
</select>
</div><br/>
<div class="selectclass">
<label>Class :</label>
<select id="classdropdownId" name="classdropdown">
<option value="0">Select Class</option>
<option value="First"<?php if($oldclass=="First") echo 'selected="selected"'; ?>>First</option>
<option value="Second"<?php if($oldclass=="Second") echo 'selected="selected"'; ?>>Second</option>
<option value="Third"<?php if($oldclass=="Third") echo 'selected="selected"'; ?>>Third</option>
<option value="Fourth"<?php if($oldclass=="Fourth") echo 'selected="selected"'; ?>>Fourth</option>
<option value="Fifth"<?php if($oldclass=="Fifth") echo 'selected="selected"'; ?>>Fifth</option>
<option value="Sixth"<?php if($oldclass=="Sixth") echo 'selected="selected"'; ?>>Sixth</option>
<option value="Seventh"<?php if($oldclass=="Seventh") echo 'selected="selected"'; ?>>Seventh</option>
<option value="Eighth"<?php if($oldclass=="Eighth") echo 'selected="selected"'; ?>>Eighth</option>
<option value="Nineth"<?php if($oldclass=="Nineth") echo 'selected="selected"'; ?>>Nineth</option>
<option value="Tenth"<?php if($oldclass=="Tenth") echo 'selected="selected"'; ?>>Tenth</option>
</select>
</div><br/>
<div class="emailid"><label>Email-Id : </label><input type="text" name="emailid" id="emailId" value="<?php echo $oldemail ?>" placeholder="Enter your Email-id" size="30px" /></div><br/>
<div id="submit1">
<input class="btnsubmit" type="submit" name="submit" id="submit" value="Submit" />
<input class="btnreset" type="reset" name="reset" id="submit" value="Reset" />
</div><br/>
</fieldset>
</form>
</body>
Thank you
Rahul Barge
I have an example with PDO and prepared statements for you. Also if I was you I would start learning PDO with prepared statements. MySQL is deprecated and can get you into trouble.
This example is using Student_Id as auto increment and primary
SelectStudentPage.php
<!DOCTYPE>
<html>
<head>
<title>Students</title>
</head>
<body>
<?php
$db_host = "localhost";
$db_username = "root";
$db_pass = "";
$db_name = "Studentsdb";
$db = new PDO('mysql:host='.$db_host.';dbname='.$db_name,$db_username,$db_pass);
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING);
//prepared statement with PDO to query the database
$stmt = $db->prepare("SELECT * FROM tblstudent ");
$stmt->execute();
?>
<?php //start of the while loop ?>
<?php while( $row = $stmt->fetch(PDO::FETCH_ASSOC) ) { ?>
<table border="1" style="table-layout: fixed; width: 1080px;">
<br>
<tr>
<th style="width:125px">STUDENT ID</th>
<th style="width:125px">STUDENT NAME</th>
<th style="width:100px">GENDER</th>
<th style="width:100px">DOB</th>
<th style="width:250px">ADDRESS</th>
<th style="width:100px">PHONE</th>
<th style="width:100px">DIVISION</th>
<th style="width:100px">CLASS</th>
<th style="width:250px">EMAIL ID</th>
</tr>
<tr style="width:25px">
<?php $id = $row['Student_Id'];?>
<?php echo "<td> <a href='StudentUpdateForm.php?Student_Id=$id'>$id</a></td>"?>
<td><?php echo $row['Student_Name']; ?></td>
<td><?php echo $row['Gender']; ?></td>
<td><?php echo $row['DOB']; ?></td>
<td><?php echo $row['Address']; ?></td>
<td><?php echo $row['Phone']; ?></td>
<td><?php echo $row['Division']; ?></td>
<td><?php echo $row['Class']; ?></td>
<td><?php echo $row['Email_Id']; ?></td>
</tr>
</table>
<?php } //end of the while loop?>
</body>
</html>
StudentUpdateForm.php
<?php
$db_host = "localhost";
$db_username = "root";
$db_pass = "";
$db_name = "Studentsdb";
$db = new PDO('mysql:host='.$db_host.';dbname='.$db_name,$db_username,$db_pass);
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING);
$id=$_GET['Student_Id'];
$result = $db->prepare("SELECT * FROM tblstudent Where Student_Id=:Student_Id");
$result->bindParam(':Student_Id', $id);
$result->execute();
for($i=0; $row = $result->fetch(); $i++){
?>
<!DOCTYPE>
<html>
<head>
<title>Example Update Form</title>
</head>
<body>
<form action="UpdateProcess.php" method="post">
<legend>Personal Information</legend><br>
<div>
<label>Student Id :<label><input name="Student_Id" type="text" value=
"<?php print($row['Student_Id']) ?>">
</div><br>
<div>
<label>Student Name :</label><input name="Student_Name" type="text" value=
"<?php print($row['Student_Name']) ?>">
</div><br>
<div>
<label>Gender :</label>
<select name ="Gender" style="width: 149px" >
<option value <?php if ($row['Gender']==1){ print('selected');} ?> ="Male">Male</option>
<option value <?php if ($row['Gender']==2){ print('selected');} ?> ="Female">Female</option>
</select>
</div><br>
<div>
<label>Date of Birth :</label><input name="DOB" type="text" value=
"<?php print($row['DOB']) ?>">
</div><br>
<div>
<label>Address :</label><textarea name="Address"><?php echo $row['Address']; ?></textarea><br>
</div><br>
<div>
<label>parents mobile no:</label><input name="Phone" type="text"value=
"<?php print($row['Phone']) ?>">
</div><br>
<div>
<label>Divison :</label><br>
<select name ="Division" style="width: 149px" >
<option value <?php if ($row['Division']==1){ print('selected');} ?> ="A">A</option>
<option value <?php if ($row['Division']==2){ print('selected');} ?> ="B">B</option>
<option value <?php if ($row['Division']==3){ print('selected');} ?> ="C">C</option>
</select>
</div><br>
<div>
<label>Class :</label><br>
<select name ="Class" style="width: 149px" >
<option value <?php if ($row['Class']==1){ print('selected');} ?> ="First">First</option>
<option value <?php if ($row['Class']==2){ print('selected');} ?> ="Second">Second</option>
<option value <?php if ($row['Class']==3){ print('selected');} ?> ="Third">Third</option>
</select>
</div><br>
<div>
<label>Email id :</label><input name="Email_Id" type="text" value=
"<?php print($row['Email_Id']) ?>">
</div><br>
<div id="submit1">
<input class="btnsubmit" id="submit" name="submit" type="submit"
value="Update"> <input class="btnreset" id="submit" name="reset"
type="reset" value="Reset">
</div><br>
</form>
</body>
</html>
<?php } ?>
UpdateProcess.php
<?php
$db_host = "localhost";
$db_username = "root";
$db_pass = "";
$db_name = "Studentsdb";
try{
$db = new PDO('mysql:host='.$db_host.';dbname='.$db_name,$db_username,$db_pass);
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING);
$sql = 'UPDATE tblstudent SET Student_Id=:Student_Id, Student_Name=:Student_Name, Gender=:Gender, DOB=:DOB, Address=:Address, Phone=:Phone, Division=:Division, Class=:Class, Email_Id=:Email_Id WHERE Student_Id=:Student_Id';
$stmt = $db->prepare($sql);
$stmt->bindParam(':Student_Id', $_POST['Student_Id'], PDO::PARAM_STR);
$stmt->bindParam(':Student_Name', $_POST['Student_Name'], PDO::PARAM_STR);
$stmt->bindParam(':Gender', $_POST['Gender'], PDO::PARAM_STR);
$stmt->bindParam(':DOB', $_POST['DOB'], PDO::PARAM_STR);
$stmt->bindParam(':Address', $_POST['Address'], PDO::PARAM_STR);
$stmt->bindParam(':Phone', $_POST['Phone'], PDO::PARAM_STR);
$stmt->bindParam(':Division', $_POST['Division'], PDO::PARAM_STR);
$stmt->bindParam(':Class', $_POST['Class'], PDO::PARAM_STR);
$stmt->bindParam(':Email_Id', $_POST['Email_Id'], PDO::PARAM_STR);
$stmt->execute();
echo $stmt->rowCount() . " record Updated successfully.";
}catch(PDOException $exception){
echo "Error: " . $exception->getMessage();
}
echo "<a href=http://localhost/students/SelectStudentPage.php>Go to Grid view Results page</a>";
?>
along with having typo,you used
$ID = $_REQUEST['Student_Id'];
in your code i dont found any element with id 'Student_Id' in your form, so there will be no value in $ID, and no update will be done.
Sorry for not commenting. I don't have 50 points yet.
I saw few errors in your query.
table name. in select query it's tblstudent and in update query its tblstuent
'$newname' is better to concatinate like this. '".$newname."'. and this should do for all variables.
No STUDENT_ID in your code. So add it as a hidden field.
Make sure column names spelled in the same way as it is in the table. simple/capital and spellings.
if you are updating same table than you should pass Student_Id instead of id in below query,
mysql_query("UPDATE tblstuent SET Student_Name='$newname', Gender='$newgender', DOB='$newdob', Address='$newaddress', Phone='$newmobileno', Division='$newdivision', Class='$newclass', Email_Id='$newemail'
WHERE id='$ID'");
or You had set wrong table name tblstuent , it should be tblstudent if you are updating same table.
Try This:
Student_Name='$newname',
put $newname in double quotes like this.
Student_Name="$newname",
In your query check data type of all column then as data type you need to set column value with quotes if datatype like string,date. otherwise not need to set quotes for good practice. and in your update query you write table name wrong so first update this. after check
$sql = "SELECT * FROM table WHERE id = '$id' " ;
while($oldvalue= mysql_fetch_array($result))
{
$oldname=$oldvalue['Student_Name'];
$oldgender=$oldvalue['Gender'];
$olddob=$oldvalue['DOB'];
$oldaddress=$oldvalue['Address'];
$oldmobileno=$oldvalue['Phone'];
$olddivision=$oldvalue['Division'];
$oldclass=$oldvalue['Class'];
$oldemail=$oldvalue['Email_Id'];
}
if(mysql_query("DESCRIBE `table`")) {
$sql = "UPDATE table SET ";
$sql.= " Student_Name = '$oldname', Gender = '$oldgender',.... ";
$sql.= " WHERE id = '$id' ";
if(mysql_query($sql)){
echo 'Good';
}
else
{
echo 'Bad';
}
}
Try something like this ;)
Hello Friends thank you,
All your answers helped me to solve problem.
I learn alot from you all.
Finally i solved above problem doing some changes in update.php file code..
Here is my new code
<?php
include('connection.php');
if(isset ($_REQUEST['Student_Id']))
{
$id = $_REQUEST['Student_Id'];
$result = mysql_query("select* from tblstudent where Student_Id ='".$id."'");
while($oldvalue= mysql_fetch_array($result))
{
$oldid = $oldvalue['Student_Id'];
$oldname=$oldvalue['Student_Name'];
$oldgender=$oldvalue['Gender'];
$olddob=$oldvalue['DOB'];
$oldaddress=$oldvalue['Address'];
$oldmobileno=$oldvalue['Phone'];
$olddivision=$oldvalue['Division'];
$oldclass=$oldvalue['Class'];
$oldemail=$oldvalue['Email_Id'];
}
}
if(isset ($_POST['newname']))
{
$newname =$_POST['newname'];
$newid =$_POST['newid'];
$newgender =$_POST['newgender'];
$newdob = $_POST['newdob'];
$newaddress = $_POST['newaddress'];
$newphone = $_POST['newphone'];
$newdivision = $_POST['newdivision'];
$newclass = $_POST['newclass'];
$newemailid = $_POST['newemailid'];
$sql= "UPDATE tblstudent SET Student_Name='$newname', Gender='$newgender', DOB='$newdob', Address='$newaddress', Phone='$newphone', Division='$newdivision', Class='$newclass', Email_Id='$newemailid' WHERE Student_Id='$newid'";
$result= mysql_query($sql);
header('location:index.php');
}
?>
<body>
<form action="update.php" method="post">
<fieldset>
<legend>Personal Information</legend><br/>
<div class="studentname"><label>Student Name :</label><input type="text" name="newname" id="studentnameId" value="<?php echo $oldname ?>" placeholder="Enter First & Last Name" size="30px" /></div><br/>
<input type="hidden" name="newid" value="<?php echo $oldid ?>"/>
<div class="gender">
<label>Gender :</label>
<input type="radio" name="newgender" value="Male" <?php echo ($oldgender == 'Male') ? 'checked' : ''; ?> /> Male
<input type="radio" name="newgender" value="Female" <?php echo ($oldgender == 'Female') ? 'checked' : ''; ?>/> Female<br />
</div> <br/>
<div class="dob"><label>Date of Birth :</label><input type="text" name="newdob" id="dobId" value="<?php echo $olddob ?>" placeholder="Enter DOB format Year-Month-DaY" size="30px" /></div><br/>
<div class="address"><label class="addresschild">Address : </label><textarea rows="4" cols="21" name="newaddress" id="addressId" value="" placeholder="Enter Your Address"><?php echo $oldaddress ?></textarea></div><br/>
<div class="mobileno"><label>Parent's Mobile No : </label><input type="text" name="newphone" id="mobilenoId" value="<?php echo $oldmobileno ?>" placeholder="Enter Parent's Mobile No" size="30px" /></div><br/>
<div class="selectdivision">
<label>Divison :</label>
<select id="divisiondropdownId" name="newdivision">
<option value="0">Select Division</option>
<option value="A"<?php if($olddivision=="A") echo 'selected="selected"'; ?> >A</option>
<option value="B"<?php if($olddivision=="B") echo 'selected="selected"'; ?> >B</option>
<option value="C"<?php if($olddivision=="C") echo 'selected="selected"'; ?> >C</option>
</select>
</div><br/>
<div class="selectclass">
<label>Class :</label>
<select id="classdropdownId" name="newclass">
<option value="0">Select Class</option>
<option value="First"<?php if($oldclass=="First") echo 'selected="selected"'; ?>>First</option>
<option value="Second"<?php if($oldclass=="Second") echo 'selected="selected"'; ?>>Second</option>
<option value="Third"<?php if($oldclass=="Third") echo 'selected="selected"'; ?>>Third</option>
<option value="Fourth"<?php if($oldclass=="Fourth") echo 'selected="selected"'; ?>>Fourth</option>
<option value="Fifth"<?php if($oldclass=="Fifth") echo 'selected="selected"'; ?>>Fifth</option>
<option value="Sixth"<?php if($oldclass=="Sixth") echo 'selected="selected"'; ?>>Sixth</option>
<option value="Seventh"<?php if($oldclass=="Seventh") echo 'selected="selected"'; ?>>Seventh</option>
<option value="Eighth"<?php if($oldclass=="Eighth") echo 'selected="selected"'; ?>>Eighth</option>
<option value="Nineth"<?php if($oldclass=="Nineth") echo 'selected="selected"'; ?>>Nineth</option>
<option value="Tenth"<?php if($oldclass=="Tenth") echo 'selected="selected"'; ?>>Tenth</option>
</select>
</div><br/>
<div class="emailid"><label>Email-Id : </label><input type="text" name="newemailid" id="emailId" value="<?php echo $oldemail ?>" placeholder="Enter your Email-id" size="30px" /></div><br/>
<div id="submit1">
<input class="btnsubmit" type="submit" name="submit" id="submit" value="Submit" />
<input class="btnreset" type="reset" name="reset" id="submit" value="Reset" />
</div><br/>
</fieldset>
</form>
</body>
Thank you :-)