Incorrect DECIMAL value when inserting MySQL 1 - php

Name Type
price decimal(7,0)
INSERT INTO products (uid, item_code,item_name, brand_name,model_number,weight,dimension,description,category,quantity,price,imagename)
VALUES (1, '01','Lenovo','Lenovo laptop','qwqeiu145','50kg','5x9','Lenovo is the best','Computers & Accessories','2','$250.0000','lenovo');
THIS IS THE ERROR
1366 - Incorrect decimal value: '$250.0000' for column 'price' at row
1

You need to Change
$250.0000
To
250.0000
because it's decimal datatype. You can only insert numbers in it, while $ is a string

In decimal fields you can only insert numbers, you just need to remove the $.
Take care u declared the field with 0 decimals, any number after the dot will be ignored.

Related

MySQl leading zeros get stored but wrong display

I am trying many typed of fields in MySQL to store numbers only, tried with INT, BIGINT with leading zeros, CHAR and VARCHAR to store INVOICE NUMBERS
I need the invoice numbers to be start with 0000000001, I stored it manually in PHPmyadmin
Now I want to display it and I dont get the leading zeros ....
Here is the database
field "folio" CHAR 15 stored I have manually did 0000000001 it displays fine on phpmyadmin
but here is the problem
<?php $maxprod=mysqli_query($datacenter,
"SELECT * FROM ventas
WHERE documento = 'boleta'
ORDER BY id DESC LIMIT 1");
while($lastcode=mysqli_fetch_assoc($maxprod)){?>
<input type="text" value="<?php echo $lastcode['folio']+1?>">
<?php }?>
the result of the query is 1 just 1 it does not display all other zeros
Any idea why?
PHP automatically converts string into number if you are performing any numerical operation on it. But you can keep the order number in integer form and pad it with zeroes when necessary:
str_pad($lastcode['folio']+1, 15, "0", STR_PAD_LEFT);
echo $lastcode['folio'] should show you a result with the leading zeroes, but not $lastcode['folio']+1.
As soon as you do that +1, the result is no longer a string. The $lastcode['folio'] variable is converted to a number in order to do the arithmetic operation on it.
The leading zeroes are just formatting and don't need to be stored with the number. If you need an autoincrementing number, just use an autoincrement integer in MySQL, and format the number with leading zeroes when you print it out.
This will retrieve the number as a string to display it as text.
Later you can manipulate it on your coding environment.
SELECT CAST(document_number AS CHAR) FROM ...

Mysql Changing My Float Values

I have a database that stores lat and long coords, along with other fields.
The lat and long are set to float 10,8.
When running the following command ...
INSERT IGNORE INTO records(unique_id, city, st,zip,lat,lon) VALUES
( '80936EN476', 'West Jordan', 'UT', '84088-5205', '40.59660', '-111.963' )
The insert is completed with no messages, but when the record is retrieved, the "lon" field has been set to -100.000000.
When I try to edit the value in phpMyAdmin, I get the following error.
Warning: #1264 Out of range value for column 'lon' at row 1
You need to put more digits in front.
10,8 means 2 digits before decimal and 8 decimal digits.
try putting something like 12,8
Do not use (m,n) on FLOAT or DOUBLE. It leads to an extra rounding. And, in your case, it leads to truncation in the upper digits.
Ok, the issue turned out to be the float values.
I changed it to 10,6 and that resolved the issue!

Value being inserted as 2147483647 into database

All phone numbers I am trying to enter into my database are being inserted as 2147483647.
The database field is an integer(11).
Before the phone number is inserted, it goes through the following code in order to remove all spaces, dashes, and brackets:
if (!empty($hphone)) $phone = $hphone;
else if (!empty($HomePhone)) $phone = $HomePhone;
else if (!empty($Phone1)) $phone = $Phone1;
$phone = preg_replace("/[^0-9]/", "", $phone);
Why is it inserting the phone number as 2147483647 every time, no matter what the phone number is?
If you can, convert the phone number to a VARCHAR field, don't store it as a signed (or unsigned) numeric value (int, bigint, double, etc...).
In this case, the limit for signed INT in MySQL of 2147483647 is what is causing your issue. Anything larger inserted into this field will be capped at that maximum value.
For example the phone number 555-555-5555 if bigger than that limit (5555555555 >2147483647), as such storing it would result in the max value 2147483647.
I also recommend not storing it as a BIG INT or any other numeric type. How will you handle extension or special encoded characters like:
+02 112020 10020
1-333-232-9393 x203
BTW: don't know if the first is real non-US number format, but you get the idea
Also, phone numbers with relevant leading 0's would be have some of it lost no mater how large the number:
021-392-9293
Would be the number 213929293
if you want to store it as a number use bigint because int has it's max value equal to 2147483647.
So whatever number you try to store that is higher than 2147483647 will be stored as 2147483647.
Here is some reference for mysql types:
http://dev.mysql.com/doc/refman/5.0/en/integer-types.html
I am also facing this type of problem and I have solved that problem after
Updating datatype From 'INT' TO 'BIGINT'.
#php #mysql #database #sql

Mysql not inserting binary data properly

I am working on data compression and for some reason i need only 8 bits.
I am converting number by decbin() and then inserting it in the mysql, the mysql column data type BIT width is 8 bit. I used mysql_query("INSERT INTO n (reading) VALUES (b'".$value."')") and tried this one toomysql_query("INSERT INTO n (reading) VALUES (".$value.")"). Before inserting the value is fine but after insertion its not the same value, it changes the value for example before insertion it echo the value 116 then I echo its binary value 1110100 and it insert the value in mysql column is 00110000.
function delta($reading){
global $flag;
$delta = $flag - $reading;
saveDelta(decbin($delta));
}
here is the other function where it saves the value
function saveDelta($dif) {
mysql_query("INSERT INTO n (reading) VALUES (".$dif.")");
}
The syntax "INSERT INTO n (reading) VALUES (b'".$value."')" should work provided that $value is properly encoded as a string of '0' and '1'.
EDIT: I noticed you didn't provide any "sequence number" while inserting your data. But, please remember that without using a proper ORDER BY clause, you cannot retrieve your bytes the order they where entered at first. Maybe you think you read "116" but MySQL return an other row from the table?
Here are some usage examples, First using the BIT type:
CREATE TABLE b (value BIT(8));
INSERT INTO b VALUES (0),(1), (255);
INSERT INTO b VALUES (b'00000000'),(b'00000001'), (b'11111111');
Please note that when retrieving BIT columns you will obtain signed result (i.e.: storing 255 will read -1).
You could retrieve your data either as signed 10-base integers or binary form (with optional padding):
SELECT value FROM b;
SELECT BIN(value) FROM b;
SELECT LPAD(BIN(value), 8, '0') FROM b;
As of myself, I would prefer the TINYINT UNSIGNED. This is an 8 bit type which support the same syntax for values (either <10-base digit> or b'xxxxxxxx') -- but will accept the UNSIGNED specifier:
CREATE TABLE t (value TINYINT UNSIGNED);
INSERT INTO t VALUES (0),(1),(255);
INSERT INTO t VALUES (b'00000000'),(b'00000001'), (b'11111111');
You could retrieve your data either as unsigned 10-base integers or binary form (with optional padding):
SELECT value FROM t;
SELECT BIN(value) FROM t;
SELECT LPAD(BIN(value), 8, '0') FROM t;
See http://sqlfiddle.com/#!2/4ff44/6 to experiment with both of them.

MySql decimal type display problem

I store money values in my db table. E.g. I have 2.50. But when I print that value the 0 is always missing so I get 2.5. The db table money field has the following type: decimal(6,2)
any idea how to fix that?
By default PHP echo and print don't print the trailing zeros of a floating point number.
To overcome this you need to use printf as:
$money = 2.50;
printf("%.2f",$money); // prints 2.50
echo $money; // prints 2.5
print $money; // prints 2.5
The format specifier used in printf is "%.2f" what it means is always print atleast two digits after the decimal point and if there are not so many digits use 0. Note that it is atleast two digits not equal to two digits. So if I print 1.234 with that format it will not truncate it to 1.23 but will print 1.234 and if I print 1 it will result in 1.00

Categories