Structuring User Data in MySQL [closed] - php

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I'm setting up a new web service, and need some help with organizing the data in my application. My application's written in PHP, and I plan to store all user data in using MySQL Databases.
I will have a bunch of users, and each of those users will have a bunch of data that is tied to their account. It's a note taking application, and so, naturally, for every user, I need to keep track of the number of notes, and the content of each of those notes.
How do I go about structuring this and translating this into MySQL tables? I know I have to do something that revolves around linked (related) tables, but can't seem to pinpoint it.
I have 10x1GB databases.

Have the following tables (you can choose your own names);
tbl_users
int(4) userid (PRIMARY)
varchar(128) firstname
varchar(128) lastname
tbl_notes
int(8) noteid (PRIMARY)
int(4) userid
text notecontent
As you can see, userid is the relation between the two tables (1 to many in this case).
See Database normalization from Wikipedia

Try following queries to create your tables:
Users:
CREATE TABLE IF NOT EXISTS `users` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`username` varchar(32) NOT NULL,
`password` varchar(255) DEFAULT NULL,
`email_address` varchar(96) NOT NULL,
`firstname` varchar(32) NOT NULL,
`lastname` varchar(32) NOT NULL,
`birth_date` datetime DEFAULT NULL,
`gender` char(1) DEFAULT NULL,
`street` varchar(16) NOT NULL,
`address` varchar(64) NOT NULL,
`city` varchar(32) NOT NULL,
`state` varchar(32) DEFAULT NULL,
`country` int(11) NOT NULL DEFAULT '0',
`postcode` varchar(10) NOT NULL,
`status` int(1) DEFAULT '0',
`created` datetime DEFAULT NULL,
`updated` datetime DEFAULT NULL
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;
Notes:
CREATE TABLE IF NOT EXISTS `notes` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`userid` int(11) NOT NULL,
`title` varchar(255) NOT NULL,
`content` text NOT NULL,
`date_added` datetime NOT NULL,
`date_sent` datetime DEFAULT NULL,
`status` int(1) DEFAULT NULL,
`locked` int(1) DEFAULT '0',
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;

Related

Why is primary key a duplicate [duplicate]

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.

Foreign key and registration form

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

Creating Appointment using mysql and php [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I have 3 tables in a mysql database
An appointment table:
CREATE TABLE `create_appointment` (
`aid` int(11) NOT NULL,
`aTime` time DEFAULT NULL,
`aDate` date DEFAULT NULL,
`pid` int(11) DEFAULT NULL,
`did` int(11) DEFAULT NULL,
`status` varchar(45) DEFAULT NULL
)
A doctor table :
CREATE TABLE `doctor` (
`did` int(11) NOT NULL,
`name` varchar(45) DEFAULT NULL,
`surname` varchar(45) DEFAULT NULL,
`speciality` varchar(45) DEFAULT NULL,
`email` varchar(45) DEFAULT NULL,
`password` varchar(45) DEFAULT NULL,
`number` varchar(45) DEFAULT NULL
)
And a patient table
CREATE TABLE `patient` (
`pid` int(11) NOT NULL,
`name` varchar(45) DEFAULT NULL,
`surname` varchar(45) DEFAULT NULL,
`email` varchar(45) DEFAULT NULL,
`password` varchar(45) DEFAULT NULL,
`number` varchar(45) DEFAULT NULL,
`gender` varchar(45) DEFAULT NULL
)
pid and did in create_appointment table are forgien keys from doctor and patient tables respectively
I want to create a form.php file where by a patient can create an appointment. And the doctor's information such as his speciality and I will also be saved since a a patient (isn't required to know a doctor's ID before reservation of appointment) . How do I go by this?
P.S Am a novice 6day old novice in php an mysql. Any help or tips will be appreciated.
Thank you :D
Since the user will be logged in, you should have pid stored in session or something, and the did can be determined by having a drop down of doctors or by browsing to the doctor's page and then clicking a button to make an appointment.
Other data like date and time can be done using a date picker by jQuery UI library or similar javascript library.
The aid is of course an auto-increment field, and the status is according to your business logic, you can start with something like "pending approval", then "approved" then "done" for example.

how to insert many items from a file txt to a database in one step

Hello guys I'm about to create a quiz as a school project
so the web application allow users to pass a quiz
the problem is I've too many questions to add and each question has a correct answer and 3 choices so it's way too difficult to add all this stuff in the database
so I'm wondered if I could do it automatically
this is a question table
CREATE TABLE IF NOT EXISTS `question` (
`Id_Qst` int(11) NOT NULL AUTO_INCREMENT COMMENT 'identifiant',
`choice1` varchar(150) NOT NULL,
`choice2` varchar(150) DEFAULT NULL,
`choice3` varchar(150) DEFAULT NULL,
`correctanswer1` varchar(150) NOT NULL,
`correctanswer2` varchar(150) DEFAULT NULL,
`qst1` varchar(150) NOT NULL,
`qst2` varchar(150) DEFAULT NULL,
`lvl` int(11) NOT NULL,
`Image` blob NOT NULL,
PRIMARY KEY (`Id_QstS`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
composed questions are included in my quiz so I chose to add qst1 and qst2
Thank you !

how to maintain records current as well as previous in mysql

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.

Categories