Multiple images are saved in a directory. They are all displayed on a page with:

PHP Code:
$dirname "users/" $id_use "/images/";
$images glob("{$dirname}*");
foreach(
$images as $image) {
    echo 
'<img src="'.$image.'" width="300px" /><br />';
    
$img_name end(explode("users/" $id_use "/images/"$image));
    include 
'profile_photo_select.php';

profile_photo_select.php is:

Code:
<form action="?profile" method="post">
	<p><input type="submit" name="submit" value="Use as profile photo"></p>
</form>
?profile leads to:

PHP Code:
<?php
if (isset($_GET['profile'])) {
    
//Get image name from directory
    
$sql = ("SELECT * FROM img_dp WHERE id = '".$id_use."'");
    
$result mysql_query($sql);
    
//Check if id already exists in img_dp
    
if(mysql_num_rows($result)){
        
mysql_query("UPDATE img_dp SET profile_img='$img_name'  WHERE id='$id_use'");
    } else {
        
mysql_query("INSERT INTO img_dp (id, profile_img) VALUES ('$id_use', '$img_name')");
    }
}
?>
Currently, it will set id to $id_use but profile_img will be blank. I think the issue is that the profile_photo_select.php is included every iteration of the loop and so it's putting the buttons in place but the $img_name variable does not match the current image value.

I'm getting out of my depth here. I'm sure there must be a better way to do this?

Cheers
Sam