Error insert PHP [duplicate]

0

I'm having a problem inserting data into the database, already checked if there is any field where it is not accepting null value, but everything is ok, when the error appears the following code is given:

  

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, how would this same document be passed to PDO or to 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");
?>
    
asked by anonymous 27.07.2015 / 18:11

1 answer

1

mysql_ * functions have been deprecated since PHP 5.5. Prefer to use MySQLi or PDO (believe me, it will be much more advantageous).

"We should not use" mysql "extension functions because their development has been discontinued, the extension will become obsolete soon, meaning code that uses these functions will not work in future versions of PHP."

Why should not we use functions of type mysql_ *?

In addition, mysqli is very similar to mysql_ * so you will not have any problems learning and migrating 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");

MySQL functions become newer, faster and more secure, have new functions and are object oriented, that is, an evolution of the mysql _ * extension.

There is still another alternative to mysql_ * and mysqli, the PDO. It allowed you to work multiple databases and has prepared statements ( which is not so fast, but much safer).

Even the PDO being heavily criticized for bringing more problems than solutions. In my opinion, I would choose it for having support for more BD drivers and for being much more secure.

    
27.07.2015 / 18:14