Hello!

There is an array of pictures

$usr_imgs = array('1.jpg','3.jpg','8.jpg','9.jpg','11.jpg'); 

The array is added to the table whose keys are the ord field, and the values ​​are photo_name:

 +----+------------+-----+ | id | photo_name | ord | +----+------------+-----+ | 13 | 1.jpg | 0 | | 14 | 3.jpg | 1 | | 15 | 8.jpg | 2 | | 16 | 9.jpg | 3 | | 17 | 11.jpg | 4 | +----+------------+-----+ 

In the $ usr_imgs array, the user added another image, for example, '5.jpg'. The array now has the following form:

 $usr_imgs = array('1.jpg','3.jpg','5.jpg' ,'8.jpg','9.jpg','11.jpg'); 

The question is, how can a single MySQL query overwrite keys for values ​​that already exist in the table? (or one will fail?)

A new picture 5.jpg add as a new entry with the appropriate key. Those. to get this table:

 +----+------------+-----+ | id | photo_name | ord | +----+------------+-----+ | 13 | 1.jpg | 0 | | 14 | 3.jpg | 1 | | 15 | 8.jpg | 3 | | 16 | 9.jpg | 4 | | 17 | 11.jpg | 5 | | 18 | 5.jpg | 2 | +----+------------+-----+ 
  • This is inconvenient, each time to rewrite all the records ... en.stackoverflow.com/questions/545763/… - Mike
  • And in your case, a simple update ord=ord+1 where ord>=2 helps - Mike
  • @Mike I read about on duplicate key update, but didn’t quite understand how to use it - Pavel
  • And where does on duplicate. he will not help. One request to insert one record and update others in MySQL is absolutely impossible - Mike
  • Or do you want to insert all the records from the array at once with a single request and at the same time update the existing keys ... but if there are no pictures in the array at all (it was deleted), you will need to delete them with a separate request - Mike

0