hi can someone help me??i was wondering on how i would use the update statement in sql in a way that it will only update selected fields??
i have a form wherein the user can edit some of the records in a way that the user will only edit some of the data in form and not all of it..i tried using the ordinary update statement but then whenever the user would only edit 1 field and save it..the rest are all blank..meaning that some fields with be left unchanged..i hope you understand me..i really can't fully explain myself..thank you
here is my sample update statement but it won't work..thank you
update table set c1 = '$c1' or c2 = '$c2' where c3 = '$c3'
i have a form wherein i can edit the records but..the fields are 'c1' and 'c2'..but i only want to edit the field 'c1' so i will only type in the textbox of 'c1'..and i would left 'c2' blank..but that blank means that i want to be left unchanged...but i cannot do it..help..thank you
update with 'or'
Moderator: General Moderators
Re: update with 'or'
I think PHP / server-side code should generate queries based on the input
For example:
For example:
Code: Select all
$changes = array();
if ($a != '') {
$changes[] = 'a = "' . sql_escape_string($a).'"';
}
if ($b != '') {
$changes[] = 'b = "' . sql_escape_string($b).'"';
}
if (count($changes)) { // make sure at least one field is filled
$sql = sprintf('UPDATE table SET %s WHERE c = "%s", implode(',', $changes), $c);
}