jlg jlg - 3 months ago 11
MySQL Question

PHP Zip Code Losing Leading Zero

I am storing zip codes in a MySQL database as

varchar(10)
. For some reason storing it in one table is not working like the other one. The only difference between the fields is that one is
varchar(10) Nullable
, and one is
varchar(10)
. The
Nullable
column is saving the zip code without a leading zero so
'05415'
becomes
5415
. This is working in the other table just fine. I think they are being stored on the same page, but I can't seem to find the problem. I'm not very good with PHP, so I would really appreciate some help here.

This is the function for registration on the site....the registration table in the db is saving the zip code with the leading 0, so I assume that this works.

$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) . "'
)";


This is what the process looks like for the orders. This is the table where the leading zero gets deleted before it gets inserted.

$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";


To get the zip code to display correctly, I updated the code with this, and the 0 showed up:

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

Answer

If I had a guess I would assume that somewhere along the way your variable is being stored as an integer, and if that happens then you would definitely lose that leading '0'. A few options to look at could be looking where the variable is stored and making sure it is stored as a string.

Alternatively, you can always make sure it has 5 numbers in php by using this:

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

See this: http://www.php.net/str_pad

(Though I would advise that you find where it is being stored as a number over 'faking it' in php. but if you can't find it, this would work)