PHP / MySQL varchar field entered as a number - php

I have within a form a textbox named PO_Number. The form submit by post to another page the textbox value.
In the second page I get $_POST['PO_Number'] and enter in MySQL.
MySQL field is varchar(15). As soon as the string of PO_Number starts with a letter or a number everything is OK.
The problem: sometimes the PO (Purchase Order) number start with 00 or 000 and it is stored with a comma before the 00
For example:
GH93737 - works
9087893 - works
0011132 - entered in database as ,0011132 (see the comma?)
The insert looks normal:
mysql_query("INSERT INTO table_name (PO_Number, ....) VALUES ('".$_POST['PO_Number']."',......)");
Many thanks for your suggestions and your help.

I'm wondering if this has something to do with your browser/server character encoding and how it's interpreting those specific numbers because all of those leading zeros and ones might be getting interpreted as a binary number?
Here's some brief info on that point:
A character encoding tells the computer how to interpret raw zeroes and ones into real characters. It usually does this by pairing numbers with characters.
http://htmlpurifier.org/docs/enduser-utf8.html

Related

Compare and trim binary/unicoded string to normal string?

I am using below mysql query to check which records vary from the trimmed value
SELECT id, BINARY(username) as binary_username, TRIM(username) as trim_username FROM table.
Above query returns binary value and trimmed value as shown below.
Result of mysql query:
Highlighted values in above image show that binary value vary from trimmed value.
I tried below 2 things:
calculating length of both binary and trimmed column but it is same LENGTH(binary_username) != LENGTH(trim_username).
equating them directly binary_username != trim_username.
but both of them are returning empty records.
How can I fetch these highlighted entries using mysql?
Edit 1: I have added HEX value in the query result
SELECT id, BINARY(username) as binary_username, TRIM(username) as trim_username, HEX(username) as hex_username FROM table
Thanks in advance...
To avoid storing, trimming, etc, the trailing zeros, use VARBINARY instead of BINARY. Why, pray tell, are you using BINARY for text strings??
Please do SELECT HEX(username) FROM ... so we can further diagnose the problem. That screenshot is suspect -- we don't know what the client did to "fix" the output.
Well, none of those are encoded in UTF-8, nor anything else that I recognize. The 'bad' characters (02, 04, 0c 17), are all "control codes" in virtually all encodings. ("Unicode" is not an encoding method, so it is not relevant.)
Would you like a REGEXP that tests for control codes?
In PHP, json_encode has an option for JSON_UNESCAPED_UNICODE. See https://www.php.net/manual/en/function.json-encode.php
But that generates \u1234 type text.
When storing binary data into MySQL, use the binding or escaping mechanism in PDO or mysqli.

How to sanitizing phone number before searching in multi search field?

I have a field in which a user can search a MySQL database for email, phone, and username. All numbers in the database are 10 digit (1231231234) format.
IF (big if there) the user enters a phone number in the following format(s) I want it to be sanitized into just a 10 digit string as it correlates in the database:
(123)123-1234
123-123-1234
123.123.1234
+1(123)123-1234
11231231234
Usernames and emails are allowed to have . and - characters. Hence I don't know how to use PHP to determine if it is in one of these formats and then sanitize it accordingly. Ideas?
This will remove anything you need to remove from phone numbers or any other value. Simply update the array if you need to remove other things.
$string = str_replace(array('-',' ','.','(',')',',','"','+'),'',$string);
libphonenumber can properly reformat phone numbers to a common format, adding international codes where appropriate, and then it can be stored in the database as a simple, searchable string.

PHP MySQL search character coding issues

