How do you do multiple SQL Update queries at once?
-
So lets say I have a table called bird, and a table called guitar(please ignore messy syntax). And I wanted to run multiple(hundreds...) SQL update queries on each, all at once using a $_POST php form button. How would I set that up in terms of the php for the multiple SQL updates(I can set up the form button easily)? I've tried doing something like below, but it is really not working well at all. I'm thinking I need to use foreach somehow...maybe? $value= array(); $value[] = array( 'A' => $row['whatever'], 'B' => 'another value ', //and so forth ), array ( 'A' => '1', 'B' => '2', //and so forth... ) $value2= array(); $value2[] = array( 'guitar' => 'old', 'music' => 'new', //and so forth ) $sql = 'UPDATE ' . BIRD_TABLE . ' SET ' . $db->sql_build_array('UPDATE', $sql_ary); $db->sql_query($sql);
-
Answer:
UPDATE can only update one sets of fields at a time. For example: UPDATE bird SET name='aaa', color='red' WHERE <expression> You can not update multiple sets of fields with one UPDATE statement: ***wrong*** UPDATE bird SET name='aaa', name='bbb', name='ccc' ***wrong*** So what you are trying to do is not possible. Why do you insist on using only one UPDATE? You should use a loop to go through each record and update them one at a time. And yes this can be all in one POST no matter how many records you need to update. One way to do this is to make their primary key (such as ID) part of the parameter name.
Vipaka at Yahoo! Answers Visit the source
Related Q & A:
- How to Compare Rows in SQL?Best solution by Stack Overflow
- How to do GROUP_CONCAT in SQL Server?Best solution by Stack Overflow
- How to make only ONE Sql query?Best solution by Stack Overflow
- How to import XML into SQL Server database?Best solution by Stack Overflow
- How do I connect to SQL Server using C#?Best solution by Stack Overflow
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.