error when insert new values into database in infinityfree - mysql

When i use InfinityFree hosting, my register form doesn't work. But in localhost it's no problem.
I am trying to insert into my phpmyadmin database. The first column is the user_id column an it's an auto_increment field.
if i do this :
INSERT INTO `user` (`user_id`, `email`, `username`, `password`, `game_id`) VALUES('', '$email', '$username', '$password', '$game_id')
the error is :
Incorrect integer value: '' for column 'user_id' at row 1
and if i do this :
INSERT INTO `user` (`user_id`, `email`, `username`, `password`, `game_id`) VALUES(NULL, '$email', '$username', '$password', '$game_id')
the error is :
Data truncated for column 'game_id' at row 1

Instead of assigning a NULL value to user_id, just leave the user_id out of the query.
It should be like this:
INSERT INTO user (email, username, password, game_id) VALUES('$email', '$username', '$password', '$game_id')

Related

MySQL INSERT and UPDATE with IF condition

I have a simple MySQL Database like:
CREATE TABLE `tab_update` (
`id` int(11) NOT NULL,
`number` int(11) NOT NULL,
`description` varchar(11) NOT NULL,
UNIQUE KEY `id` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
and some data inside:
INSERT INTO `tab_update` (`id`, `number`, `description`) VALUES ('111', '5', 'first value');
INSERT INTO `tab_update` (`id`, `number`, `description`) VALUES ('222', '5', 'first value');
INSERT INTO `tab_update` (`id`, `number`, `description`) VALUES ('333', '5', 'first value');
What I want is to:
insert data to the database, if the same 'id' does not exist already
or
update data if both below conditions are met:
a) 'id' is already in the database
b) new number value is bigger than the number stored already
What I am trying to do now is:
INSERT INTO tab_update (id, number, description) VALUES (333, 1, 'second value')
ON DUPLICATE KEY UPDATE number = GREATEST(number, VALUES(number)), description = 'second value'
this works partially - inserts data or update, however, I have a wrong behavior. If we have 'number = 1' (which is less then 5) this should not update the record.
At the moment keeps updating description, but should not.
Could you please guys help me improve this query?
update:
I am trying also:
INSERT INTO tab_update (id, number, description) VALUES (333, 6, 'second value')
ON DUPLICATE KEY UPDATE
number = GREATEST(number, VALUES(number)),
description = IF(VALUES(number) > number, VALUES(description), description)
but last line does not work as expected.

Operand should contain 1 column(s) Or Error in sql syntax

I checked almost all related topics, not working for me.
I am a beginner
Either I get "operand should contain 1 columns" or "you have an error in your sql syntax check the manual that corresponds to your mysql server version for the right sytax to use near ' ' at line 1 "
Here is my query :
create database makla;
use makla;
create table orders(
order_id int auto_increment primary key,
order_date DATE
);
create table productionitem(
order_id int not null,
item_name varchar (20),
item_description varchar (100),
constraint order_fk foreign key (order_id) references orders (order_id)
);
insert into orders(order_date) values ('2014/11/4');
insert into orders(order_date) values ('2017/9/30');
insert into orders(order_date) values ('2019/4/13');
insert into productionitem(order_id, item_name, item_description)
values (1, 'tv', 'samsung X');
insert into productionitem(order_id, item_name, item_description)
values (1, 'watch', 'swatch X');
insert into productionitem(order_id, item_name, item_description)
values (2, 'pan', 'metal X');
insert into productionitem(order_id, item_name, item_description)
values (3, 'cup', 'world X');
insert into productionitem(order_id, item_name, item_description)
values (3, 'chair', 'plastic X');
select *
from productionitem
where order_id in (select order_id
from orders
where order_date between '2015/11/4' and '2020/11/4')
please help,
You may need to put the date in proper format yyyy-mm-dd
insert into orders(order_date) values ('2014-11-04');
insert into orders(order_date) values ('2017-09-30'); -- notice 09 not just 9
insert into orders(order_date) values ('2019-04-13');
Same date format will be used for SELECT queries.

