update problem

Discussion in 'PHP' started by cris02, Oct 29, 2009.

  1. #1
    Hi there,

    I can't imagine why the code below still adding 2 instead of 1. Anyone can help? thanks
    
    $num_view = "SELECT * FROM forum_thread where tid = '".$_GET['tid']."' LIMIT 1";
    $result = mysql_query($num_view);
    while($view = mysql_fetch_array($result)) {
    $no_view = $view['views'] + 1;
    }
    
    $views_counter = "UPDATE forum_thread SET views = '$no_view' WHERE tid = '".$_GET['tid']."'";
    mysql_query($views_counter);
    mysql_close();
    PHP:
     
    cris02, Oct 29, 2009 IP
  2. mastermunj

    mastermunj Well-Known Member

    Messages:
    687
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    110
    #2
    the purpose of this code is to increment thread view count by 1? if so, then just single update query will do the trick as follows.

    
    $update_counter = "UPDATE forum_thread SET views = views + 1 WHERE tid = {$_GET['tid']}";
    mysql_query($update_counter);
    
    PHP:
     
    mastermunj, Oct 29, 2009 IP