Home > Software engineering >  MySQL INSERT INTO putting every field at null or 0?
MySQL INSERT INTO putting every field at null or 0?

Time:11-16

Still learning sql, but I'm not understanding how all the data I'm putting in my tables are set to 0 or NULL ?

MySql commands

CodePudding user response:

The correct command:

insert into users (email, name, forename, pwhash) values ('mail', name, 'blaa', 'befbf')

You are getting null as sql format is wrong. Id is 1 because that is auto incremented as defined by you. But other columns are null or 0.

CodePudding user response:

Try

INSERT INTO users (email, name, forename, pwdssh)
    VALUES ('email', 'name', 'blaa', 'befbf');

id should be autoincremental as a primary key so no need to specify it. If you want to do it inline as you have attempted, I don't think a hyphen is the right syntax, it would be an equals, as in email='email'

Explicitly listing the columm names like this means you can specify the Column names followed by VALUES in any order. You could also miss out the column names and just say:

    INSERT INTO users VALUES ('email', 'name', 'blaa', 'befbf');

Where the values are listed in the order that the columns are defined. Doing it this way, the order becomes important.

You may find this quite useful

CodePudding user response:

You don't need to specify "field={value}", just put in the values in the proper order. INSERT INTO Table (Field1,..., FieldN) VALUES ('Value1',....,'ValueN');

https://www.w3schools.com/mysql/mysql_insert.asp

CodePudding user response:

You need to split up the columns and VALUES into separate statements (also the id should get added automatically since you have it as an identity, so you don't need to explicitly use it).

Try this:

INSERT INTO users (email, name, forename, pwhash) 
VALUES ('mail', 'name', 'blaa', 'befbf');

You can use this to insert multiple values at once if you need to

INSERT INTO users (email, name, forename, pwhash) VALUES 
('mail', 'name', 'blaa', 'befbf'),
('mail2', 'another name', 'blaa2', 'befbf123'),
('mail3', '1 more name', 'blaa3', 'befbf456'),
('mail4', 'one final name', 'blaa4', 'befbf789');

(Etc.)

  • Related