I am trying to set a default image for my image upload form. In the database I have the column “deafult_image” and it will be set to 1 for default and set to 0 if the image is not default.
My problem right now is I can update a row to 1 to make an image the default but if another image exists and is default “1” then I will have 2 default images. How can i update my default image but also change the current default image to 0 at the same time to make it not default anymore.
The media_id column is unique.
link_id is not and could have multiple images for any given link_id.
Here is what I came up with for updating the row to make the image default.
$media = $_GET['media_id'];
$media_id = '1';
$sql = "select * FROM images where media_id = '$media'";
$result = mysql_query($sql) or die(mysql_error());
while($row = mysql_fetch_array($result)){
mysql_query("UPDATE images SET default_image='$media_id' where media_id = '$media'") ;
}
What should I do to find if another image with the same link_id exists and change it from 1 to 0 if it is default. There is always a default image for every link_id so I need to update to the current default image.
Also I know I need to validate, this is just for testing.
This will reset every image to 0
This will set your relevant image as the default