3
Ola would like to know how I can use php to avoid adding the same item to a database on a save page that adds the names of the subcategories if you update your browser.
Since I cannot use in the database Unique in the table subcategory in the name field because it would make it impossible for me to use the same name if it is affiliated to another category.
Below are the two tables.
Table Category
CREATE TABLE IF NOT EXISTS `categoria` (
`id` int(255) NOT NULL AUTO_INCREMENT,
`categoria_url` char(255) COLLATE utf8_unicode_ci NOT NULL,
`nome` char(255) COLLATE utf8_unicode_ci NOT NULL,
`modo` enum('UNICO','MULTIPLO') COLLATE utf8_unicode_ci NOT NULL DEFAULT 'UNICO',
`data` date NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `categoriaUnica` (`categoria_url`),
UNIQUE KEY `nomeUnico` (`nome`),
KEY `colunasIndexadas` (`id`,`categoria_url`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=1 ;
Table subcategory
CREATE TABLE IF NOT EXISTS `subcategoria` (
`id` int(255) NOT NULL AUTO_INCREMENT,
`categoria_url` char(255) COLLATE utf8_unicode_ci NOT NULL,
`subcategoria_url` char(255) COLLATE utf8_unicode_ci NOT NULL,
`temporada` int(3) NOT NULL,
`nome` char(255) COLLATE utf8_unicode_ci NOT NULL,
`cat` int(255) NOT NULL,
`semana` enum('Selecionar Semana','Domingo','Segunda-Feira','Terca-Feira','Quarta-Feira','Quinta-Feira','Sexta-Feira','Sábado') COLLATE utf8_unicode_ci NOT NULL DEFAULT 'Selecionar Semana',
`ativadorOn` enum('ON','OFF') COLLATE utf8_unicode_ci NOT NULL DEFAULT 'OFF',
`sinopse` text COLLATE utf8_unicode_ci NOT NULL,
`status` enum('Completo','Incompleto','Andamento','Pausado','Lançamento') COLLATE utf8_unicode_ci NOT NULL DEFAULT 'Andamento',
`genero` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`genero_url` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`numeroMedias` int(255) NOT NULL DEFAULT '0',
`autor` char(20) COLLATE utf8_unicode_ci NOT NULL,
`acessos` int(255) NOT NULL,
`arquivo_nome` varchar(355) COLLATE utf8_unicode_ci NOT NULL,
`arquivo_tipo` varchar(100) COLLATE utf8_unicode_ci NOT NULL,
`arquivo_data_cad` date NOT NULL,
`arquivo_hora_cad` time NOT NULL,
PRIMARY KEY (`id`),
KEY `colunasIndexadas` (`id`,`cat`,`categoria_url`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=1 ;
It seems to me you have a database normalization problem. If you change the name of a subcategory or its url for example, you would need to update the entire category table with the new data. In this case we would have a relationship 1 to N. If you want to use a category aligned to more than one scrap, that is a relation N to N, you would need an auxiliary table to solve this situation by grouping the id of the category table and the id of the subcategory table. So you would avoid duplicity if there is already a match even if you exit the application and return
– jefissu
@Rodrigo posts the code of his pages, the registration and which inserts the data in the bank.
– KaduAmaral
Do not rush to accept the first answer as absolute, it is not always the solution to your problem, the correct is to put as Unique the ( id of the category + name subcategory ), and treat the error "23000" example: UNIQUE INDEX
nome_da_unique
(cat
,nome
)– Williams