PHP mysql User Registration Form

Asked

Viewed 1,062 times

-1

I just implemented this form (Form page) on my website, but I could not connect to the database to save the records. I used the mysql_connect and she gives that mistake:

erro

changed to mysqli and still gave error.

Someone would have as it shows the code that I can use?

Editing...

I used the code suggested by @Ray BM on the config.php page, it worked but it does not connect to the table, I used the <?php require("config.php"); ?> on the form page.

<?php
$servidor = "50.116.87.53"; /*ip da maquina a qual o banco de dados está*/
$usuario = "***"; /*usuario do banco de dados MySql*/
$senha = "***"; /*senha do banco de dados MySql*/
$banco = "***_registo"; /*seleciona o banco a ser usado*/

$conexao = mysqli_connect($servidor,$usuario,$senha);  /*Conecta no bando de dados*/
if (!$conexao){
   die('Não foi possível conectar: '. mysql_error());
   }

mysqli_select_db($conexao,$banco); /*seleciona o banco a ser usado*/

?>

how can I save the records if I have no connection to the table?

can take a look at the registration page through the link above. Thank you !!

  • 2

    What mistake you made when you changed ?

  • could you pass on more details please ?

  • updated question with errors

  • Never use text prints that you can copy, please.

  • You said you switched to mysqli and still gives the error? What mistake, the same mistake? It would not make sense, if it is a new error explain what it is. Do not just go out changing something else, follow the examples of the documentation https://php.net/manual/en/book.mysqli.php, sometimes you should redo everything, no use copying, learn how it works, if it is not the same that you want to drive a car without knowing when to pass the march and take the road without knowing to change a "steppe". You’ll only get headaches

  • Hello William, have you come to criticize or try to help? as you can see in my question, there is an issue and just below is described the new problem I found.

Show 1 more comment

3 answers

3

Actually this is not an error but a warning about using the mysql_connect function().

To remove this warning put at the beginning of the connection code the following line:

error_reporting (E_ALL & ~ E_NOTICE & ~ E_DEPRECATED);

or you can delete the message using @ before the function.

That is, use @mysql_connect(...);

But deleting errors with @ is always a bad practice and should be avoided.

The mysql extension is obsolete, instead you should use the mysqli extinction, it is practically the same at the time of use, being that in one the functions are mysql_* and in the other mysqli_*. Although they have very similar function names to make it easy to migrate from one to the other, internally mysqli_ is much better.

Review your code, probably after the change the message persisted due to mysql usage still somewhere. If you still have the error, post the code.

How to convert a MYSQL connection to MYSQLI?

2


The direct connection in mysqli I do so...

$mysqli = new mysqli("localhost", "usuario", "senha", "bancodedados");
if ($mysqli->connect_errno) {
    echo "Falha ao conectar com o mysql: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;
}
echo $mysqli->host_info . "\n";

if you need to send the connection door, it would look like this...

 $mysqli = new mysqli("localhost", "usuario", "senha", "bancodedados", 3306);

If you want to work object-oriented you use the expensive PDO, I believe it will help you a lot more, and it is much easier to work object-oriented with it...

Below the class I have here that makes this connection, and an example of another file using this connection...

   <?php 
    class Conexao { 
        public $conn; 
        function Conexao() { 
            $string  = "mysql:";
            $string .= "host=localhost ";
            $string .= "port=5432 "; 
            $string .= "dbname=nomedobanco";
            $string .= "user=usuario";
            $string .= "password=senha"; 

            try { 
                $this->conn = new PDO($string);
            }catch(PDOException $e){ 
                echo $e->getMessage();
            }

        }

    }





?> 

if you do not want to use the new PDO with the $string variable you can use it as well...

new PDO('mysql:host=localhost;dbname=testdb;charset=utf8mb4', 'username', 'password');

If you want a hint, create a "class.conexao.php" file with this code and in "class.suaclasse.php", you use, as in the example below...

<?php 

    require('class.conexao.php');

    class Querys extends Conexao { 


        function validaLogin($login, $senha) { 

            $sql = 
                    "
                        select nome, cpf from validaUsuario('".$login."', '".$senha."');
                    ";

            $stmt = $this->conn->prepare($sql); 
            $stmt->execute();
            $result = $stmt->fetchAll(PDO::FETCH_ASSOC); 
            return $result; 

        }


}

this function in case validates login, then you can create for what Voce needs...

that part

$stmt = $this->conn->prepare($sql); 
        $stmt->execute();
        $result = $stmt->fetchAll(PDO::FETCH_ASSOC); 
        return $result;

serves to prepare the query that Voce will execute, and then run and return the associated results....

Voce would use the return in this way..

<?php   
    require_once('class.querys.php');

    class Login extends Querys { 

        function validarLogin() { 

                $query = new Querys(); 

                $retorno = $query->validaLogin(aqui vem os parametros)); 

                return $retorno;
        }
    }

?>

so there you are working object-oriented.

  • it is difficult because I do not understand so much of mysql, I still do not understand how I will save the User, password and Emal of the registration form, in the table of mysql. thank you

  • The connection is OK now then?

  • Let’s assume you used the $mysqli connection I mentioned above...

  • 1

    To register for example, you must use this variable that has the connection. It would look like $mysqli->query("Insert into tabelausuario (user,password,email) values (its variables with the values of the form)")

  • In this case you will enter the data in the user table, then just exchange the table name for yours and put the data...

  • the connection is ok, I used the code I posted

  • how so "(its variables with the form values)"? the values are not the users who will put?

  • in the case tabelausuario would be the table inside the database? user, password and email are the table fields or the name form imput"user,..."?

  • That guy, what I meant by the "(its variables)" is the values that Voce will receive from the form.

Show 4 more comments

2

I use the following code for connection:

<?php
$servidor = "0.0.0.0"; /*ip da maquina a qual o banco de dados está*/
$usuario = "usuario"; /*usuario do banco de dados MySql*/
$senha = "senha"; /*senha do banco de dados MySql*/
$banco = "nomedoSchema"; /*seleciona o banco a ser usado*/

$conexao = mysqli_connect($servidor,$usuario,$senha);  /*Conecta no bando de dados*/
if (!$conexao){
   die('Não foi possível conectar: '. mysql_error());
   }

mysqli_select_db($conexao,$banco); /*seleciona o banco a ser usado*/

?>

When inserting I use:

require("autenticacao/conexao.php");

 $sql = "INSERT INTO tabela (campo) VALUES (valor)"; 
 $resultado = mysqli_query($conexao,$sql) or die("ocorreu um erro e seu registro não foi inserido");
  • how can I connect to the table? to save the fields.

Browser other questions tagged

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