-1

What is the point, in the assembly of denver, everything is running smoothly, but LAMPP (on Ubuntu 16.04 LTE) does not process the second request, php has set 5.6.

$result2 = mysql_query ("INSERT INTO users (login,password) 
VALUES('$login','$password')");
$result2 = mysql_query ("INSERT INTO tasters SET id=LAST_INSERT_ID()");

In the users table there is an id field with A_I, and the same field in tasters, but without A_I, both designated as INDEX. Tables are created in advance in this way:

CREATE TABLE IF NOT EXISTS `users` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`login` varchar(18) NOT NULL,
`password` varchar(18) NOT NULL,
`status` int(11) NOT NULL DEFAULT '3',
PRIMARY KEY (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;

And:

CREATE TABLE IF NOT EXISTS `tasters` (
`id` int(11) NOT NULL,
`name` varchar(32) NOT NULL,
`surname` varchar(32) NOT NULL,
`patronymic` varchar(32) NOT NULL,
`sex` int(11) NOT NULL,
`birth` date NOT NULL,
`salary` int(11) NOT NULL,
`marital_status` int(11) NOT NULL,
`children` int(11) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
3
  • Are you sure that this "INSERT INTO tasters SET id=LAST_INSERT_ID()" have correct syntax for INSERT command? Did you try to run it by hand? – mariaczi Apr 6 '18 at 6:37
  • @mariaczi i checked it an hour ago (but on denwer) and it is ok, on LAMPP it isn't. How is it trying to run it by hand? – Arty_Prof Apr 6 '18 at 6:43
  • 1
    "running by hand" - login to mysql console, run the first insert command and next run the second. I'm very interested of the output for the second insert command. – mariaczi Apr 6 '18 at 6:51
1

Firstly, the tasters table has all fields marked as not null and has no default. You are trying to insert a row by only setting the id field, leaving the other with a null value. I would not expect the second insert to work because they are declared as not null. So your statement tries to insert an invalid record. Set default values for all fields or set them all in an insert statement.

Use

CREATE TABLE IF NOT EXISTS `tasters` (
  `id` int(11) NOT NULL,
  `name` varchar(32) NOT NULL DEFAULT '',
  `surname` varchar(32) NOT NULL DEFAULT '',
  `patronymic` varchar(32) NOT NULL DEFAULT '',
  `sex` int(11) NOT NULL DEFAULT '0',
  `birth` date NOT NULL DEFAULT '1970-01-01',
  `salary` int(11) NOT NULL DEFAULT '0',
  `marital_status` int(11) NOT NULL DEFAULT '0',
  `children` int(11) NOT NULL DEFAULT '0',
  PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;

or

INSERT INTO tasters SET id=LAST_INSERT_ID(), name = '', surname = '', patronymic = '', sex = 0, birth = '1970-01-01', salary = '0', marital_status = 0, children = 0;

Secondly, do not store plain passwords in the table users. You have a password function in MySQL. Use that for storing passwords:

INSERT INTO users (login,password) VALUES('$login',password('$password'));

When you check for passwords use encrypted passwords:

select * from users where login = $entered_login and password = password($entered_password));

In that way, you do not operate with plain passwords and this increases the security of your system: Also, varchar(18) is too small: use varchar(64) or more instead.

3
  • is it ok if i put them null? Really usefull answer, i define all fields null except 'id' in tasters all worked like i want! – Arty_Prof Apr 6 '18 at 7:09
  • Yes it is OK to leave them null. – nobody Apr 6 '18 at 7:16
  • Edited and upvoted. – Fabby Apr 7 '18 at 9:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.