Dependent selection option field in codeigniter and mysql - php

I am new to Codeigniter, I just want to ask on how to populate the second select option field depending on the first option field from the database.
I have two tables the city and province. The first option field is the province if the user chooses on a particular province, the second option field which is the city will automatically populate.
This is my database.
CREATE TABLE `city` (
`city_id` int(11) NOT NULL AUTO_INCREMENT,
`city_name` varchar(45) NOT NULL,
`province_id` int(11) NOT NULL,
PRIMARY KEY (`city_id`,`city_name`),
KEY `province_idx` (`province_id`),
CONSTRAINT `province` FOREIGN KEY (`province_id`) REFERENCES `province` (`province_id`)
)
CREATE TABLE `province` (
`province_id` int(11) NOT NULL AUTO_INCREMENT,
`provice_name` varchar(45) DEFAULT NULL,
PRIMARY KEY (`province_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

Related

MySQL foreign key issue

I am having a problem trying to load my database into MySQL as I am having a few errors with my foreign keys. Things I have tried to do to fix this issue is:
- Putting foreign keys after the primary keys
Eg:
CREATE TABLE IF NOT EXISTS Passenger (
.......
PRIMARY KEY(tNum),
FOREIGN KEY (fNum) REFERENCES Flights(fNum),
FOREIGN KEY (fDate) REFERENCES Flights(fDate),
FOREIGN KEY (sCity) REFERENCES Flights(sCity),
FOREIGN KEY (eCity) REFERENCES Flights(eCity)
);
- Bunching the foreign keys in the format of
Eg:
ALTER TABLE Passenger ADD FOREIGN KEY (fNum, fDate, sCity, eCity) REFERENCES Flights(fNum, fDate, sCity, eCity);
The error I get is:
1005 - Can't create table 'airline.#sql-1d7_7c' (errno: 150)
My full code is:
DROP DATABASE IF EXISTS airline;
CREATE DATABASE IF NOT EXISTS airline;
USE airline;
CREATE TABLE IF NOT EXISTS Flights (
fNum char(6) not null,
pID char(4) not null,
fDate DATE not null,
eDate DATE not null,
sTime char(4) not null,
lTime char(4) not null,
sOStart char(4) null,
sOEnd char(4) null,
sCity varchar(30) not null,
eCity varchar(30) not null,
sOCity varchar(30),
sNum char(5) not null,
PRIMARY KEY (fNum)
);
CREATE TABLE IF NOT EXISTS Passenger (
tNum char(4) not null,
dPurch DATE not null,
pMethod varchar(30) not null,
fNum char(6) not null,
fDate DATE not null,
sCity varchar(30) not null,
eCity varchar(30) not null,
tType varchar(30) not null,
Price decimal(4,2) not null,
iType varchar(30) not null,
idNum char(8) not null,
fName varchar(30) not null,
lName varchar(30) not null,
Sex char(1) not null,
pAddress varchar(30) not null,
pPhone char(8) not null,
pEmail varchar(30) not null,
PRIMARY KEY(tNum)
);
CREATE TABLE IF NOT EXISTS Planes (
pID char(4) not null,
pType char(3) not null,
pDesc varchar(30) not null,
pRange char(4) not null,
Capacity char(3) not null,
mDate DATE not null,
pDate DATE not null,
sDate DATE not null,
PRIMARY KEY (pID)
);
CREATE TABLE IF NOT EXISTS Staff (
sNum char(5) not null,
sName varchar(30) not null,
sDOB DATE not null,
sAddress varchar(30) not null,
pCompany varchar(30) ,
pStart DATE ,
pEnd DATE ,
jID char(1) not null,
PRIMARY KEY (sNum)
);
CREATE TABLE IF NOT EXISTS Emergency (
eID char(5) not null,
sNum char(5) not null,
eName varchar(30) not null,
eAddress varchar(30) not null,
ePhone char(8) not null,
eEmail varchar(30) not null,
eRelationship varchar(30) not null,
PRIMARY KEY(eID)
);
CREATE TABLE IF NOT EXISTS Pilot (
sNum char(5) not null,
pID char(4) not null,
cDate DATE not null,
jID char(1) not null,
PRIMARY KEY(jID)
);
CREATE TABLE IF NOT EXISTS Attendant (
sNum char(5) not null,
tSDate Date not null,
tFDate Date not null,
tDesc Varchar(30) not null,
jID Char(1) not null,
PRIMARY KEY(jID)
);
ALTER TABLE Flights ADD FOREIGN KEY (pID) REFERENCES Planes(pID);
ALTER TABLE Flights ADD FOREIGN KEY (sNum) REFERENCES Staff(sNum);
ALTER TABLE Passenger ADD FOREIGN KEY (fNum) REFERENCES Flights(fNum);
ALTER TABLE Passenger ADD FOREIGN KEY (fDate) REFERENCES Flights(fDate);
ALTER TABLE Passenger ADD FOREIGN KEY (sCity) REFERENCES Flights(sCity);
ALTER TABLE Passenger ADD FOREIGN KEY (eCity) REFERENCES Flights(eCity);
ALTER TABLE Emergency ADD FOREIGN KEY (sNum) REFERENCES Staff(sNum);
ALTER TABLE Pilot ADD FOREIGN KEY (sNum) REFERENCES Staff(sNum);
ALTER TABLE Pilot ADD FOREIGN KEY (pID) REFERENCES Planes(pID);
ALTER TABLE Pilot ADD FOREIGN KEY (jID) REFERENCES Staff(jID);
ALTER TABLE Attendant ADD FOREIGN KEY (sNum) REFERENCES Staff(sNum);
ALTER TABLE Attendant ADD FOREIGN KEY (jID) REFERENCES Staff(jID);
For me this is the one it fails on first
ALTER TABLE Passenger ADD FOREIGN KEY (fDate) REFERENCES Flights(fDate);
Try adding an index on Flights.fdate first, then doing the foreign key and doing that for additional references until it works. Let me know if that works for you, it did for me.
By the way you might want to reconsider your schema there. Joins and relations should really be done on integers only. That schema will bog down fast as it grows. Also, and maybe this is just personal preference but I prefer first_name to fName. Easier to read for other developers, this isn't 1986, we can have human readbable names now :-)

How to build database table structure for customer service products in mysql 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 6 years ago.
Improve this question
I am building a cable customer project database structure in this project customers(subscribers) use service products like (settopbox, modem, etc 3 or 4 service products with different attributes). Below is my database table structure :-
Customer Table
customer_id,name,address, phone_no, etc
customer_settopbox table
customer_modem table
packages table
my question is :-
1. Is it good to build separate table for each device and allot to customer.
or any other method is there for this. because each device has their own attributes like settopbox has VCNO, SerialNo etc modem has other attributes.
I will be vary thankful.
Add all common product fields to product table.
All custom fileds can be placed to product_custom_attribute table.
you can use structure like this :
CREATE TABLE `customer` (
`id` INT NOT NULL AUTO_INCREMENT,
`name` VARCHAR(45) NOT NULL,
`address` VARCHAR(255) NULL,
`phone_no` VARCHAR(10) NULL,
PRIMARY KEY (`id`)
);
CREATE TABLE `product_type` (
`id` INT NOT NULL AUTO_INCREMENT,
`name` VARCHAR(45) NOT NULL,
PRIMARY KEY (`id`)
);
CREATE TABLE `product` (
`id` INT(11) NOT NULL AUTO_INCREMENT,
`type` INT(11) NOT NULL,
`name` VARCHAR(45) NOT NULL,
`description` VARCHAR(255) DEFAULT NULL,
`customer_id` INT(11) DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `FK product type product_type id_idx` (`type`),
KEY `FK product customer_id customer id_idx` (`customer_id`),
CONSTRAINT `FK product customer_id customer id` FOREIGN KEY (`customer_id`)
REFERENCES `customer` (`id`)
ON DELETE CASCADE ON UPDATE NO ACTION,
CONSTRAINT `FK product type product_type id` FOREIGN KEY (`type`)
REFERENCES `product_type` (`id`)
ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=INNODB DEFAULT CHARSET=UTF8;
CREATE TABLE `product_custom_attribute` (
`id` INT NOT NULL AUTO_INCREMENT,
`product_id` INT NOT NULL,
`name` VARCHAR(45) NOT NULL,
`value` VARCHAR(45) NOT NULL,
PRIMARY KEY (`id`),
INDEX `FK product_custom_attribute product_id product id_idx` (`product_id` ASC),
CONSTRAINT `FK product_custom_attribute product_id product id` FOREIGN KEY (`product_id`)
REFERENCES `product` (`id`)
ON DELETE CASCADE ON UPDATE NO ACTION
);
If you do not familiar with FOREIGN KEYs use this one
CREATE TABLE `customer` (
`id` INT NOT NULL AUTO_INCREMENT,
`name` VARCHAR(45) NOT NULL,
`address` VARCHAR(255) NULL,
`phone_no` VARCHAR(10) NULL,
PRIMARY KEY (`id`));
CREATE TABLE `product` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`type` int(11) NOT NULL,
`name` varchar(45) NOT NULL,
`customer_id` int(11) DEFAULT NULL,
`description` varchar(255) DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `FK product type product_type id_idx` (`type`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE `product_custom_attribute` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`product_id` int(11) NOT NULL,
`name` varchar(45) NOT NULL,
`value` varchar(45) NOT NULL,
PRIMARY KEY (`id`),
KEY `FK product_custom_attribute product_id product id_idx` (`product_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE `product_type` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(45) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
Don't build a table for each device. Instead, you can have a table that is called "device attributes" that has the attributes for each device. it will be easier to refer to that table and take the attributes for each device. In the customer's table, you can refer to a device.
It very much depends on how you see the future. If there is a know number of products which will NEVER be expanded, you can have additional columns within the customer's table. If, on the other hand, the list of products is prone to change in the future, having a separate table with (essentially) 3 columns:
- Customer ID (reference to the customer's table record)
- Product type
- Product status
This will give you all the needed flexibility while keeping things simple.

Yii2-Integrity constraint violation – yii\db\IntegrityException

Integrity constraint violation – yii\db\IntegrityException
SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (`jts`.`employee`, CONSTRAINT `employee_ibfk_1` FOREIGN KEY (`client_code`) REFERENCES `client` (`client_code`))
The SQL being executed was: INSERT INTO `employee` (`client_code`, `company_name`, `emp_email`, `emp_mobile`, `emp_first_name`, `emp_last_name`) VALUES ('12345678', 'PVPPCOE', 'saurabhkulkarni2010#hotmail.com', '+449029792183', 'Saurabh', 'Kulkarni')
Error Info: Array
(
[0] => 23000
[1] => 1452
[2] => Cannot add or update a child row: a foreign key constraint fails (`jts`.`employee`, CONSTRAINT `employee_ibfk_1` FOREIGN KEY (`client_code`) REFERENCES `client` (`client_code`))
)
I have three tables Client, Employee and create_client, out of which client and employee has two foreign keys.
This problem is showing when I try to insert data from create_client to employee which has exact same field.
What should I do so that I can insert two tables at one i.e create_client and employee
UPDATE-
Table Structure
1)client
CREATE TABLE IF NOT EXISTS `client` (
`id` int(11) NOT NULL,
`client_code` varchar(11) NOT NULL,
`company_name` varchar(45) NOT NULL
) ENGINE=InnoDB AUTO_INCREMENT=29 DEFAULT CHARSET=utf8;
2)create_client
CREATE TABLE IF NOT EXISTS `create_client` (
`id` int(11) NOT NULL,
`client_code` varchar(11) NOT NULL,
`company_name` varchar(45) NOT NULL,
`emp_email` varchar(45) NOT NULL,
`emp_mobile` varchar(45) NOT NULL,
`emp_first_name` varchar(45) NOT NULL,
`emp_last_name` varchar(45) NOT NULL
) ENGINE=InnoDB AUTO_INCREMENT=43 DEFAULT CHARSET=utf8;
3)employee
CREATE TABLE IF NOT EXISTS `employee` (
`id` int(11) NOT NULL,
`client_code` varchar(11) NOT NULL,
`company_name` varchar(45) NOT NULL,
`emp_email` varchar(45) NOT NULL,
`emp_mobile` varchar(45) NOT NULL,
`emp_first_name` varchar(45) NOT NULL,
`emp_last_name` varchar(45) NOT NULL
) ENGINE=InnoDB AUTO_INCREMENT=20 DEFAULT CHARSET=utf8;
This is my table structure, first user will create client that means first it will update create_client. Now if user wants he can add many employee under one client code now user will update employee table from Yii2 dynamic form widget.
For using this widget I have created one table call client this will store only client_code and company name remaining data will go in employee table e.g emp_mobile,emp_email, emp_first_name, emp_last_name.
Now this problem is pop up when user first enters data into create_client table.
everything is working between client and employee table user able to enter as many employee as it wants using Yii2 dynamic form Widget but not working for create_client
Apparently you also have a foreign key in the employee table referencing the client_code of the client table, so you can only use client_code values that already exist in the client table.
I don't know what the structure of the create_client table looks like and what its purpose is, but based on your info I assume that you should first insert the data in client, then in employee and finally in create_client, so that every foreign key's value exists in its respective table.
If this is not correct, please post your table structures and the data or queries that could be causing this.
Edit after your comment: I assume that your table structure looks like this:
CREATE TABLE `create_client` (
`client_code` INT UNSIGNED NOT NULL,
`company_name` VARCHAR(255) NOT NULL,
`emp_mobile` VARCHAR(255) NOT NULL,
`emp_email` VARCHAR(255) NOT NULL,
`emp_first_name` VARCHAR(255) NOT NULL,
`emp_last_name` VARCHAR(255) NOT NULL,
UNIQUE KEY `client_code` (`client_code`)
) ENGINE=INNODB DEFAULT CHARSET=utf8;
CREATE TABLE `client` (
`client_code` INT UNSIGNED NOT NULL AUTO_INCREMENT,
`company_name` VARCHAR(255) NOT NULL,
PRIMARY KEY `client_code` (`client_code`),
CONSTRAINT `client_ibfk_1` FOREIGN KEY (`client_code`) REFERENCES `create_client` (`client_code`)
) ENGINE=INNODB DEFAULT CHARSET=utf8;
CREATE TABLE `employee` (
`client_code` INT UNSIGNED NOT NULL,
`company_name` VARCHAR(255) NOT NULL,
`emp_mobile` VARCHAR(255) NOT NULL,
`emp_email` VARCHAR(255) NOT NULL,
`emp_first_name` VARCHAR(255) NOT NULL,
`emp_last_name` VARCHAR(255) NOT NULL,
KEY `client_code` (`client_code`),
CONSTRAINT `employee_ibfk_1` FOREIGN KEY (`client_code`) REFERENCES `client` (`client_code`)
) ENGINE=INNODB DEFAULT CHARSET=utf8;
However, I would suggest to use client as the parent table and move the foreign key constraint to create_client, so that both employee and create_client have a foreign key of client. I would also like to normalise this and get rid of company_name in the child tables:
CREATE TABLE `client` (
`client_code` INT UNSIGNED NOT NULL AUTO_INCREMENT,
`company_name` VARCHAR(255) NOT NULL,
PRIMARY KEY `client_code` (`client_code`)
) ENGINE=INNODB DEFAULT CHARSET=utf8;
CREATE TABLE `create_client` (
`client_code` INT UNSIGNED NOT NULL,
`emp_mobile` VARCHAR(255) NOT NULL,
`emp_email` VARCHAR(255) NOT NULL,
`emp_first_name` VARCHAR(255) NOT NULL,
`emp_last_name` VARCHAR(255) NOT NULL,
UNIQUE KEY `client_code` (`client_code`),
CONSTRAINT `create_client_ibfk_1` FOREIGN KEY (`client_code`) REFERENCES `client` (`client_code`)
) ENGINE=INNODB DEFAULT CHARSET=utf8;
CREATE TABLE `employee` (
`client_code` INT UNSIGNED NOT NULL,
`emp_mobile` VARCHAR(255) NOT NULL,
`emp_email` VARCHAR(255) NOT NULL,
`emp_first_name` VARCHAR(255) NOT NULL,
`emp_last_name` VARCHAR(255) NOT NULL,
KEY `client_code` (`client_code`),
CONSTRAINT `employee_ibfk_1` FOREIGN KEY (`client_code`) REFERENCES `client` (`client_code`)
) ENGINE=INNODB DEFAULT CHARSET=utf8;
The next improvement would be to use the employee table for both unique and non-unique client_code inserts. Instead of using 2 identical tables, apart from the unique key, you could do the unique validation in Yii instead of in MySQL. You can specify the table name in the tableName() method of the CreateClient model . This will remain:
CREATE TABLE `client` (
`client_code` INT UNSIGNED NOT NULL AUTO_INCREMENT,
`company_name` VARCHAR(255) NOT NULL,
PRIMARY KEY `client_code` (`client_code`)
) ENGINE=INNODB DEFAULT CHARSET=utf8;
CREATE TABLE `employee` (
`client_code` INT UNSIGNED NOT NULL,
`emp_mobile` VARCHAR(255) NOT NULL,
`emp_email` VARCHAR(255) NOT NULL,
`emp_first_name` VARCHAR(255) NOT NULL,
`emp_last_name` VARCHAR(255) NOT NULL,
KEY `client_code` (`client_code`),
CONSTRAINT `employee_ibfk_1` FOREIGN KEY (`client_code`) REFERENCES `client` (`client_code`)
) ENGINE=INNODB DEFAULT CHARSET=utf8;
This should allow you to use the dynamic form, with Client as the main model and Employee as the items model.
On Yii2 using IO Generator (Model) and MySQL I have found the following.
I have seen SET '' instead of SET NULL in update SQL statement(s) on field(s) with foreign key constraints that have caused a "Integrity constraint violation".
My "fix" is to add a default rule on the foreign key field in the model, like so:
public function rules()
{
return [
//
// your other rules
//
[['field_name_with_foreign_key'], 'default', 'value' => null],
];
}
Hope this helps someone.
remove all foreign key checks and simplify database.
the problem lies in your foreign key structure.
learn carefully about primary keys and foreign keys.
make new database again without foreign keys then check.

