1

I have this query that works for updating the value from one column with the value that the user enters with php...How can i rewrite it to update multiple values ??

UPDATE  `lahiguera_hor`.`cupos` 
SET  `cuposLun8`='$_POST[cupolun8]'   
WHERE  `cupos`.`cuposLun8` =  '$cupolun8';

thanks in advance!

rs.
  • 26,707
  • 12
  • 68
  • 90
  • See docs first [**UPDATE Syntax**](http://dev.mysql.com/doc/refman/5.0/en/update.html) – M Khalid Junaid Jun 02 '14 at 16:03
  • You appear wide open to [SQL Injection](http://security.stackexchange.com/a/25710/3396), beyond your current problem. Use [parameterized queries](http://stackoverflow.com/a/60496/812837), or suffer the consequences. Note that generally columns with numbers in them indicate potential design problems. – Clockwork-Muse Jun 03 '14 at 10:45

1 Answers1

3

Try

 UPDATE  `lahiguera_hor`.`cupos` SET  
`cuposLun8`='$_POST[cupolun8]',
 `AdditonalCol1`='$_POST[AdditonalCol1]',
 `AdditonalCol2`='$_POST[AdditonalCol2]'
 WHERE  `cupos`.`cuposLun8` =  '$cupolun8';

Set columns should be separated with a comma.

user1628449
  • 375
  • 1
  • 3
  • 16
  • I tried this : UPDATE `lahiguera_hor`.`cupos` SET `cuposLun8`='$_POST[cupolun8]',`cuposLun10`='$_POST[cupolun10]' WHERE `cupos`.`cuposLun8` = '$cupolun8', `cuposLun10` = '$cupolun10'; But didnt work! – Mariano Urbonas Jun 02 '14 at 16:09
  • the error resides in your where clause. You would need AND as you're providing two qualifiers: WHERE cupos.cuposLun8 = '$cupolun8' AND cuposLun10 = '$cupolun10'; – user1628449 Jun 02 '14 at 16:11
  • It works fine!! Should it work also for more than 2? if i add more using AND? – Mariano Urbonas Jun 02 '14 at 17:01
  • It will work for as many columns you would like. You can also load up on the and statements as well. if this was the proper solution, can you please mark it as such? – user1628449 Jun 02 '14 at 18:36