Table information retrieval in mySQL

ALL,
I just tried following query:
SELECT cols.column_name, cols.data_type, cols.character_maximum_length, cols.character_octet_length,
cols.numeric_precision, cols.numeric_scale, cols.column_default, cols.is_nullable,
cols.ordinal_position, kcu.column_name
FROM information_schema.columns cols, information_schema.key_column_usage kcu
WHERE kcu.constraint_name = 'PRIMARY' AND kcu.table_schema = cols.table_schema AND
kcu.table_name = cols.table_name AND cols.table_schema = 'draft' AND cols.table_name = 'leagues';
The last column of that query does return the primary key column name.
However, what I'd like to see instead is this:
If the column is a primary key then the query will output '1' in the last query column.
If the column is not a primary key, then the query will output '0' in the last query column.
Is there a function inside mySQL that will help me do that?
Thank you.
Never use commas in the FROM clause. Always use proper, explicit JOIN syntax.
The "function" is case:
SELECT cols.column_name, cols.data_type, cols.character_maximum_length, cols.character_octet_length,
cols.numeric_precision, cols.numeric_scale, cols.column_default, cols.is_nullable,
cols.ordinal_position,
(CASE WHEN kcu.column_name IS NULL THEN 0 ELSE 1 END) as pk_flag
FROM information_schema.columns cols LEFT JOIN
information_schema.key_column_usage kcu
ON kcu.constraint_name = 'PRIMARY' AND kcu.table_schema = cols.table_schema AND
kcu.table_name = cols.table_name
WHERE cols.table_schema = 'draft' AND cols.table_name = 'leagues';
This is the query I will use:
SELECT cols.column_name, cols.data_type, cols.character_maximum_length, cols.character_octet_length,
cols.numeric_precision, cols.numeric_scale, cols.column_default, cols.is_nullable,
cols.ordinal_position,
(CASE WHEN kcu.column_name = cols.column_name THEN 1 ELSE 0 END) as pk_flag
FROM information_schema.columns cols, information_schema.key_column_usage kcu
WHERE kcu.constraint_name = 'PRIMARY' AND kcu.table_schema = cols.table_schema AND
kcu.table_name = cols.table_name AND cols.table_schema = 'draft' AND cols.table_name = 'leagues';
It works as expected in mySQL Workbench.
Note: This is not an answer to your question.
#Igor, here are my tests regarding running a select query using FROM on two tables (table1, resp. table2), compared with a select command called by LEFT JOIN-ing the same tables.
As you mentioned, there is indeed a difference: LEFT JOIN-ing implies the existence of one or more relations between the tables, but FROM-ing do not (in the context).
My question is, why would/should data from uncorrelated tables be fetched?
Table creation:
-- Create table1 table.
CREATE TABLE `tests`.`table1` (
`id` INT(11) NOT NULL AUTO_INCREMENT,
`name` VARCHAR(50) NULL,
PRIMARY KEY (`id`));
-- Create table2 table.
CREATE TABLE `tests`.`table2` (
`id` INT(11) NOT NULL AUTO_INCREMENT,
`name` VARCHAR(50) NULL,
PRIMARY KEY (`id`));
Value insertions:
-- Insert values into table1 table.
INSERT INTO `tests`.`table1` (`id`, `name`) VALUES ('1', 'TAB1 1');
INSERT INTO `tests`.`table1` (`id`, `name`) VALUES ('2', 'TAB1 2');
INSERT INTO `tests`.`table1` (`id`, `name`) VALUES ('3', 'TAB1 3');
INSERT INTO `tests`.`table1` (`id`, `name`) VALUES ('4', 'TAB1 4');
INSERT INTO `tests`.`table1` (`id`, `name`) VALUES ('5', 'TAB1 5');
INSERT INTO `tests`.`table1` (`id`, `name`) VALUES ('6', 'TAB1 6');
INSERT INTO `tests`.`table1` (`id`, `name`) VALUES ('7', 'TAB1 7');
INSERT INTO `tests`.`table1` (`id`, `name`) VALUES ('8', 'TAB1 8');
INSERT INTO `tests`.`table1` (`id`, `name`) VALUES ('9', 'TAB1 9');
INSERT INTO `tests`.`table1` (`id`, `name`) VALUES ('10', 'TAB1 10');
-- Insert values into table2 table.
INSERT INTO `tests`.`table2` (`id`, `name`) VALUES ('1', 'TAB2 1');
INSERT INTO `tests`.`table2` (`id`, `name`) VALUES ('2', 'TAB2 2');
INSERT INTO `tests`.`table2` (`id`, `name`) VALUES ('3', 'TAB2 3');
Table previews:
FROM query & result:
JOIN query & result:

MySQL SHA256 with Insert Statement

I want to create a script to fill a database for testing. How would I set a string to be hashed and the inserted into the database?
I have:
INSERT INTO `loop`.`User`
(`userID`,
`firstName`,
`lastName`,
`email`,
`password`,
`userName`,
`bio`,
`spamCount`)
VALUES
('gZvTtlPtjGRqeMBaLji3HxoKB5EZCsNL',
'Joe',
'Smith',
'test0#email.com',
SHA2('testgZvTtlPtjGRqeMBaLji3HxoKB5EZCsNL', 256),
'test0#email.com',
"TEST BIO",
0);
How do I hash the string and INSERT in same statement?
You can insert a SELECT instead of VALUES to run a function on one of the inputs:
INSERT INTO `loop`.`User`
(`userID`,
`firstName`,
`lastName`,
`email`,
`password`,
`userName`,
`bio`,
`spamCount`)
SELECT
'gZvTtlPtjGRqeMBaLji3HxoKB5EZCsNL',
'Joe',
'Smith',
'test0#email.com',
SHA2('testgZvTtlPtjGRqeMBaLji3HxoKB5EZCsNL', 256),
'test0#email.com',
"TEST BIO",
0;
If you already have the table filled by some content, you can alter it with the following :
ALTER TABLE `page` ADD COLUMN `hash` char(64) AS (SHA2(`content`, 256)) AFTER `content`
This solution will add hash column right after the content one, generates hash for existing and new records too without need to change your INSERT statement.

Trigger Error with MySQL

I'm doing something simple. Basically everytime a user register themself into the system, a trigger should run and copy the row which contain the ID and insert it into another table called userinfo.
Theres two table. One only contain username id and password, and another one contains foreign key linking the users.UserID and userinfo.UserID. I wish to insert new user to userinfo everytime users table is updated with new member. However the trigger keeps giving me error. Can someone tell me what am I doing wrong?
I'm open for any easier/proper way too. This is my first time playing with trigger.
CREATE TRIGGER NewUsers_Trigger AFTER insert ON users FOR EACH ROW
BEGIN INSERT INTO `pms`.`userinfo` (`UserID`, `FirstName`, `LastName`,
`Address`, `City`, `Country`, `PostCode`, `PhoneNumber`) VALUES ('2',
'test', 'test', 'test', 'test', 'test', 'test', 'test'); END
Trigger needs delimiter to be set, also you mentioned that there is a foreign key so the trigger should look like
delimiter //
CREATE TRIGGER NewUsers_Trigger AFTER insert ON users
FOR EACH ROW
BEGIN
INSERT INTO `pms`.`userinfo` (`UserID`, `FirstName`, `LastName`,
`Address`, `City`, `Country`, `PostCode`, `PhoneNumber`) VALUES (new.UserID,
'test', 'test', 'test', 'test', 'test', 'test', 'test');
END; //
delimiter ;
Note I have removed 2 with new.UserID which is the last inserted id from the user table so that same id is inserted to the other table and there is no error raised on foreign key constraints