Insertion in MySQL with PHP does not work for nothing

1

I'm trying to create a registration page and save the user's data in MySQL, but it's impossible to do that. I already researched the internet, but apparently my code is correct.

SQL code:

    CREATE SCHEMA IF NOT EXISTS site;

CREATE TABLE IF NOT EXISTS site.usuario (
  'id' int(11) NOT NULL AUTO_INCREMENT,
  'nome' varchar(255) CHARACTER SET utf8 NOT NULL,
  'email' varchar(255) CHARACTER SET utf8 NOT NULL,
  'senha' varchar(255) CHARACTER SET utf8 NOT NULL,
  'metodoPagamento' varchar(255) CHARACTER SET utf8 NOT NULL,
  'emailPagamento' varchar(255) CHARACTER SET utf8 NOT NULL,
  'tituloSite' varchar(255) CHARACTER SET utf8 NOT NULL,
  'linkSite' varchar(255) CHARACTER SET utf8 NOT NULL,
  'idiomaSite' varchar(255) CHARACTER SET utf8 NOT NULL,
  'permiteContato' tinyint(1) NOT NULL,
  'usuarioCima' int(11) NOT NULL,
  PRIMARY KEY ('id'),
  UNIQUE KEY 'email' ('email')
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;

PHP Code:

<?php
      // estabelece conexão com o banco de dados
      $conexao = mysql_connect('localhost', 'root', 'senha', 'site');

      // define variáveis e atribui valor vazio a todas
      $nome = $email = $senha = $metodoPagamento = $emailPagamento = $tituloSite = "";
      $linkSite = $idiomaSite = $permiteContato = $promo = "";

      function testaEntrada($dados)
      {
        $dados = trim($dados);
        $dados = stripslashes($dados);
        $dados = htmlspecialchars($dados);
        return $dados;
      }

      if ($_SERVER["REQUEST_METHOD"] == "POST") 
      {
        $nome = testaEntrada($_POST['nome']);
        echo("Nome = ".$nome."<br />");
        $email = testaEntrada($_POST['email']);
        echo("Email = ".$email."<br />");
        $senha = testaEntrada($_POST['senha']);
        echo("Senha = ".$senha."<br />");
        $metodoPagamento = testaEntrada($_POST['metodoPagamento']);
        echo("Metodo de Pagamento = ".$metodoPagamento."<br />");
        $emailPagamento = testaEntrada($_POST['emailPagamento']);
        echo("Email pagamento = ".$emailPagamento."<br />");
        $tituloSite = testaEntrada($_POST['tituloSite']);
        echo("Título do site = ".$tituloSite."<br />");
        $linkSite = testaEntrada($_POST['linkSite']);
        echo("Link do site = ".$linkSite."<br />");
        $idiomaSite = testaEntrada($_POST['idiomaSite']);
      }
      if($idiomaSite == 'Outro')
      {
        $idiomaSite = testaEntrada($_POST['outroIdioma']);
      }
      echo("Idioma do site = ".$idiomaSite."<br />");
      if (testaEntrada($_POST['permiteContato']) == 'sim') 
      {
        $permiteContato = 1;
      }
      else
      {
        $permiteContato = 0;
      }
      $promo = 1;
      $promo = (int) $_GET['promo'];
      if($promo == "" || $promo == 0) 
      {
        $promo = 1; 
      }
      echo("Permite o usuário ".$promo." entrar em contato comigo? ");
      if ($permiteContato == 1) 
      {
        echo "SIM<br />";
      }
      else
      {
        echo "NÃO<br />";
      }
      if (mysqli_connect_errno())
      {
        echo "A conexão com o banco de dados falhou: " . mysqli_connect_error();
        mysqli_close($conexao);
      }
      else
      {
        $sql = "INSERT INTO usuario(nome, email, senha, metodoPagamento, emailPagamento, tituloSite, linkSite, idiomaSite, permiteContato, usuarioCima)
              VALUES ('$nome', '$email', '$senha', '$metodoPagamento', '$emailPagamento', '$tituloSite', '$linkSite', '$idiomaSite', $permiteContato, $promo)";
        if (mysqli_query($conexao,$sql)) 
        {
          echo ($nome . " você foi cadastrado com sucesso! Enviamos um email de confirmação para " . $email);
        }
        else
        {
          die('Erro: ' . mysql_error($conexao));
        }
      }
      mysqli_close($conexao);
    ?>

When you run the code, the% s of% s print normally and below only echo does not print. What error is occurring?

    
asked by anonymous 03.10.2014 / 07:13

2 answers

8

If you copied and pasted the error is in opening the connection with mysql_connect and then using the methods of class mysqli.

When you use mysql i _query () , you are calling a mysqli method in a way that php calls "procedural." This form is the same as writing mysqli :: query ().

Before this class was available, access to the database was done through functions rather than object methods. In this case the mysql_connect () function is used to open a connection to the database.

In the mysqli class connection is then opened by the class constructor. The mysqli form is just an alias (another way to call this constructor).

In short: you have to decide whether to use mysql or mysqli . You can not switch between them.

    
03.10.2014 / 12:31
0

Try changing the following line

die('Erro: ' . mysql_error($conexao));

For this

die('Erro: ' . mysqli_error($conexao));

I think the error already appears and it's easier to see what's wrong.

    
03.10.2014 / 10:35