Register

If this is your first visit, please click the Sign Up now button to begin the process of creating your account so you can begin posting on our forums! The Sign Up process will only take up about a minute of two of your time.

Results 1 to 3 of 3
  1. #1
    Junior Member
    Join Date
    Jun 2005
    Posts
    3
    Member #
    10229
    The relevant code can be found at:
    http://headbanger.no-ip.org/code.txt

    What I'm trying to do is take a textarea, and for each seperate line add the data into the database individually. My problem is preventing them from entering the same data twice. I would load it all into an array, but I'm not sure how to do that with this setup.

    Any help would be awesome. Thanks for your time.

  2.  

  3. #2
    Senior Member rosland's Avatar
    Join Date
    Jul 2003
    Location
    Norway
    Posts
    1,944
    Member #
    2096
    Your present code already checks for duplicate entries in the while loop (though it's not very resource efficient).
    It will not add duplicate name entries to your table, but will add an extra vote to an already existing name.
    PHP Code:
    while ($counter $numLines) {
            
    $line rtrim($lines[$counter]);
            
    $check mysql_fetch_row(mysql_query("SELECT COUNT(*) FROM nominations WHERE username='$line'"));
            if (
    $check[0] == 0) {
                
    mysql_query("INSERT INTO nominations VALUES ('$line', '1')");
            } else {
                
    mysql_query("UPDATE nominations SET num=num+1 WHERE username='$line'");
            }
            
    $counter++;
        } 
    However, it does not prevent the user from entering the same name multiple times in the same textfield and hence giving multiple 'votes' to the same candidate. (I assume names are to be entered).

    If the user is only allowed to submit the form once, you can load all the textfield entries into an array and check for unique values before submitting it to the while loop.
    This minimal alteration to your existing code will do that:
    PHP Code:
    while ($counter $numLines//This while loop loads all textfield entries into the array $names
    {
        
    $names[] = rtrim($lines[$counter]);
    }
    $lines array_unique($names); //this checks the array for duplicate entries

    $numLines count($lines); //This counts the number of values in the refined array

    //The remaining is your original code with the rtim() removed as it's already done
    $counter 0;
    while (
    $counter $numLines) {
            
    $line $lines[$counter];
            
    $check mysql_fetch_row(mysql_query("SELECT COUNT(*) FROM nominations WHERE username='$line'"));
            if (
    $check[0] == 0) {
                
    mysql_query("INSERT INTO nominations VALUES ('$line', '1')");
            } else {
                
    mysql_query("UPDATE nominations SET num=num+1 WHERE username='$line'");
            }
            
    $counter++;
        } 
    S. Rosland

  4. #3
    Junior Member
    Join Date
    Jun 2005
    Posts
    3
    Member #
    10229
    I'd actually already changed the entire script to a different system, so I don't really need this, but this is something I'll need to know in the future, so it's still useful.

    Thank you, it's very much appreciated.


Remove Ads

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
All times are GMT -6. The time now is 11:44 PM.
Powered by vBulletin® Version 4.2.3
Copyright © 2019 vBulletin Solutions, Inc. All rights reserved.
vBulletin Skin By: PurevB.com