-2

I'm having problem with my query, it inserts the same the value again and again even though the value of the variable is different.

Here is the first page. it contains a randomizer for the creation of accounts.

 function getRandomString($length = 10) {
        $validCharacters = "1234567890";
        $validCharNumber = strlen($validCharacters);

        $result = "";

        for ($i = 0; $i < $length; $i++) {
            $index = mt_rand(0, $validCharNumber - 1);
            $result .= $validCharacters[$index];
        }

        return $result;
    }

    $idNo=getRandomString();


    function getRandomPassword($length = 6) {
        $validCharacters = "qwertyuiopasdfghjklzxcvbnm1234567890";
        $validCharNumber = strlen($validCharacters);

        $result = "";

        for ($i = 0; $i < $length; $i++) {
            $index = mt_rand(0, $validCharNumber - 1);
            $result .= $validCharacters[$index];
        }

        return $result;
    }

    $password=getRandomPassword();

The values will then be submitted to the next page which is below. The submitted values are correct because i print them out first to check. It always inserts the value "2147483647" for the ID_No even though this is not the randomize value.

   $insert = "INSERT INTO person (ID_No, P_Word, E_Status)
        VALUES('$userName', '$userPass', 'Applicant')";

  $result = mysql_query($insert);

How do you fix this? Is it a bug?

user3797088
  • 563
  • 3
  • 7
  • 16

3 Answers3

0

We can only guess given the code posted.

I don't see where $index ends up being $userName.
The sql posted shows userName being inserted as a character string, quotes around it.

ID_No is likely an integer type and thus the character string fills it up.

This should just blow up on insert so you know that this is wrong. Best practice is to use sql_mode = 'STRICT_ALL_TABLES' in the MySQL configuration file.

Reenactor Rob
  • 1,508
  • 1
  • 11
  • 20
0

I think you should check data type for id_no column..You must give double data type to that column

Kedar B
  • 774
  • 5
  • 18
  • 47
-2

I think what you have in mind is this : You must use the string concatenation operator (here, in PHP, it is the DOT) if you are thinking of $ symbols as variable names.

$insert = "INSERT INTO person (ID_No, P_Word, E_Status) " . 
        " VALUES( " . 
         "'" . $userName . "'" . " , " . 
         "'" . $userPass. "'" . " , " . 
         "'" . "Applicant. "'" . " ) " ; 

$result = mysql_query($insert);

Whirl Mind
  • 884
  • 1
  • 9
  • 18