I looked for my problem in the archive but the solutions reported were not good for my case. I can not understand the right syntax to insert a php variable instead of the table name:
$insert_sql = "INSERT INTO galleria1(contatore, immagine) VALUES ('', '".$filename."')";
where instead of galleria1 I have to enter the $gallery php variable. Thanks to those who can help me.