GRS
GRS

Reputation: 3084

SQL query not executing via php, can't find the error source

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

Upvotes: 0

Views: 36

Answers (1)

ScaisEdge
ScaisEdge

Reputation: 133360

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

Upvotes: 4

Related Questions