MySQL: Complete missing data with parent tables

Let's assume I have three tables in my database, representing a hierarchic order:
countries, states, and cities
Each city is connected to one state, each state to one country. This is simple to represent in a database.
Let's further assume each of those tables contains a field tax_rate. In a basic case the tax rate is defined on country level and null on all other levels. However, it could be overwritten on any of the levels below.
When I query for a city node I would like to get its tax rate. It could be defined right within the same node, but more likely it will be defined on any of the next-higher levels.
What is the most efficient way to accomplish this either in MySQL or on PHP level? In my real life application there will not be only one such field but many of them.
Below is a simple database schema of my example. Of course it would also have foreign key definitions.
CREATE TABLE `countries` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`tax_rate` float(4,2) DEFAULT NULL,
`name` varchar(20) DEFAULT NULL,
PRIMARY KEY (`id`)
);
INSERT INTO `countries` (`id`, `tax_rate`, `name`)
VALUES
(1,8.00,'Switzerland'),
(2,16.00,'Germany');
CREATE TABLE `cities` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`state_id` int(11) DEFAULT NULL,
`tax_rate` float(4,2) DEFAULT NULL,
`name` varchar(20) DEFAULT NULL,
PRIMARY KEY (`id`)
)
NSERT INTO `cities` (`id`, `state_id`, `tax_rate`, `name`)
VALUES
(1,1,NULL,'Bern'),
(2,1,NULL,'Zollikofen'),
(3,2,NULL,'Zurich'),
(4,2,5.30,'Wettingen'),
(5,2,NULL,'Winterthur'),
(6,2,6.60,'Handligen'),
(7,3,NULL,'Bayern-Town 1'),
(8,3,NULL,'Bayern-Town 2'),
(9,3,9.00,'Bayern-Town 3');
CREATE TABLE `states` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`country_id` int(11) DEFAULT NULL,
`tax_rate` float(4,2) DEFAULT NULL,
`name` varchar(20) DEFAULT NULL,
PRIMARY KEY (`id`)
);
INSERT INTO `states` (`id`, `country_id`, `tax_rate`, `name`)
VALUES
(1,1,NULL,'Bern'),
(2,1,9.00,'Zurich'),
(3,2,NULL,'Bavaria');
Use COALESCE(). That's what it's for.
This could be handled on either level: MySQL or PHP
I'd prefer the MySQL approach:
select cities.name, COALESCE(cities.tax_rate,states.tax_rate,countries.tax_rate) from cities
join states on cities.state_id=states.id
join countries on states.country_id = countries.id
This will return the city's tax rate if it is not NULL, else the state's. If that also is null, it'll return the country's tax rate.

