-1

ive googled for 1½ hour and cant come to an understanding how to do this.

i want to update my current row, but keep values that are already assigned. i currently have a checkbox which either gives "1" or "0". if the checkbox returns "0" i dont want it to update it as there's no point to it updating. however i dont know how to do this.

if i get col1 as a duplicate entry, then it should update col2,3,4,5 if the entry isnt already filled by a "1" or if the variable gotten from my form is "0".

i could do this with a lot of if statements but id prefer not to. and be aware that my code is extremely messy right now because im just trying to find a solution. the code will be fixed when i get the result i want to. also the code is written in mysql because im more familiar in it and will be ported to pdo when its done. please dont tell me "dont use mysql" and dont give me an answer. already gotten those often enough

 $insert_movie = "INSERT INTO filmer (namn, rel_720p, rel_1080p, rel_dvdr, swesub, addate)
 VALUES ('$namn','$rel_720p','$rel_1080p','$rel_dvdr','$swesub',CURRENT_TIMESTAMP)
 ON DUPLICATE KEY UPDATE
 rel_720p = IF('$rel_720p' IS NOT NULL, '$rel_720p', rel_720p), rel_1080p = IF('$rel_1080p' IS NOT NULL, '$rel_1080p', rel_1080p), rel_dvdr = VALUES(rel_dvdr), swesub = VALUES(swesub)";
 $result = mysql_query($insert_movie, $connection) or die (mysql_error());
 mysql_close($connection);

pictures of whats happening: first entry: http://puu.sh/9iN0p/91f0839b8f.png second entry: http://puu.sh/9iN2Q/8cafdb5422.png

look at the last row in there. from the first picture to the second it should be 1,1,1,1 and not 0,1,1,0.

saknar namn
  • 139
  • 1
  • 1
  • 10
  • Why do you use `insert` when you want to `update`? – juergen d Jun 07 '14 at 12:01
  • because if it doesnt exist it needs to be created. im not sure if it exists yet. if it can only update then its quite useless. – saknar namn Jun 07 '14 at 12:03
  • I think the query is a bit confused on the difference between 0 and NULL. You may want to compare to 0 instead of IS NULL, they're not the same thing. `'$rel_720p'` could be `'0'`, but definitely never `NULL`. – Joachim Isaksson Jun 07 '14 at 12:09
  • @GotchaRob "also the code is written in mysql because im more familiar in it and will be ported to pdo when its done. please dont tell me "dont use mysql" and dont give me an answer. already gotten those often enough" – saknar namn Jun 07 '14 at 12:14
  • A side note, you may want to be a bit more careful with your sample database contents when posting here, had the contents been in english, I'm pretty sure the question would have been flagged. – Joachim Isaksson Jun 07 '14 at 12:16
  • @JoachimIsaksson and how would i do that? and yes i know that it would've been. but sometimes i need to express my anger somehow. and that way is better than to rage at someone – saknar namn Jun 07 '14 at 12:16
  • I don't like questions that lecture me on what I can and cannot say, but this kind of problem is often symptomatic of poor design. – Strawberry Jun 07 '14 at 12:53

1 Answers1

0

probably not the best solution, but i managed to do it anyhow:

ON DUPLICATE KEY UPDATE
rel_720p = IF('$rel_720p'=1, 1, rel_720p), rel_1080p = IF('$rel_1080p'=1, 1, rel_1080p), rel_dvdr = IF('$rel_dvdr'=1, 1, rel_dvdr), swesub = IF('$swesub'=1, 1, swesub)";
saknar namn
  • 139
  • 1
  • 1
  • 10