Reputation: 31
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.....
Upvotes: 3
Views: 5101
Reputation: 11
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'];
}
Upvotes: 1
Reputation: 88
Another solution could be to set the value of all fields as what the current value is in the database. That way, they know what the current value is and would be easier for them to know what to change, and when the form is submitted, it'll just resubmit the info in the form whether it was changed or not.
For example:
<input type="text" name="firstname" value="<?php echo $row['firstname']; ?>" required>
when the form is submitted, it'll keep the same data that was currently there, or update it if the value was changed in the input field
Upvotes: 0
Reputation: 752
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.
Upvotes: 2