Active10 months ago
Update Multiple Fields Php Mysql

I have a php form into which I need to populate 5 fields from one MySQL table after choosing an item from a dynamic drop down select list (from MySQL database). Updating multiple columns in single MySQL table. I am making a program using PHP/MySQL. I need to update 3 columns in a table. When i use a single UPDATE-SET. MySQL UPDATE multiple columns MySQL UPDATE command can be used to update multiple columns by specifying a comma separated list of column_name = new_value. Where column_name is the name of the column to be updated and new_value is the new value with which the column will be updated.

I am trying to understand how to UPDATE multiple rows with different values and I just don't get it. The solution is everywhere but to me it looks difficult to understand.

For instance, three updates into 1 query:

I read an example, but I really don't understand how to make the query. i.e:

Update Multiple Fields Mysql Php

I'm not entirely clear how to do the query if there are multiple condition in the WHERE and in the IF condition..any ideas?

Adriaan Koster
13.2k3 gold badges34 silver badges50 bronze badges
franvergara66franvergara66
4,33915 gold badges48 silver badges87 bronze badges

4 Answers

You can do it this way:

I don't understand your date format. Dates should be stored in the database using native date and time types.

Gordon LinoffGordon Linoff
854k38 gold badges356 silver badges454 bronze badges

MySQL allows a more readable way to combine multiple updates into a single query. This seems to better fit the scenario you describe, is much easier to read, and avoids those difficult-to-untangle multiple conditions.

This assumes that the user_rol, cod_office combination is a primary key. If only one of these is the PK, then add the other field to the UPDATE list.If neither of them is a primary key (that seems unlikely) then this approach will always create new records - probably not what is wanted.

However, this approach makes prepared statements easier to build and more concise.

Adriaan Koster

Php Mysql Update Multiple Records

13.2k3 gold badges34 silver badges50 bronze badges
TrevedhekTrevedhek
2,6311 gold badge13 silver badges13 bronze badges

You can use a CASE statement to handle multiple if/then scenarios:

Hart COHart CO
29.5k5 gold badges35 silver badges49 bronze badges
Matteo Baldi

Mysql Multiple Update

3,4589 gold badges24 silver badges41 bronze badges
Akshay BhanAkshay Bhan
Mysql update multiple values

protected by Nilesh RathodApr 19 '18 at 11:51

Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).
Would you like to answer one of these unanswered questions instead?

Not the answer you're looking for? Browse other questions tagged mysqlsqlsql-update or ask your own question.

Active1 year, 6 months ago

How to set multiple columns of a table using update query in mysql?

David
155k29 gold badges155 silver badges222 bronze badges
AmoghAmogh

2 Answers

Just add parameters, split by comma:

See also: mySQL manual on UPDATE

Pekka 웃Pekka 웃
368k120 gold badges866 silver badges1026 bronze badges
Derlin
6,9152 gold badges19 silver badges34 bronze badges
Hendra JayaHendra Jaya

Not the answer you're looking for? Browse other questions tagged mysql or ask your own question.