Insert and update at same time and add ++

Hi i need to count the number items in an album now on the album i have create a field called count which counts the number items in that album

but now i dnt knw how to update those as soon insert a item in the table item

so i thought woul be possible to create a script that when insert data into item tables also update album table row count by adding one like update count with A now A equal row count ++1 minus if delete any help on start of it if possible???/

If you must use a trigger.

1 is an integer not a string.

i tried sunibfin like this

$sql = "INSERT INTO " .PHOTOS_TABLE. "
        (
            photo_name,
            photo_date,
            photo_proper,
            photo_size,
            album_id
        )
        VALUES
        (
            '" .addslashes($photo_name). "',
            " .time(). ",
            '" .addslashes($key_name). "',
            " .intval($size). ",
            " .$album. "
        )";
        
    
$update_query="UPDATE albums set count=count+'1' where condition"; // total will be updated to total+number of new puppies added.

mysql_query($update_query);

The count field in the albums table you’re describe is what’s known as redundant data – data that is saved in the database but could achieved by some queries on the rest of the database (in this case by using COUNT())

Now, for really heavy applications with thousands of albums each containing hundreds of images such a column can really speed things up, but if your application won’t be so heavy I advise to stay clear of columns like this, as they only tend to come with problems such as the one you’re having now (among others …)

When you need to know how many images are in an album, just join the album table with the images table and use the count() function

:slight_smile: