Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3

Thread: cannot insert

  1. #1
    New Coder
    Join Date
    Jun 2006
    Posts
    37
    Thanks
    0
    Thanked 0 Times in 0 Posts

    cannot insert

    i just don't understand why i cannot insert some form data into MySQL INNODB table, plz help!

    insert statement
    PHP Code:
    $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
    Last edited by abanana; 08-21-2006 at 09:30 PM.

  • #2
    Senior Coder
    Join Date
    Sep 2005
    Posts
    1,791
    Thanks
    5
    Thanked 36 Times in 35 Posts
    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:
    PHP Code:
    $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.
    PHP Code:
    $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.

  • #3
    New Coder
    Join Date
    Jun 2006
    Posts
    37
    Thanks
    0
    Thanked 0 Times in 0 Posts
    thanks Gjay, you are right to the point, i change the code you mentioned first, and it works now! thanks again!


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •