2013-12-20 61 views
1

我在一個MySQL數據庫中存儲郵政編碼爲varchar(10)。由於某種原因,將其存儲在一張桌子中並不像另一張桌子那樣工作。這兩個字段之間的唯一區別是一個是varchar(10) Nullable,另一個是varchar(10)Nullable列正在保存沒有前導零的郵政編碼,因此'05415'變爲5415。這在其他表格中工作得很好。我認爲他們被存儲在同一頁面上,但我似乎無法找到問題。我對PHP不太好,所以我非常感謝這裏的幫助。PHP郵編失去領先零

這是在網站上註冊的功能.... db中的註冊表正在保存帶有前導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); 
+0

你可以'var_dump($ this-> fields)'? – sjagr

+0

'[「ShippingZip」] =>字符串(5)「05154」'看起來是對的。嗯 – jlg

+0

@sjagr我還添加了'var_dump($ this-> record)',但它仍然顯示正確的郵政編碼。我認爲這行'$ newId = $ this-> record-> insert();'是插入數據,但如果郵政編碼顯示罰款時,我傾倒它,那麼問題在哪裏呢? – jlg

回答

4

如果我有一個猜測我將承擔沿途的某個地方你變量被存儲爲一個整數,如果發生這種情況,那麼你肯定會丟失前導'0'。有幾個選項可以查看變量的存儲位置,並確保將其存儲爲字符串。

或者,你總是可以確保它通過這在PHP 5號:

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

看到這個:http://www.php.net/str_pad

(雖然我會建議你找到它被存儲爲如果你找不到它,這將工作)

+0

我一直在尋找無處不在找到問題。我需要用您提供的代碼替換我的代碼? – jlg

+0

這取決於它失敗的地方。你可能需要將這個變量存儲爲你的變量,以便每個使用這個數據的地方都有前導0,或者在你發現你的變量失敗的每個地方使用它(但是如果你發現變量是如果沒有你,那麼我們可以更好地解決問題)。 – bobkingof12vs

+0

我想我可能已經找到它了。我將用我認爲沒有正確插入的代碼編輯原始帖子。 – jlg

-1

數據庫中的數據類型應該是字符(5)的郵政編碼。