Insertion in Mysql with PHP works for nothing

Asked

Viewed 266 times

1

I am trying to create a registration page and save user data in Mysql, but it is impossible to do this. I’ve searched 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 I run the code echos print normally and below appears only Erro:, does not print. Which error is occurring?

  • 1

    I reopened the question, considering that those who closed should not have noticed the presence of mysql_ and mysqli_ at the same time in the code.

2 answers

8


If you copied and pasted the error is to open the connection with mysql_connect and then use the methods of class mysqli.

When you use, for example, mysqli_query(), is calling a method of class mysqli in a way that php calls "procedural". This way is the same as writing mysqli::query().

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

In the mysqli connection class is 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’t switch between one and the other.

  • I only used mysqli and it worked. Thanks for the clarification Manuel!

  • 2

    About choosing mysql, mysqli or PDO, here’s some information that might help: https://php.net/manual/en/mysqlinfo.api.choosing.php

0

Try changing the following line

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

for this

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

I think this way the error already appears and it’s easier to understand what’s wrong.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.