Page 1 of 1

php mysql insert error

Posted: Wed Jul 26, 2006 11:38 pm
by remus
Hi, im new to php and mysql and am learning from a book called Sams teach yourself "PHP, MySQL and Apache all in one"
The book is ok, and is helping me learn the new stuff. I used to be into access and asp.

Heres my problem, the book has gotten me to write a php script that creates a table named testtable with 2 fields, one is called id and is int not null prmary key auto_increment, the other is called testField and is varchar(75). The create table script works fine.
But when i try the insert script example from the book to insert some data I get an out of range of column error.
The insert code looks like this.

insert into testtable values('', 'some value')

So I guess the problem is that im trying to insert an empty string into an int column type.... this does not make sense, but thats how the book says to do it, and is the same in the code examples that are on a cd that came with the book.

Posted: Wed Jul 26, 2006 11:50 pm
by bdlang
If your column type is defined as you say, entering an empty string, a NULL or a 0 should update the record with the appropriate value.

You might try changing your query, specify the `testField` column only, as in

Code: Select all

INSERT INTO testtable (testField) VALUES ('some value');
Leaving the `id` column off altogether is the alternate method of forcing the AUTO_INCREMENT update.

Otherwise, can you give us the output of the following statement:

Code: Select all

DESCRIBE testtable;

Posted: Wed Jul 26, 2006 11:53 pm
by JasonMWaldo
Try:

Code: Select all

INSERT INTO testtable VALUES('NULL', 'some value')

Posted: Thu Jul 27, 2006 12:00 am
by bdlang
Actually, that's attempting to insert the string literal 'NULL' into the INT type column, that won't work either.

Specify NULL (without the quotes)

Code: Select all

INSERT INTO testtable VALUES (NULL, 'some value');
Note it is usually recommended to specify the columns rather than doing a 'blind insert'.

Posted: Thu Jul 27, 2006 1:21 am
by JasonMWaldo
bdlang, you are absolutely right, my error.