PHP mysqli准备的语句删除了前导零 [英] PHP mysqli prepared statements removing leading zeros

查看:78
本文介绍了PHP mysqli准备的语句删除了前导零的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在我的数据库中输入电话号码,该列设置为varchar(15),并且在phpmyadmin内部它将接受前导0的电话号码(例如07712123123).

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).

在我的PHP脚本中,我有一个变量$ contactNo,它是一个包含07712123123的字符串

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();
}

我已经在语句之前和之后回显了该变量,并且0在那里,因此必须在将其放入数据库时​​将其删除.我是刚准备好的语句的新手,无法弄清楚该如何解决.

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.

谢谢!

推荐答案

您在问题中提供的准备好的语句没有流程,所以我猜想数据类型有一个静默转换.

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

再次检查列number确实是VARCHAR类型. 我很确定您在那里有一个INT(或类似的)类型.

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

这篇关于PHP mysqli准备的语句删除了前导零的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