tags:

views:

94

answers:

2

I have a MySQL table, with the field ('ad_id').

I store variables in different fields with PHP.

One of these fields, the 'ad_id' field as mentioned above, stores the same exact nr over and over again, no matter what the "REAL" name is in the PHP file.

Example: $ad_id= 12345; When trying to store this, the number 11111 is stored. ALWAYS, no matter what I change the $ad_id variable to.

I even echo the $ad_id variable, and it actually IS 12345, but MySQL stores a value of 11111 anyway.

CODE:

mysql_query("INSERT INTO cars_db 
       (ad_id, area, area_community, price, year, mileage, gearbox, fuel, poster_name, poster_email, poster_tel, poster_password, private_or_company, headline, description, salebuy, total_pics, changeable, hide_tel)
VALUES ('$ad_id', '$area', '$kommun', '$price', '$year', '$mile', '$gearbox', '$fuel', '$name', '$email', '$tel', '$ad_passw', '$priv_or_comp', '$headline', '$ad_text', '$forsale', '$nr_of_pics', '$changeable', '$hide_tel')");


Update with MySQL table info:

CREATE TABLE `cars_db` (
 `id` int(7) NOT NULL AUTO_INCREMENT,
 `ad_id` int(13) NOT NULL,
 `area` varchar(40) COLLATE utf8_swedish_ci NOT NULL,
 `area_community` varchar(50) COLLATE utf8_swedish_ci NOT NULL,
 `price` int(9) NOT NULL,
 `year` int(4) NOT NULL,
 `mileage` int(6) NOT NULL,
 `gearbox` varchar(12) COLLATE utf8_swedish_ci NOT NULL,
 `fuel` varchar(12) COLLATE utf8_swedish_ci NOT NULL,
 `insert_date` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
 `poster_name` varchar(40) COLLATE utf8_swedish_ci NOT NULL,
 `poster_email` varchar(50) COLLATE utf8_swedish_ci NOT NULL,
 `poster_tel` varchar(20) COLLATE utf8_swedish_ci NOT NULL,
 `poster_password` varchar(15) COLLATE utf8_swedish_ci NOT NULL,
 `private_or_company` int(2) NOT NULL,
 `headline` varchar(60) COLLATE utf8_swedish_ci NOT NULL,
 `description` text COLLATE utf8_swedish_ci NOT NULL,
 `salebuy` int(2) NOT NULL,
 `total_pics` int(2) NOT NULL,
 `changeable` int(1) NOT NULL,
 `hide_tel` int(1) NOT NULL,
 PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=134 DEFAULT CHARSET=utf8 COLLATE=utf8_swedish_ci


Update again:

I tried setting the $ad_id=012345678901 (12 digits) and it didn't work. Once again same problem. But then I tried setting $ad_id=555555 and it worked, MySQL stored that information... The length is the problem I think. Is there a maximum or something? Is there hidden decimals I don't know about. If so, how can I round it?


Update string insert into: All looks fine. Even the value of the $ad_id is ok, 12 digits, but that's not what's inserted into MySQL, because that's a different nr and it is only 10 digits!

INSERT INTO cars_db (ad_id, area, area_community, price, year, mileage, gearbox, fuel, poster_name, poster_email, poster_tel, poster_password, private_or_company, headline, description, salebuy, total_pics, changeable, hide_tel) VALUES (668244234626, 'Göteborg', 'Centrala Göteborg', '150 000', '2004', '14000', 'Manuell', 'Bensin', 'kamran', '[email protected]', '0704392064', 'hejhej', '1', 'Bmw 330ci Fin som fan', 'Hej.

- AC
- STEREO
- VINTERDÄCK', 'Säljes', '0', '0', '0')
+4  A: 

Camran you number is too big if you take a look at mysql documentation here it says:

Type  N byte  min value     max value 
INT   4   -2147483648      2147483647

You should generate a smaller random number also if you need that to be random you can use the MySQL RAND function.

RageZ
thanks RageZ, would never have figured it out without you.
Camran
+1  A: 

The number 668244234626 is out of range for an integer. It's being truncated to the max value for an integer, which is 2147483647.

If you run your INSERT statement in a MySQL client, you get this result:

Query OK, 1 row affected, 1 warning (0.00 sec)

Then if you show warnings:

mysql> show warnings;

+---------+------+------------------------------------------------+
| Level   | Code | Message                                        |
+---------+------+------------------------------------------------+
| Warning | 1264 | Out of range value for column 'ad_id' at row 1 |
+---------+------+------------------------------------------------+

I notice you declare ad_id INT(13). Note that the argument 13 has no effect on the range of values that you can store in the integer. A MySQL integer data type always stores a 32-bit number, and the value you gave is greater than 232.

If you need to store larger values, you should use BIGINT.

Bill Karwin