insert and update at same time and add ++

Discussion in 'PHP' started by macaela, Sep 15, 2010.

  1. #1
    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???/
     
    macaela, Sep 15, 2010 IP
  2. macaela

    macaela Active Member

    Messages:
    181
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    51
    #2
    i tried like this but did not work any sugestions are welcome??

        $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 $count=count"; // total will be updated to total+number of new puppies added.
    
    mysql_query($update_query);
    PHP:
     
    macaela, Sep 15, 2010 IP
  3. bartolay13

    bartolay13 Active Member

    Messages:
    735
    Likes Received:
    14
    Best Answers:
    1
    Trophy Points:
    98
    #3
    what your thinking might work but not as accurately when your database grows... this album is like a directory right??
    why not just count how many items are inside that directory..

    search more on fopen() directories function in php.
     
    bartolay13, Sep 16, 2010 IP
  4. imperialDirectory

    imperialDirectory Peon

    Messages:
    395
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    0
    #4
    try something like
    
    $update_query = 'UPDATE `albums` SET `count` = `count` + 1 WHERE `album_id` = ' . $album . ' LIMIT 1';
    
    Code (markup):
     
    imperialDirectory, Sep 17, 2010 IP
  5. seoforall

    seoforall Peon

    Messages:
    40
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #5
    true that

    however, know that your albums table is redundant if it's only purpose is to keep the count. Even if it has any other purpose, i would suggest not to keep the count in there .. it may become a source of errors in future.

    instead, to show the count, i would suggest to simply use the following in your script (the one that should show the count):
    $count = mysql_result(mysql_query(SELECT COUNT(*) FROM `PHOTOS_TABLE` WHERE `album_ID` = '$someID' ), 0);

    the count queries are usually very light on MySQL so it should execute very fast (just make sure your PHOTOS_TABLE has a primary key)

    good luck
     
    seoforall, Sep 18, 2010 IP
  6. macaela

    macaela Active Member

    Messages:
    181
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    51
    #6
    Hi thanks i manage to do by using the count functions THANKS EVERYONE
     
    macaela, Sep 18, 2010 IP