How to get a random row, and not change.

Discussion in 'PHP' started by killaklown, Oct 24, 2006.

  1. #1
    How can i get a random row in my database and not have it change?

    Im trying to add a simple anti-spam feature, and i have 25 number combinations in the mysql table, but as soon as I enter the same combo and click send on the form, it changes that random combo so it says that the anti-spam combo was not the same...

    I have:

        $asql  = ("SELECT * FROM antispam ORDER BY rand() LIMIT 1");
        $ars = mysql_query($asql);
        $acount = mysql_num_rows($ars);
    	while ($combo = mysql_fetch_array($ars))
    	{
    	$ncombo = $combo[Code];
    PHP:
    and the code to check if the code entered by the user matches the random combination:

    else if ($antispam != $ncombo) {
    echo''.$ncombo.''; // Was just testing to see if it actually keeps the same number, and it doesnt
    }
    PHP:
    and then in the form, i have this:

    	 <? echo'&nbsp;&nbsp;&nbsp;&nbsp;'.$ncombo.''; ?><br>
    	 &nbsp;&nbsp;<input name='antispam' size='5' maxlength='5'><br><br>
    PHP:

     
    killaklown, Oct 24, 2006 IP
  2. php-lover

    php-lover Active Member

    Messages:
    261
    Likes Received:
    21
    Best Answers:
    0
    Trophy Points:
    58
    #2
    Check this line..

    $ncombo = $combo
    ;[/COLOR]
    
    I think it's should be 
    
    [COLOR="Sienna"]$ncombo = $combo['code']; [/COLOR]
    Code (markup):
     
    php-lover, Oct 24, 2006 IP
  3. killaklown

    killaklown Well-Known Member

    Messages:
    2,666
    Likes Received:
    87
    Best Answers:
    0
    Trophy Points:
    165
    #3
     
    killaklown, Oct 24, 2006 IP
  4. php-lover

    php-lover Active Member

    Messages:
    261
    Likes Received:
    21
    Best Answers:
    0
    Trophy Points:
    58
    #4
    Im trying to add a simple anti-spam feature, and i have 25 number combinations in the mysql table, but as soon as I enter the same combo and click send on the form, it changes that random combo so it says that the anti-spam combo was not the same...

    I think you enter a combo is in the table but remember the query will pick random the code from the table....maybe that why this message appear "anti-spam combo was not the same"
     
    php-lover, Oct 24, 2006 IP
  5. RRWH

    RRWH Active Member

    Messages:
    821
    Likes Received:
    49
    Best Answers:
    0
    Trophy Points:
    70
    #5
    This is not so much a programming issue as a logic issue.

    Yes, use the rand() function to make a selection to generate your form etc, but pass along another parameter - such as the id of the randomly selected object.

    When you submit the form the ID is passed, along with the entered data, and you look up the entered data using the ID to find the correct response in your database.
     
    RRWH, Oct 25, 2006 IP
    streety likes this.
  6. killaklown

    killaklown Well-Known Member

    Messages:
    2,666
    Likes Received:
    87
    Best Answers:
    0
    Trophy Points:
    165
    #6
    heh, i was thinking about trying to do something with the id :)
     
    killaklown, Oct 25, 2006 IP