Insert an ID in SQL + PHP

1

I am making a form where the user inserts the data and the same one receives the confirmation along with an ID record in php with sql, I am just not getting this final confirmation along with the id, can anyone help me?

I know that mysql is like this.

$exec_sql = mysql_query($select); 

if (!$exec_sql) {
    print mysql_error();
}
else {
    $id = mysql_insert_id();    
    print '<h3>Registro #'.$id.' inserido com sucesso</h3>';

But what about sql?

    
asked by anonymous 09.09.2016 / 15:50

1 answer

0

A code example for this action.

<?php
//Define as variáveis do banco
$servername = "localhost";
$username = "username";
$password = "password";

// Cria a conexão
$conn = new mysqli($servername, $username, $password);

//Verifica a conexão
if($conn->connect_error){
    die("Connection failed: " . $conn->connect_error);
}else{ 
echo "Conexão ok";
}
//Atribui a consulta a uma variável, você está usando select como nome de variável, mas é presumível um INSERT
$select = "INSERT INTO minhaTabela (colunaFoo, colunaBar)
VALUES ('dadoFoo', 'dadoBar')";

//Executa a consulta
$exec_sql = mysqli_query($conn, $select);

if(!$exec_sql){
print mysqli_error($conn);
}
else{
$id = mysqli_insert_id($conn);    
print '<h3>Registro #'.$id.' inserido com sucesso</h3>'; 
}

//Fecha a conexão   
mysqli_close($con);
?>

I hope it helps.

    
09.09.2016 / 16:40