1

Before I begin, I realize that I can create the random number function with php but for my own reasons it has to be javascript.

I have a function that generates a random number and it gets saved in the database. In order to do so I have so submit it through a form but when it gets submitted, a different integer is saved in the database.

A random number such as:

6108784240204841

Produces this in my database:

-223602981

HTML

<form action="submit.php" method="post">
        <input type="text" name="number" id="number">
        <button type="submit"></button>
</form>

JS

 $randomNumber = Math.floor(Math.random()*1E16);
 $('#number').val($randomNumber);

PHP

<?php
    if ($_SERVER['REQUEST_METHOD'] === 'POST') {
        if (!empty($_POST['id'])) {
            $inputNumber = filter_var($_POST['number'], FILTER_VALIDATE_INT);
        };
    };

try {
    $db = new PDO('mysql:host=localhost;dbname=myDb;charset=utf8', 'myDb_admin', 'myDb_password');
    $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $db->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);

    $stmt = $db->prepare('INSERT INTO user (number) VALUES (:number)');
    $stmt->bindParam(':number', $number);

    $number = $inputNumber;

    $stmt->execute();

} catch(PDOException $e) {
    return $e->getMessage();
};

Database Structure

number    
int(100)    
Primary
5
  • Number is too large to save... Commented May 22, 2015 at 15:28
  • There is no int size that i can set in the database to save it? Or even a different way of saving it? Commented May 22, 2015 at 15:28
  • INT is always 32 bits irrespective of that (100) - use BIGINT if appropriate, or of course varchar Commented May 22, 2015 at 15:29
  • 2
    Use BIGINT, they're 64 bits. See dev.mysql.com/doc/refman/5.0/en/integer-types.html Commented May 22, 2015 at 15:29
  • possible duplicate of MySQL: bigint Vs int Commented May 22, 2015 at 15:32

1 Answer 1

2

Your trying to save a number that is too large use BIGINT .

number
BIGINT(20)
Primary

Integer Types

Sign up to request clarification or add additional context in comments.

3 Comments

It's just BIGINT, not INT(BIGINT)
Sorry was racing to type it up. Sorry. Actually getting ready to vote to close.
@Nix I made the changes you suggested and the number 7132156763691455 Gets changed to 783165534

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.