How do i serlialize the product using php?

i am building a real estate application where in it will store the properties and search it. the property will have different categories like (residential, commercial, industrial or agricultural). based upon the category i want to serailize each and every property listing . for example the property with id 1 belongs to resedential will have the serial code rs_SOMERANDOMUNIQUENUMBER. and for commercial it can be cm_SOMERANDOMUNIQUENUMBER and so on. for this my database table looks like this.
CREATE TABLE IF NOT EXISTS `propSerials` (
`id` bigint(20) NOT NULL auto_increment,
`serial` varchar(50) NOT NULL,
`property_id` int(10) UNIQUE NOT NULL,
PRIMARY KEY  (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
what would be the best possible format to store the serial with the prefix according to category?
thank you
Why dont you add another column that holds category_id and in category table add column with prefixes for that category.
CREATE TABLE IF NOT EXISTS `propSerials` (
`id` bigint(20) NOT NULL auto_increment,
`serial` varchar(50) NOT NULL,
`property_id` int(10) UNIQUE NOT NULL,
`category_id` int(10) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE IF NOT EXISTS `propCategories` (
`id` bigint(20) NOT NULL auto_increment,
`category` varchar(50) NOT NULL,
`property_prefix` char(3) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
In query you can:
SELECT CONCAT('prefix_', 'serial');

Categories