-1

My task is to calculate the number of time each word is dragged into the wrong box and then store it into mysql database ,i used jquery drag and drop ,each time word is dragged into the box the jquery ftn sends the word id,and category to the next page by using ajax post,if the word id and user anme exsit it updates the word dragged else it insert new record.my this code works fine for jst single new insertion and then uodates it too bt it doesnt work for new insertion kindly chk whats wrong with it :

    <?php     
    session_start();   
    error_reporting(7);   
    require("db.php");    

    $Cat=$_POST['Cat'];    
    $WID=$_POST['Wid'];    

    $S_qry="Select Wrong_Drag from user_count where W_ID='".$WID."' and   User_ID='".$_SESSION['UID']."'";    
    $R_qry=mysql_query($S_qry);

    $num=mysql_num_rows($R_qry);

    if ($num == 0)
    {
        $qry="Insert into user_count (User_ID,W_ID,Wrong_Drag)     Values('".$_SESSION['UID']."','".$WID."','1')";
        $Result=mysql_query($qry);  
    }   
    else
    {
        while(mysql_fetch_assoc($R_qry))
        {
        $sql   = "Update user_count  set Wrong_Drag=Wrong_Drag+1 Where W_ID='".$WID."' and User_ID ='".$_SESSION['UID']."' ";
        $query = mysql_query($sql);
        }
    }

?>`

  • **Danger**: You are using [an **obsolete** database API](http://stackoverflow.com/q/12859942/19068) and should use a [modern replacement](http://php.net/manual/en/mysqlinfo.api.choosing.php). You are also **vulnerable to [SQL injection attacks](http://bobby-tables.com/)** that a modern API would make it easier to [defend](http://stackoverflow.com/questions/60174/best-way-to-prevent-sql-injection-in-php) yourself from. – Quentin Dec 14 '13 at 17:31

1 Answers1

0

try by updating your else part --

else
{

    $sql   = "Update user_count  set Wrong_Drag=Wrong_Drag+1 Where W_ID='".$WID."' and User_ID ='".$_SESSION['UID']."' ";
    $query = mysql_query($sql);
}

As there is no need of while loop for that clause. just due to it is coming from that check number of rows is not zero.

Abhishek
  • 1,543
  • 3
  • 13
  • 29