update with 'or'

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
enna
Forum Newbie
Posts: 8
Joined: Thu Jun 04, 2009 4:54 am

update with 'or'

Post by enna »

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
User avatar
Darhazer
DevNet Resident
Posts: 1011
Joined: Thu May 14, 2009 3:00 pm
Location: HellCity, Bulgaria

Re: update with 'or'

Post by Darhazer »

I think PHP / server-side code should generate queries based on the input
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);
}
Post Reply