Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

PHP

INSERT duplicates mobile number

Hey

I have this problem were on submitting form data, the INSERT for a mobile number column seems to enter a same number across different entries/databases. The number is always 2147483647 even after entering a different number. Sending an email of the data reveals the correct number the user input though.

I can't understand why it does this and looking at my code i'm stumped. Can anyone see were I might be slipping up?

Thanks in advance!

<?php

require_once('phpmailer/PHPMailerAutoload.php');

//VARIABLES & INSERT QUERY

if (isset($_POST["submit"])) {

// Get the form fields and remove whitespace.
$name   = strip_tags(trim($_POST["name"]));
$mobile = strip_tags(trim($_POST["mobile"]));
$email  = filter_var(trim($_POST["email"]), FILTER_SANITIZE_EMAIL);
$userID = mysqli_insert_id($success);

//INSERT INTO DATABASE

$stmt = $success->prepare("INSERT INTO test (name, email, mobile) VALUES (?, ?, ?)");
$stmt->bind_param("ssi", $name, $email, $mobile);
$stmt->execute();

$mail = new PHPMailer;

$mail->isSMTP();                                      // Set mailer to use SMTP

$mail->Host = '';
// Specify main and backup SMTP servers
$mail->SMTPAuth = true;                               // Enable SMTP authentication
$mail->Username = '';                 // SMTP username
$mail->Password = '';                           // SMTP password
$mail->SMTPSecure = '';                            // Enable TLS encryption, `ssl` also accepted
$mail->Port = ;                                    // TCP port to connect to

$mail->CharSet = 'UTF-8';
$mail->setFrom('', 'SMTP TEST');
$mail->addAddress($email, $name);              // Add a recipient
$mail->addReplyTo('', 'SMTP TEST');

$mail->Subject = 'SMTP TEST';
$mail->isHTML(true);                                  // Set email format to HTML
$mail->Body    = 'Your competiton number is <b>' . $name . '</b>';
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';

if(!$mail->send()) {
    // echo 'Message could not be sent.';
    // echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
   //  MOVE INTO IF STATEMENT IF EMAILING
   header('Location: success.php');
   exit();
}

$stmt->close(); $success->close();

}

?>

John, nothing stands out. It seems you have it right. You prepare the statement, give it the right type specification characters (ssi) and bind it correctly.

So, seeing nothing there, can you give us a copy of what the query string would look like if this were GET rather than POST? That is, I'd like to look at what you are sending this php page.

Hey jcorum

Thanks for the reply! I'm using the bit of code below to echo out the last mobile entry.

   <?php

        $sql = "SELECT mobile FROM test ORDER BY mobile DESC LIMIT 1";
        $result = $success->query($sql);

        if ($result->num_rows > 0) {
          // output data of each row
          while($row = $result->fetch_assoc()) {

            echo implode(" ",$row);
          }
        } else {
          echo "0 results";
        }

        $success->close();

        ?>

I'm printing out the mobile number onto the success page in the link below if you want to try. Even if the database records the correct number i.e. 1234567890 it still prints 2147483647. I don't know if this is a cache problem or something.

Test link http://johncashin.net/test/db_form2-mailer/

Image of the db mobile col http://imgur.com/uiTMmSO

1 Answer

The number seemed familiar, and now I know why. I searched for it in your files, and found it in the jquery.validate.js file:

        // maxlength may be returned as -1, 2147483647 ( IE ) and 524288 ( safari ) for text inputs
        if ( rules.maxlength && /-1|2147483647|524288/.test( rules.maxlength ) ) {
            delete rules.maxlength;
        }

For some reason, it's changing the phone number during validation to the max length for IE. You might want to start by testing on Chrome, Firefox or Safari and see what happens.

I also noticed, in your image of the db column data that not all of the saved numbers are 2147483647, only 7 of the 13 listed are. So you also might want to see why some are, some are not. I.e., what you did differently in the cases where they appear to have been saved correctly.

Hey jcorum

I never would of thought of the validation playing an issue. I'll dig into it and see what I can find.

Cheers Buddy!!

EDIT: I think I know what the issue was and just wanted to post if anyone ever has the same issue.

My mobile col was set to 32-bit integer but would only accept the correct input data if it was under 10 digits. Once over I would get the maxlength bug. As a work around I am now just storing mobile numbers as a string.