Hello!

There is a MySQL table in which the names of image files for a single user are stored:

+----+------------+ | id | photo_name | +----+------------+ | 1 | 1.jpg | | 2 | 2.jpg | | 3 | 3.jpg | | 4 | 4.jpg | | 5 | 5.jpg | +----+------------+ 

If the user adds two more images 6.jpg and 7.jpg, then from the client’s side we will get an array of already 7 images (5 were + 2 added), which looks like this (using Jquery UI Sortable):

  $ar_img = array('1.jpg','2.jpg','3.jpg','4.jpg','5.jpg','6.jpg','7.jpg'); 

How can I add new images to the table based on a comparison of existing ones?

Those. I need to take an array of $ ar_img, compare the composition of the names of the pictures in the array with the names of the pictures that are in the table. If there are changes, then add the missing to the existing table!

How can this be done using MySQL and PHP?

Thank you in advance!

    1 answer 1

    Get an array of images from the database, find the difference between the arrays, and add those that do not exist:

     //массив из бд $old_imgs = ['1.jpg','2.jpg','3.jpg','4.jpg','5.jpg']; //массив с новыми картинками $ar_img = ['1.jpg','2.jpg','3.jpg','4.jpg','5.jpg', '6.jpg','7.jpg']; //разница, в массиве diff будут ['6.jpg','7.jpg'] $diff = array_diff($ar_img, $old_imgs);