PHP邮政编码失去领先零

时间:2013-12-20 17:48:35

标签: php mysql

我将邮政编码存储在MySQL数据库中varchar(10)。出于某种原因,将其存储在一个表中并不像另一个表那样工作。字段之间的唯一区别是一个是varchar(10) Nullable,一个是varchar(10)Nullable列保存的邮政编码没有前导零,因此'05415'变为5415。这在另一张表中正常工作。我认为它们存储在同一页面上,但我似乎无法找到问题所在。我对PHP不是很了解,所以我非常感谢你的帮助。

这是在网站上注册的功能....数据库中的注册表保存带有前导0的邮政编码,所以我认为这是有效的。

 $Zip       = strip_tags(trim($_POST['MembersZip']));
    if (strlen($Zip = trim($Zip)) == 0) {
                $isError = true;
                print "<script>$('#MembersZip').addClass('error');</script>";
                print '<div class="errors">Please enter zip / postal code</div>';
            }
    if (strlen($Zip = trim($Zip)) < 5) {
                    $isError = true;
                    print "<script>$('#MembersZip').addClass('error');</script>";
                    print '<div class="errors">Zip code must be at least 5 digits</div>';
                }
    $sql = "INSERT INTO tbl_ecommerce_addresses ( 
                    ead_postal_code
                ) VALUES (
                    '" . addslashes($Zip) . "'
                )";

这是订单的流程。这是在插入前删除前导零的表。

        $fieldName = "BillingZip";
        $fieldValue = strip_tags($_POST[$fieldName]);
        if (!$fieldValue || strlen($fieldValue = trim($fieldValue)) == 0) {
            $isError = true;
            print "<script>$('#{$fieldName}').addClass('error');</script>";
            print '<div class="errors">Please enter billing zip / postal code</div>';
        } else {
            $this->fields[$fieldName] = $fieldValue;
            $this->record->eod_billing_postal_code = $fieldValue;
        }
    $Zip    = $this->record->eod_billing_postal_code;
    if (strlen($Zip = trim($Zip)) < 5) {
                $isError = true;
                print "<script>$('#BillingZip').addClass('error');</script>";
                print '<div class="errors">Billing Zip Code must be at least 5 digits</div>';
            }
It looks like this line
$newId = $this->record->insert();
is doing the insert, but when I do a var_dump of $this->record, the zip code still shows the leading 0. The only other thing I can think of is that the payment gateway is changing it somehow.


        $paymentType        = urlencode("Authorization");  // 'Sale' or 'Authorization'
        $firstName          = urlencode($this->fields["BillingFirst"]);
        $lastName           = urlencode($this->fields["BillingLast"]); 
        $creditCardType     = urlencode($this->fields["CardType"]); 
        $creditCardNumber   = urlencode($this->fields["CardNumber"]); 
        $padDateMonth       = urlencode(str_pad($this->fields["CardMonth"], 2, '0', STR_PAD_LEFT)); 
        $expDateYear        = urlencode($this->fields["CardYear"]); 
        $cvv2Number         = urlencode($this->fields["CardCode"]); 
        $address1           = trim(urlencode($this->fields["BillingAddress"]));
        $address2           = urlencode($this->fields["BillingAddress2"]); 
        $city               = urlencode($this->fields["BillingCity"]); 
        $state              = urlencode($this->fields["BillingState"]); 
        $zip                = urlencode($this->fields["BillingZip"]); 
        $country            = urlencode($CountryCode); // US or other valid country code 
        $amount             = urlencode($this->fields["PurchasedTotal"]); 
        $currencyID         = urlencode($this->siteConfig->cy_code); 
        $ipAddress          = $main->getIP();
        $invoice            = substr($this->fields["CardNumber"],-4,4) . substr($this->fields["BillingZip"],0,4) . substr($this->fields["PurchasedTotal"],0,2);

        $nvpStr = "&PAYMENTACTION=$paymentType&IPADDRESS=$ipAddress&AMT=$amount&CREDITCARDTYPE=$creditCardType&ACCT=$creditCardNumber".
        "&EXPDATE=$padDateMonth$expDateYear&CVV2=$cvv2Number&INVNUM=$invoice&FIRSTNAME=$firstName&LASTNAME=$lastName".
        "&STREET=$address1&CITY=$city&STATE=$state&ZIP=$zip&COUNTRYCODE=$country&CURRENCYCODE=USD";

为了正确显示邮政编码,我用这个更新了代码,0出现了:

$zip = str_pad($order->eod_shipping_postal_code, 5, '0', STR_PAD_LEFT);

2 个答案:

答案 0 :(得分:4)

如果我有一个猜测,我会假设你的变量被存储为一个整数的方式,如果发生这种情况,你肯定会失去领先的'0'。要查看的几个选项可能是查找存储变量的位置并确保将其存储为字符串。

或者,您可以使用以下命令始终确保它在php中有5个数字:

str_pad($zip, 5, '0', STR_PAD_LEFT)

请参阅:http://www.php.net/str_pad

(虽然我会建议你在php中找到它作为一个数字存储在'伪造它'的数字。但如果你找不到它,这将有效)

答案 1 :(得分:-1)

对于邮政编码,数据库中的数据类型应为char(5)。