0

I created the artists table using the command:

$sql  = "CREATE TABLE artists (
            id int(11) NOT NULL auto_increment, 
            username VARCHAR(20), 
            password VARCHAR(20), 
            first_name VARCHAR(20), 
            last_name VARCHAR(50), 
            email VARCHAR(30), 
            phone VARCHAR(15), 
            genre VARCHAR(20), 
            location VARCHAR(20), 
            description VARCHAR(3000), 
            promo VARCHAR(200), 
            soundclound VARCHAR(100), 
            instruments VARCHAR(100), 
            event_type VARCHAR(100), 
            youtube VARCHAR(100), 
            experience VARCHAR(10), 
        PRIMARY KEY (id) 
    );";
$database->query($sql);

I would like to simply add a record of username and password to the table created above. I have a function which creates the following SQL query when I echo it:

INSERT INTO artists 
            (id,username,password,first_name,last_name,
            email,phone,genre,location,description,
            promo,soundcloud,instruments,event_type,youtube,
            experience) 
    VALUES ('', '12345', '12345', '', '', 
            '', '', '', '', '', 
            '', '', '', '', '', 
            '')

However, when I remove the echo $sql and call $database->query($sql) , it returns an error.

It works when I simply use the SQL

INSERT INTO artists (first_name,last_name) VALUES ('12345','12345')

EDIT: I've edited the function that goes through every single field, and inserts and id as well:

 // if ($database->query($sql)) {
 // $this->id = $database->insert_id();
 // return true;} else {return false;}
 echo $sql;

It returns the following:

INSERT INTO artists (username,password,first_name,last_name,email,phone,genre,location,description,promo,soundcloud,instruments,event_type,youtube,experience) VALUES ('133', '133', '', '', '', '', '', '', '', '', '', '', '', '', '')

But when I try to run it, I still get an error.

I tried a simple query INSERT INTO artists (username,password) VALUES ('123','123') which works fine

1
  • 2
    Don't bother giving a value to the id because it is already auto incremented. And please include the error it returns Commented Mar 2, 2017 at 11:06

1 Answer 1

4

the error is the '' (string empty ) for ID (int declared) value and the correct solution is baed on

don't insert the ID (is auto_increment)

INSERT INTO artists (
    ,username
    ,password
    ,first_name
    ,last_name
    ,email
    ,phone
    ,genre
    ,location
    ,description
    ,promo
    ,soundcloud
    ,instruments
    ,event_type
    ,youtube
    ,experience) 
VALUES (
    '12345'
    , '12345'
    , ''
    , ''
    , ''
    , ''
    , ''
    , ''
    , ''
    , ''
    , ''
    , ''
    , ''
    , ''
    , '');

the error is the '' (string empty ) for and int value

Sign up to request clarification or add additional context in comments.

5 Comments

So that is really the problem, and here I thought it is just a small part of the error that's why I commented it haha
@CarlJan .i have updated the answer with the error cause
Nice. OP didn't include the error that's why I'm not sure if my comment is correct or not
Thanks, I modified the code, but still seem to be getting the error
The query seems correct .. then update your question show the exact error message ..

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.