Stack Overflow is a community of 4.7 million programmers, just like you, helping each other.

Join them; it only takes a minute:

Sign up
Join the Stack Overflow community to:
  1. Ask programming questions
  2. Answer and help your peers
  3. Get recognized for your expertise

I have this code and couldn't get it to work:

$character_set_array = array();

    $character_set_array[] = array('count' => 8, 'characters' => '0123456789');
    $temp_array = array();
    foreach ($character_set_array as $character_set) {
        for ($i = 0; $i < $character_set['count']; $i++) {
            $temp_array[] = $character_set['characters'][rand(0, strlen($character_set['characters']) - 1)];
        }
    }
    shuffle($temp_array);
    $pinstart = 'AA';
    $pinend = implode('', $temp_array);
    $newpin = $pinstart.$pinend;




function regenerate_pin($pin)
{   
    if ($PIN == 'PIN') { return ''; } else {
    $pin = mysql_real_escape_string($pin);  // SECURITY!
    $result = mysql_query("SELECT pin FROM pins WHERE pin='$pin' LIMIT 1");



    if(mysql_num_rows($result) == 0) {
        return 'This pin has already been used';
            } else {
        $sql = mysql_query("UPDATE pins SET pins='$newpin'"); 
        return "The pin has been regened, the new pin is '.$newpin.'";
             } }
}

Basically what I'm trying to get it to do is this: - Get the pin from a HTML input box, - Check if the pin exists in the database (works) - If it exists, replace $pin with $newpin and print out the $newpin, this is how my table looks like

enter image description here

  • Some info about my table:
  • Table name = pins Column where pin is stored is named Pin

Thank you for reading and I hope I find a quick solution.

share|improve this question
    
This will fail with PINs that start with "0". – Ignacio Vazquez-Abrams Aug 9 '13 at 7:15
    
Shouldn't it be if(mysql_num_rows($result) != 0) { because if there is a record there then it is in use otherwise it is a new pin. – jeff Aug 9 '13 at 7:16
    
I already have a script that checks if the string exists everything is working fine @jeff so I don't think its a problem – Swaly Aug 9 '13 at 7:25
    
@ignacio vazquez-abrams I make it start with AA – Swaly Aug 9 '13 at 7:25
    
Then I'm very not surprised that your queries don't work. Have you tried printing them out? – Ignacio Vazquez-Abrams Aug 9 '13 at 7:26
up vote 1 down vote accepted

Your regenerate_pin() function doesn't see the $newpin variable and your UPDATE SQL might be wrong:

Change "UPDATE pins SET pins='$newpin'" to "UPDATE pins SET pin='$newpin'"

For the function you have two options:

Add global $newpin; line inside the function regenerate_pin($pin) -code

function regenerate_pin($pin)
{  
 global $newpin;
...
}

or send the $newpin variable as an arbument to regenerate_pin-function.'

function regenerate_pin($pin, $newpin)
{  
...
}

and you have to call your regenerate_pin function like this

regenerate_pin('oldpin', 'newpin');
share|improve this answer
    
Thank you this worked, also if its ok I had to do this for it to work regenerate_pin($pin, $newpin); instead of regenerate_pin('oldpin', 'newpin'); but yeah should be working now, do I need to check for duplicates or is this highly unlikely? if so what do I need to do? – Swaly Aug 9 '13 at 7:28
    
You should check for duplicates. I'd put an unique key to the pin fields 'alter table pins add unique key pin' so you will never have duplicate pins. And you should change "SELECT pin FROM pins WHERE pin='$pin' LIMIT 1" to "SELECT pin FROM pins WHERE pin='$newpin' LIMIT 1" since you are checking does $newpin exsist. – iiro Aug 9 '13 at 7:32
    
It's currently working how I want it to @irro , except the fact that their could be duplicates, how would I add a unique key to the pin field? and its working with out your fix, I'm checking if $pin exists, if it does replace it with the a new pin. – Swaly Aug 9 '13 at 7:35

It seems i misunderstood.
But had better change:

$result = mysql_query("SELECT pin FROM pins WHERE pin='$pin' LIMIT 1");

if(mysql_num_rows($result) == 0) {

On

$result = mysql_fetch_row(mysql_query("SELECT COUNT(pin) FROM pins WHERE pin='$pin' LIMIT 1"));

if($result[0] == 0) {
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.