PHP User Registration Form mysql [closed]

-1

I just implemented this form ( Form Page ) on my site, but I could not connect to the database to save the records . I used mysql_connect and it gives this error:

  

Ichangedtomysqliandstillgaveerror.

Editing...

[email protected],itworkedbutitdoesnotconnecttothetable,Iused<?phprequire("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 !!

    
asked by anonymous 15.11.2016 / 23:10

3 answers

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 port, it would look like this ...

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

If you want to work object oriented uses the PDO guy, 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 variable $ string you can use it as well ...

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

If you want a hint, create a file "class.conexao.php" 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 the case validates login, then you can create for what you need ...

this part

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

is to prepare the query that you are going to execute, and then execute and return the associated results ....

You 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;
        }
    }

?>

In this way, you are working on object-oriented.

    
16.11.2016 / 00:35
4

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

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

error_reporting (E_ALL & ~ E_NOTICE & ~ E_DEPRECATED);

or you can suppress the message by using the @ before function.

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

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

The mysql extension is deprecated, in its place you should use the mysqli extinction, it is practically the same at the time of use, and in one the functions are mysql_ * and the other mysqli_ *. Although they have function names very similar 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 the use of mysql still somewhere . If you still have the error, please post the code.

Convert a link from MYSQL to MYSQLI?

    
16.11.2016 / 02:23
3

I use the following code to connect:

<?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*/

?>

In the insert 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");
    
16.11.2016 / 02:15