I really need help on this and couldn't find any previous questions related to my problem so here I am.
I have a registration form which is split up into 3 stages and therefore 3 tables. Keep in mind I have indexed and created foreign key constraints in phpmyadmin.
First stage is users (table):
CREATE TABLE `dbtest`.`users`(
`user_id` INT(5) NOT NULL AUTO_INCREMENT PRIMARY KEY,
`email` VARCHAR(35) NOT NULL,
`password` VARCHAR(50) NOT NULL,
UNIQUE(`email`)
) ENGINE = INNODB;
When a user fills in the first stage and the data is posted into the database and first user_id becomes 1. The user is then redirected onto the next stage (table):
CREATE TABLE `dbtest`.`personal`(
`player_id` INT(5) NOT NULL,
`first` VARCHAR(35) NOT NULL,
`last` VARCHAR(35) NOT NULL,
`mobile` INT(12) NOT NULL,
`parentmobile` INT(12) NOT NULL,
`gender` VARCHAR(6) NOT NULL,
`address` VARCHAR(50) NOT NULL,
`city` VARCHAR(25) NOT NULL,
`postcode` VARCHAR(10) NOT NULL,
`county` VARCHAR(35) NOT NULL,
`country` VARCHAR(35) NOT NULL,
PRIMARY KEY(mobile)
) ENGINE = INNODB;
When he fills in this form all the data gets posted into the database however player_id is 0 not 1. My question is how do I pass the user_id from stage 1 to player_id in stage 2. I have attached pictures my code.
PHP code for Stage 1:
PHP code for Stage 2:
I do not know php, but I have faced similar problems in other languages. What you want, is the id of the last inserted row, right? And then use it to insert the other related parts of the registration.
All you really need to know, is how to find the id:
Object oriented style
mixed $mysqli->insert_id;
Procedural style
mixed mysqli_insert_id ( mysqli $link )
Reference and more info: http://php.net/manual/en/mysqli.insert-id.php
Related
This question already has answers here:
MySQL 1062 - Duplicate entry '0' for key 'PRIMARY'
(9 answers)
Closed 6 years ago.
I am building a CRUD app in PHP & MySql. It uses two tables, called users and medical_records:
CREATE TABLE `users` (
`id` int(11) NOT NULL,
`username` varchar(255) NOT NULL,
`first_name` varchar(255) NOT NULL,
`last_name` varchar(255) NOT NULL,
`email` varchar(255) NOT NULL,
`password` varchar(1024) NOT NULL,
`validation_code` text NOT NULL,
`active` tinyint(1) NOT NULL,
`telefon` varchar(255) DEFAULT NULL,
`oras` varchar(255) DEFAULT NULL,
`adresa` text,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
CREATE TABLE medical_records (
`id` int(11) NOT NULL AUTO_INCREMENT,
`user_id` int(11) NOT NULL,
`fo` VARCHAR(255),
`condition` VARCHAR(255),
`transfer` VARCHAR(255),
`memo` text(1024),
`body_temperature` VARCHAR(255),
PRIMARY KEY (`id`),
CONSTRAINT FK_medical_records_1
FOREIGN KEY (user_id) REFERENCES users(id)
ON UPDATE CASCADE ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1;
The "business logic" of these tables is: every user has one (only one) medical record.
I used and tested the application when it only had the users table. Register and login worked fine, so I conclude that the problem's source can't be the application's (PHP) code.
I later doped the users table and added the 2 fresh tables the application now uses (users and medical_records).
At this moment whenever I try to register a second user, I get the error:
QUERY FAILED: Duplicate entry '0' for key 'PRIMARY'
This happens despite the fact that both tables have auto incremented primary keys. What could be the explanation of that?
the id in the users table is not set to AUTO_INCREMENT. that's why this happens.
I am developing a website (using PHP,MySQL) that deals with resumes of job-seekers. So the database should hold data like education details, projects, skills, etc. I have created different tables for them like shown below.
Basic info table
CREATE TABLE IF NOT EXISTS `candidates` (
`candId` bigint(20) NOT NULL,
`firstName` varchar(255) NOT NULL,
`lastName` varchar(255) NOT NULL,
`email` varchar(255) NOT NULL,
`contact` char(10) NOT NULL,
`address` text NOT NULL,
`gender` char(1) NOT NULL,
)
Experience table
CREATE TABLE IF NOT EXISTS `Experience` (
`candId` bigint(20) NOT NULL,
`companyName` varchar(255) NOT NULL,
`duration` varchar(20) NOT NULL,
`jobRole` varchar(255) NOT NULL,
`workType` varchar(50) NOT NULL
)
Like this,i have created tables for master degree(course,percentage),bachelors degree(course,percentage),skills(name,efficiency),achievements,etc.
In a page,i want to display all the information.For that 10 tables should be accessed in the page.I know that will load the page slowly.Are there any better ways to implement it?
What you need to do is use indexes, foreign keys and MySQL JOIN to achieve the performance.
So Table which have candId should be referenced from main candidates table using foreign keys.
Now when you fetch candidates's all details, use single query using JOIN to get all the candidate's related data. This will help you to improve performance issue.
Whenever I try to create a new table with this...
CREATE TABLE IF NOT EXISTS `users` (
`uid` int(11) NOT NULL AUTO_INCREMENT,
`email` varchar(300) NOT NULL UNIQUE,
`password` varchar(300) NOT NULL,
`activation` varchar(300) NOT NULL UNIQUE,
`status` enum('0','1') NOT NULL DEFAULT '0',
PRIMARY KEY (`uid`)
)
I get a pop up that says. Missing value in the form!. I've tried looking for answers, but most of them say to disable Ajax, which I cannot do. I've also tried different browsers and nothing seems to work. I'm using phpMyAdmin 4.1.6
varchar must have no more than 255 character, and you set is 300 character
look at this ( https://dev.mysql.com/doc/refman/5.0/en/char.html )
Hi any one please help i have a contact table in which i can Insert,Delete,Modify database using PHP web pages....but only current changes will be updated to database. what i want is how i can maintain history of database...
Is there any tutorial for this using (PHP/MYSQL).
I tried creating version of MySQL table for patient... how to proceed further.
CREATE TABLE IF NOT EXISTS `contact` (
`name` varchar(30) NOT NULL,
`phone` varchar(12) NOT NULL,
`mobile` varchar(12) NOT NULL,
`email` varchar(30) NOT NULL,
`address` text NOT NULL,
`conid` int(11) NOT NULL AUTO_INCREMENT,
PRIMARY KEY (`conid`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=4 ; # MySQL returned an empty result set (i.e. zero rows).
CREATE TABLE IF NOT EXISTS `contactversion` (
`name` varchar(30) NOT NULL,
`phone` varchar(12) NOT NULL,
`mobile` varchar(12) NOT NULL,
`email` varchar(30) NOT NULL,
`address` text NOT NULL,
`conid` int(11) NOT NULL,
`revision_id` int(11) AUTO_INCREMENT,
type ENUM('INSERT', 'UPDATE', 'DELETE') NOT NULL,
`change_time` DEFAULT current_timestamp,
PRIMARY KEY (`revision_id`)
);
what to do next....
When running the queries to contact, just simply run this right before to take the current contact and copy it in your revision table...
"INSERT INTO
contactversion (name,phone,mobile,email,address,conid,type)
SELECT
name,phone,mobile,email,address,conid,'".$type."' as type
FROM contact
WHERE conid='".$conid."'"
Both tables will require to be identical, with contactversion having type and change_time as additionnal last columns.
It is obvious that this query should be ran before UPDATE and DELETE of the contact table, but after an INSERT. If you are updating multiple contacts with another where clause than the conid, you'll want to consider building the where statement in a variable to use it inside the INSERT's SELECT and the UPDATE/DELETE
While creating contactversions table make sure conid should not be primary key and auto incremented. I hope that is causing the problem.
I have a project coming up for doing Admin functions so my question is this. I will try and be clear as possible.
I will have one SUPER-USER who updates all information for other regular-users/people(being our clients).
The clients/regular-users when they log in will only see their info and download files uploaded by SUPER-USER and not see for regular-users.
So if you are Client:#01 you will see the dashboard (welcome page) and your info. Can anyone suggest possible database designs for this.
How to use left/right sql-joins between the user and files table?
UPDATE
I have a users table as well as a company table that the user belongs to. So essentially I want something like this::
$sql = select everything in the users table where the username and pass = to the given form, then left or right join that username to the company that he belong to.
Then they will see their information. if logged in successfully. Because user #01 belongs to company #03 /#01 etc...
USER TABLE looks so
`id` int(11) NOT NULL AUTO_INCREMENT,
`fname` varchar(50) NOT NULL,
'lname` varchar(100) NOT NULL,
`username` varchar(50) ,
`password` varchar(100) ,
`company` varchar(50) // the company name that ther user belongs to
PRIMARY KEY (`id`)
COMPANY table
'id' int(11) not null auto_increment,
'user_id' int(11) //This is to tie the users to this table
'description' varchar(text),
'filename' varchar(25) not null,
'mimetype' varchar (25) not null
PRIMARY KEY ('id')
Well, it depends on how simple or complex you want to go. with something like this I usually will keep it relatively simple and have a main user database (for all users) example:
CREATE TABLE IF NOT EXISTS `user` (
`user_id` int(255) NOT NULL AUTO_INCREMENT,
`user_name` varchar(50) NOT NULL,
`user_pass` varchar(100) NOT NULL,
`user_permissions` tinyint(1) NOT NULL DEFAULT '0',
`active` tinyint(1) NOT NULL DEFAULT '1',
`date` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`id`)
) ENGINE=MYISAM;
Then I would have possible a second table of permissions depending on how many permissions I was going to have. If all you are going to have is users and super users then you could probably just assign users a value of 0 and then super user a value of 1.
Then in your PHP script it would treat the users different based on their "user_permissions" value.
Now if you are intending to have lots of different levels of permissions then I would definitely create at least one more table to define permissions example:
CREATE TABLE IF NOT EXISTS `permission` (
`permission_id` int(255) NOT NULL AUTO_INCREMENT,
`permission_name` varchar(100) NOT NULL,
`permission_value` int(10) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MYISAM;
Then in the permissions table you could assign all sorts of different permissions... read, write, publish, admin, regular user, super user etc.
This is just a very simple starting point. hope that helps.