I have more than 30 fields and I am trying to save the data in the database automatically.
I am able to insert the data in the database but it's continuously inserting. It's not updating the last record. It's not checking the else part.
I am getting the total count of the stud_id
and updating the record with the help of stud_id
. Is it a good idea?
public function student_autosave($data){
$sql_count_id="SELECT COUNT(stud_id) FROM student_info";
$q = $this->db->query($sql_count_id);
$last_count=$q->num_rows();
if ($last_count == 1) {
$this->db->insert('student_info',$data);
}else{
$query = $this->db->where(['stud_id'=>$last_count])->update('student_info',$data);
if($this->db->affected_rows()>0){
return 1;
}else{
return 0;
}
}
}
Autosave script
$(document).ready(function() {
timePicker(10);
});
var s;
function timePicker(vr) {
if (vr > 0) {
if (vr > 1) {
$('#timer').html('Data will be updated in next '+ vr+' secounds');
} else {
$('#timer').html('Data will be updated in next 1 secound');
}
vr--;
s = setTimeout('timePicker(' + vr + ')', 1000);
} else {
clearInterval(s);
$.ajax({
type: 'post',
url: '<?php echo base_url("index.php/Student_controller/student_data_autosave"); ?>',
data: $('form[name="student_formname"]').serialize(),
success: function (data) {
s = setTimeout('timePicker(' + 10 + ')', 5000);
return false;
}
});
}
}
Need small correction, like below
num_rows()
- Gives the number of rows returned by the query, that is number of rows that the result has fetched when selecting, this is not count, you have to access count separately
$sql_count_id="SELECT COUNT(stud_id) as count FROM student_info";
// perform your query,
$q = $this->db->query($sql_count_id);
// if there were rows returned and
if($q->num_rows() ){
// count ==1 then
if($q->row()->count == 1){
// perform your insert
}else{
// perform your update
}
}else{
// something went wrong with your query
// should return atleast 1 even if count is zero
}
My new answer to this is: Redesign your process.
- Short of generating a unique form id for new users on every new page load, you won't be able to confidently differentiate between users. Furthermore if something happens to a user mid-form and the form id is "lost", then the user will have to start all over because the next time they load a form, it will have a new random generated id. (well, the deeper truth is, you could probably find a way preserve a form id when things go wrong, but it is an unnecessary convolution)
- Using a timer to auto-save is an unnecessary feature and will certainly lead to wasteful/undesirable calls to the server/database. Imagine someone starts filling the form, goes to the toilet, has a coffee, takes a phone call to a relative, has to pick up the kids from school, remembers that a few things are needed from the grocery store... you get the idea. In the meantime, your page is auto-saving over and over. There is absolutely no benefit to putting this zombie on the treadmill.
When I immigrated to Australia, I had to fill government forms online. The forms were long. They did the sensible thing; they broke the form up into chunks of 5-7 questions/fields and offered Back/Next buttons. Every time I clicked one of those buttons, my progress was saved. It was not a horribly clunky experience and the truth is, I did need to step away or make a phone call to retrieve some personal information for the form. These forms, once I submitted so much as an email address, were assigned a permalink (which was emailed to me asap) so that even if I became disconnected, I could pick right up where I left off.
Using this story, my recommendation is this:
- Page 1 of your form must be the bare minimum contact information, so that your program can assign an id (auto-incremented primary key) to each new user.
- Because I recommend that you email a permalink to the user, you can double your benefit by using it to verify that the user has actually used an email that is real and that they can access. This means that they will enter their name & email (& password), the form will pause while the user receives your automated email, they click the permalink that automatically places them on the page that they need to continue on, and you get confirmation that the registration is (probably) legitimate.
- Every time your user clicks Back/Next, you update the database. You can do this with ajax or you can submit the page the simple/old-school way with pure html/php.
- No matter how slow the user is, or if they suffer a crash/disconnection, your program will not perform any mindless-calls and you can even build in some very necessary validations and checkpoints.
Lastly, always sanitize and validate everything that a user submits before using the data in a query -- likely always and forever.