Based on your sample code and the comments, I'm going to suggest a more normalized approach. It appears as though your Pictures table looks something like:
Pictures {
ID Int,
P1 varchar(??),
P2 varchar(??),
...
}
... Where, I assume, P1..Pn is n columns that will contain paths to some image files. As previous comments said, this is not normalized and is a poor design. It might get the job done, but it's not scalable or efficient. Look at this article about normalization as a starter. If you change to something like:
Pictures {
UserID Int,
PictureID Int,
PicturePath varchar(??)
}
...then you would have one row per image per user. (The combination of UserID and PictureID will be unique. So each user can have PictureID of 1, 2, etc.) Then you don't have to worry about trying to find the "next null column". You just add a new row for each new picture.
Next point... Assuming that you were going to keep the current table structure for some reason. (not recommended.) Your logic shows that you are making (potentially) a lot of unnecessary calls to the database. Each query that you run is a round-trip to the database. Each round-trip takes up resources and time. Whenever possible, you should attempt to reduce the number of round-trips that your application needs to make. You will get better performance and better scalability.
Last point... One of the comments mentioned SQL Injection. The code that you provided is vulnerable to SQL Injection attacks. It's a fairly simple matter to fix that. But you do need to look it up and understand what it is and why your code is vulnerable.
Good luck!