...

View Full Version : cannot insert



abanana
08-21-2006, 10:27 PM
i just don't understand why i cannot insert some form data into MySQL INNODB table, plz help!

insert statement
$q = "INSERT INTO books VALUES ('$title', '$author')";
return mysql_query($q, $this->connection);
first table
CREATE TABLE books (
title varchar(90),
author varchar(90)
) TYPE=INNODB;

no problem at all insert form data into this table, but when i change the table to
CREATE TABLE books (
book_id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
title varchar(90),
author varchar(90)
) TYPE=INNODB;

i cant insert nothing, very strange!
there's no foreign key involved, just a single table. what's the problem?

thx very much

GJay
08-21-2006, 11:05 PM
if you're not putting values in all the columns, you need to specify which ones you want- how would you expect mysql to know otherwise?
i.e your query should be:


$q = "INSERT INTO books (title,author) VALUES ('$title', '$author')";

the alternative is to pass NULL as the first column, mysql will then do it's autoincrement magic, and you'll get the same result, i.e.


$q = "INSERT INTO books VALUES (NULL,'$title', '$author')";


For the sake of clarity, the first method is preferable, it's much easier to see what you're doing.

abanana
08-22-2006, 07:09 AM
thanks Gjay, you are right to the point, i change the code you mentioned first, and it works now! thanks again!



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum