PHP mysqli prepared statements removing leading ze

2019-09-06 00:11发布

问题:

Im trying to enter a phone number into my database, the column is set as varchar(15) and inside phpmyadmin it will accept a phone number with a leading 0 (07712123123 for example).

In my PHP script i have the variable $contactNo which is a string containing 07712123123

but when I use my prepared statement to input the data into the database the leading zero is removed. How can I stop this?

// Insert data into customer_accounts
if($stmt = $mysqli -> prepare("INSERT INTO customer_accounts (username, password, firstname, lastname, email, number) VALUES (?,?,?,?,?,?)")) {
    $stmt -> bind_param("ssssss", $username, $password, $firstname, $lastname, $emailAddress, $contactNo);
    $stmt -> execute();
    $stmt -> close();
}

I have echo'd out the variable before and after the statement and the 0 is there so it must be removed as its put into the database. I'm new to prepared statements and I cant figure out what to do to fix it.

Thanks!

回答1:

The prepared statement you provide in your question has no flow, so I am guessing there is a silent conversion of datatype.

Double-check that column number is indeed of type VARCHAR. I am pretty sure you have an INT (or similar) type there instead.