Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Update only edited/changed fields

Tags:

php

mysql

I have a form that can be edited by users. When changes are made, I want only those changes to update. A member of staff has pressed "save" before the whole page has loaded meaning the script saw the form fields as being empty so replaced the fields with empty ones.

Current code is:

$res= mysql_query ("UPDATE table SET site='$site', the1='$the1' WHERE id='$id'");

Appreciate the help here.....

like image 341
Mitch Avatar asked Sep 20 '26 20:09

Mitch


2 Answers

You need write javascript script for check data changes and send only changed data. For example, you can save previous data in data-prev-value attribute.

<input type="text" name="my_field" id="field_id" value="old_value" data-prev-value="old_value">

On submit, you compare

$('#field_id').val() != $('#field_id').attr('data-prev-value')

and if they not equal add data to array and send it to server.

like image 151
Ivan Bolnikh Avatar answered Sep 23 '26 08:09

Ivan Bolnikh


Here is a suggestion:

$res= mysqli_query("SELECT * from table where id ='$id'");

Fetch the result to array. After selecting that data you need to compare them with your POST data if your db fetched data and your posted data is not same then you will set new value.

if($_POST['name'] == $res['name']){
    $name = $res['name'];
}
else{
$name = $_POST['name'];
}
like image 31
Sheikh Muhammad Talha Avatar answered Sep 23 '26 10:09

Sheikh Muhammad Talha