-2

I have the following set of if/elseif conditions that update columns titled Col & Col2 when a form is submitted:

if($data3["Col"] < 2 ){

      $db->query("UPDATE answers SET Col = Col+1");

}elseif( $data3["Col"]= 2){
      $db->query("UPDATE answers SET Col = Col+0");
  }

if($data3["Col2"] < 10){

 $db->query("UPDATE answers SET Col2 = Col2+1");

}elseif( $data3["Col2"] >= 10){

      $db->query("UPDATE answers SET Col2 = Col2+0");
 }

When the code runs, it ignores the if/elseif conditions and does every update. For example, let's say upon form submit, Col = 2 & Col2 = 10. Col and Col2 should both stay stay the same. However, the code updates both by 1. I've tried every iteration that I can think of using continue, break, and return but nothing seems to work.

Any help would be much appreciated.

Thanks!

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055

4 Answers4

1

You missed an equal to here

elseif($data3["Col"]==2)
Joey Pinto
  • 1,735
  • 1
  • 18
  • 34
1

Equal sign (=)is for assignment not for testing equality(==), so your elseif clause should be
$data3["Col"] == 2 instead of $data3["Col"]= 2;

if($data3["Col"] < 2 ){

      $db->query("UPDATE answers SET Col = Col+1");

}elseif( $data3["Col"]= 2){// not $data3["Col"]= 2 but this $data3["Col"] == 2
      $db->query("UPDATE answers SET Col = Col+0");
  }

Good luck

kourouma_coder
  • 1,078
  • 2
  • 13
  • 24
1

Instead of

elseif($data3["Col"]=2){ $db->query("UPDATE answers SET Col = Col+0");}

use this:

elseif($data3["Col"]==2){ $db->query("UPDATE answers SET Col = Col+0");}
Nitish Kumar Diwakar
  • 663
  • 4
  • 14
  • 25
-1

you don't need to used elseif for no action .. just try if for any action...

check $data3["Col2"] value. I think your problem is here..