I'm using PDO to connect to a MySQL database. In my connection string I have already added charset=utf8mb4 and all of my databases and tables are utf8mb4_unicode_ci, But I'm facing a problem.
In order to search for entries based on their title on content table I'm using the code below:
SELECT * FROM content WHERE title LIKE '%سيگنالها%'
the keyword is a Persian word. Now the above code returns 1 result which is correct and as expected.
But If I make a form in my PHP app and enter the SAME word either by using a macOS/Windows PC or by using an Android phone I get 0 results.
I tracked this issue down and it seems like even though the words entered by user look exactly the same as the one already in the database, they are in fact NOT the same.
According to this online tool, the decimal character code
for سيگنالها it's: 1587, 1610, 1711, 1606, 1575, 1604, 1607, 1575
While
for سیگنالها it's: 1587, 1740, 1711, 1606, 1575, 1604, 1607, 1575
Did you spot the difference? It's in bold. In fact if you copy both values and past them in here you will see the difference for yourself.
What can I do to solve this annoying problem? I'm using PHP 7 and MariaDB 10.1.
Your first "ي" in the word "سيگنالها" is different character from second word "سیگنالها" which is "ی"
First ي: is ARABIC LETTER YEH (U+064A)
Second ی: is ARABIC LETTER FARSI YEH (U+06CC)
They are different in their Unicode entities, so that they are not match.
Please see https://www.key-shortcut.com/en/writing-systems/%EF%BA%95%EF%BA%8F%D8%A2-arabic-alphabet/ for more information.
They are not the same character, even though they look the same when stringed together and might even have the same meaning.
The first string (1610) is ARABIC LETTER FARSI YEH[1] while the other (1740) is ARABIC LETTER YEH[2].
[1] https://en.wiktionary.org/wiki/%DB%8C
[2] https://en.wiktionary.org/wiki/%D9%8A
I also created a simple form for PHP and tested both strings to see if the value sent through $_POST is kept. Result: the value isn't converted.
So what's probably going on is that you're using an Arabic keyboard to produce Farsi text. The recommended solution is some kind of normalization of the input.
See these discussions:
1) https://groups.google.com/forum/embed/?place=forum/persian-computing#!topic/persian-computing/xS-G0qIGS8A
2) https://github.com/Samsung/KnowledgeSharingPlatform/blob/master/sameas/lib/lucene-analyzers-common-5.0.0/org/apache/lucene/analysis/fa/PersianNormalizer.java
3) can't search in farsi text with arabic keyboard on iphone

Barcode input into database fails in php

When i scan a barcode in "form" it automatically inserts it in database but two first digits are missing (two first zeros) . So , for example if a barcode 0020166196800002 is scanned and inserted into database only number 20166196800002 is inserted without first two digits of "00". Please, help!
Databases do strip leading zeros if the column type is numeric and there is no zero padding configured.
For example, MySQL allows a column to be defined with "zerofill(x)", which results in every number being at least x digits long, and a stored zero will come up as x 0 characters instead of only one.

MySQL Int Removes 0

Hi I have got a column in my database which is set to Int.
But my data always starts with a 0 so whenever I add a new record, it strips the 0 off and I don't want it to do that incase the first character has to be a 1 at some point.
How can I overcome this issue?
Is the best way to use VARCHAR any then validate using PHP?
Update
If I enter 02118272 it is saved to the database as 2118272.
The integer 7 is the same thing as the integer 000000000000000000000000007. They're both... ya know... seven. When stored in the database, it's actually stored as something like 000000000000000000000000007 but most MySQL clients won't bother to show all those zeros.
If it's important in your application to show these numbers using a certain number of digits, you can add back as many leading zeros as you want using the str_pad() function:
str_pad($your_string, 10, '0', STR_PAD_LEFT);
Here, 10 is the length you want the string to be, '0' is the character that will get added on in order to make it that length, and STR_PAD_LEFT says to add characters to the left-hand side of the string.
If, on the other hand, the number '007' is fundamentally different than the number '7', then you will have to use a VARCHAR() field to store it. Those are no longer integers; they're strings with very different meanings.
What you should be storing in your database is data. Formatting of that data is the responsibility of applications, not the database itself.
I would store it as an integer and, if you need that to 7 decimal places with leading zeros, the right place to do that is after extraction of the data to your application.
I think that you should use varchar type for that field. If you want to convert a variable to integer in php you can simply do this:
$var=preg_replace("/[^\d]/","",$var);
with this you delete all characters that aren't numbers and then you can put this value into the db preserving the initial 0.

Categories