PHP Insert error

Asked

Viewed 313 times

1

I am having a problem when entering data in the database, I have already checked if there is any field where it is not accepting null value, however everything is ok, when appearing the error is informed the following code:

Array ( [type] => 8192 [message] => mysql_connect(): The mysql Extension is deprecated and will be Removed in the Future: use mysqli or PDO Instead [file] => /var/www/html/jribeirocomunicacoes.com.br/web/admin/includes/conexao.php [line] => 7 )

My code is as follows:.

<?php

// incluindo o arquivo que faz a conexao com o banco
include ("../includes/conexao.php");
include ("../includes/suc_validacao.php");
include ("../includes/suc.php");

$nome = $_POST['TXT_NOMEX_CLIEN'];
$mensagem = $_POST['MEM_APRES_CLIEN'];
$naturalidade = $_POST['TXT_NATUR_CLIEN'];
$nacionalidade = $_POST['TXT_NACIO_CLIEN'];
$dtnasc = $_POST['DAT_NASCI_CLIEN'];
$dtnasc = implode("-", array_reverse(explode("/", $dtnasc)));
$ocupacao = $_POST['TXT_OCUPA_ATUAL'];
$clube = $_POST['TXT_NOMEX_CLUBE'];
$desde = $_POST['TXT_DATAX_ADMIS'];
$desde = implode("-", array_reverse(explode("/", $desde)));
$altura = $_POST['TXT_ALTUR_CLIEN'];
$altura = str_replace(",", ".", $altura);
$peso = $_POST['TXT_PESOX_CLIEN'];
$peso = str_replace(",", ".", $peso);
$gostede = $_POST['TXT_GOSTO_CLIEN'];
$naogostade = $_POST['TXT_NGOST_CLIEN'];
$twitter = $_POST['TXT_ENDER_TWITR'];
$facebook = $_POST['TXT_ENDER_FACEB'];
$youtube = $_POST['TXT_ENDER_YOUTB'];
$menuvinc = $_POST['P_COD_IDENT_MENUX'];
$usurLoga = $_SESSION['UsuarioID'];

$query = "INSERT INTO tbl_CLIENTES (COD_IDENT_MENUX, TXT_NOMEX_CLIEN, MEM_APRES_CLIEN, FLG_TIPOX_CLIEN, COD_IDULT_ATUAL, DAT_ULTIM_ATUAL) VALUES";
$query .= "('$menuvinc', '$nome','$mensagem','F', '$usurLoga', now())";

$inserir = mysql_query($query)
        or die("teste");

$COD_IDENT_ULTIM_CLIEN = mysql_insert_id();

$query2 = "INSERT INTO tbl_CLIENTES_PF (COD_IDENT_CLIEN, TXT_NATUR_CLIEN, TXT_NACIO_CLIEN, DAT_NASCI_CLIEN, TXT_OCUPA_ATUAL, TXT_NOMEX_CLUBE, TXT_ALTUR_CLIEN, TXT_PESOX_CLIEN, TXT_ENDER_TWITR, TXT_ENDER_FACEB, TXT_ENDER_YOUTB, TXT_DATAX_ADMIS, TXT_GOSTO_CLIEN, TXT_NGOST_CLIEN, COD_IDULT_ATUAL, DAT_ULTIM_ATUAL) VALUES";
$query2 .= "('$COD_IDENT_ULTIM_CLIEN','$naturalidade','$nacionalidade','$dtnasc','$ocupacao','$clube','$altura','$peso','$twitter','$facebook','$youtube','$desde','$gostede','$naogostade', '$usurLoga', now())";

//executando a query
$inserir = mysql_query($query2)
        or print_r(error_get_last()); die;

$response = array("success" => true);

//fechando a conexao com o banco
mysql_close($conn);

header("Location: listaClientes.php");
exit; // Redireciona o visitante
?>

The error happens in the second die.

Here is my document that makes the connection with the bank, in case the error is giving to warn that this document is obsolete, as it would be this same document passing for PDO or mysqli ?

<?php

$dbUser = 'dbUSer';
$dbPassword = 'dbPassword';
$dbSite = 'jrcomunicacoes';

$conn = mysql_connect("jrcomunicacoes.mysql.uhserver.com", $dbUser, $dbPassword); // or die ("[HTM]Problema ao conectar ao MYSQL[/HTM]");

if (!$conn) {
    die('[HTM]Problema ao conectar ao MYSQL; erro=' . mysql_error() . '-' . mysql_errno() . '[/HTM]');
}

$db = mysql_select_db($dbSite, $conn); // or die ("[HTM]Problema ao conectar ao banco de dados[/HTM]");

if (!$db) {
    die('[HTM]Problema ao conectar ao banco de dados; erro=' . mysql_error() . '[/HTM]');
}

if (!function_exists('fnc_preparaComando')) {

    function fnc_preparaComando($p_string) {
        return str_replace("'", "\'", $p_string);
    }

}

if (!function_exists('fnc_leituraDB')) {

    function fnc_leituraDB($p_sql) {

        global $w_registro, $numRows;

        $sql = mysql_query($p_sql);

        if (!$sql || ( ( $numRows = mysql_num_rows($sql) > 0 ) && !$w_registro = mysql_fetch_object($sql) )) {

            $message = '[ERR]LEITURA-DB: ' . mysql_error() . '[/ERR]';

            die($message);
        }
    }
}

// Para retornar valores do banco de dados com a acentuação e pontuação corretamente sem carecteres especiais no lugar
    mysql_query("SET NAMES utf8");
    mysql_query("SET CHARACTER_SET utf8");
?>

1 answer

1

mysql_* functions are obsolete since PHP 5.5. Prefer to use Mysqli or PDO (believe me, it will be much more advantageous).

"we should not use functions of the "mysql" extension because its development has been discontinued; the extension will soon become obsolete, ie code using these functions will not work in future versions of PHP."

Why should we not use mysql type functions_*?

Also, mysqli is very similar to mysql_* so you won’t have any problems to learn and migrate your projects. Example:

// Conexão mysql
$db = mysql_connect('localhost', 'mysql_user', 'mysql_password');

// Conexão mysqli
$db = mysqli_connect("localhost","my_user","my_password","my_db");

Mysqli functions end up being newer, faster and safer, have new functions and are object oriented, IE, is an evolution of the mysql extension_*.

There is still another alternative to mysql_* and mysqli, PDO. It allows working multiple databases and has Prepared statements (that is not so fast, but much safer).

Even though the PDO has been criticized for bringing more problems than solutions. In my opinion, I would choose it because it supports more BD drivers and because it is much safer.

  • Instead of replying, mark this as a duplicate of the other.

  • You Here’s an exact answer from you , the ideal would be to create this answer here: Phpmyadmin database connection error

  • 1

    I’ve flagged the question. @Jorgeb.

  • @Jorgeb. It would not be a duplicate my question, because it is working some Insert just this one that is not, what reason the first Insert work and the second not ?

  • @Renanrodrigues is true that it can happen. But the answer to the question is that, the error means that. Besides you yourself considered her duplicate.

  • Yes because I had not read all, more can help me that it may occur, because the other registers works perfectly.

Show 1 more comment

Browser other questions tagged

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