How to avoid multiple inserts in the database

3

Hello, I 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 subcategories if I update the browser.

Since I can not use the Unique in the subcategory table in the name field, this would make it impossible for me to use the same name if it is affiliated with another category.

Below are the two tables.

Category 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 ;
    
asked by anonymous 21.07.2015 / 13:54

2 answers

2
  • You can use access tokens.
  • the 1 and 2 page should have session_start (); at the beginning and pass a token with a random number generated or a randomly generated code, making it impossible to access the page again without going to 1 page!
  • The 1 page and where the user ve adds things to the cart (eg "on page 2 where the process adds to the database!
  • How to generate the token on 1 page?

    <?php
    session_start();
    function geraToken()
    {
    return rand(1, 1000)."".rand(1, 1000); // Gerar numeros aleatorios
    }
    $_SESSION['token'] = geraToken();
    ?>
    

    How to use the token on page 2?

    <?php
    session_start();
    if(!isset($_SESSION['token']))
    {
        die("Acesso negado!");
    }
    //Adicione aki o resto da 2 pagina...
    //...
    
    // NO fim do processo adicione isto
    $_SESSION['token'] = ""; // Fazendo a token invalida e impedindo o refresh da pagina!
    ?>
    
        
    21.07.2015 / 14:11
    3

    In the Unique Key function, you can put as many fields as you want to keep in the table, the line is Única

    Example with more than one field

    UNIQUE KEY 'nomeIdUnique' ('nome','cat')
    

    In your table

    CREATE TABLE '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'),
      UNIQUE KEY 'nomeIdUnique' ('nome','cat'),
      KEY 'colunasIndexadas' ('id','cat','categoria_url')
    ) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
    

    In this way it will never be repeated, and if you are using PDO, you do not need to send the error to the log file

    try {
    
    } catch (PDOException $e) {
        if ($e->getCode !== 23000) {
            //log
        }
    }
    
        
    21.07.2015 / 16:20