When adding user, it always gives ERROR but ends up adding

Asked

Viewed 20 times

0

Good people, I have a problem that is the following: in a form to add the user I fill everything and add the user but appears the message to say:

ERROR:"Try Again".

How is it possible to add the user but show an error message. I leave here the code php in connection with mysql:

    <?php
session_start();
error_reporting(0);
include('includes/config.php');
if(strlen($_SESSION['alogin'])==0)
    {   
header('location:index.php');
}
else{
if(isset($_POST['add']))
{
$nomeutente=$_POST['NomeUtente'];
$pessoaref=$_POST['PessoaRef']; 
$responsavel=$_POST['Responsavel'];   
$nrutente=$_POST['NrUtente']; 
$telemovel=$_POST['TelemovelContact'];  
$telefone=$_POST['TelefoneContact'];  
$datanascimento=$_POST['DataNascimento'];  
$nridcivil=$_POST['NrIdCivil'];  
$nacionalidadeutente=$_POST['NacionalidadeUten'];  
$nrsaude=$_POST['NrUtenSaude']; 
$sql="INSERT INTO tblutentes(NomeUtente, PessoaRef, Responsavel, NrUtente, TelemovelContact, TelefoneContact, DataNascimento,NrIdCivil, NacionalidadeUten, NrUtenSaude) VALUES(:nomeutente, :pessoaref ,:responsavel, :nrutente, :telemovel, :telefone, :datanascimento, :nridcivil, :nacionalidadeutente, :nrsaude)";
$query = $dbh->prepare($sql);
$query->bindParam(':nomeutente',$nomeutente,PDO::PARAM_STR);
$query->bindParam(':pessoaref',$pessoaref,PDO::PARAM_STR);
$query->bindParam(':responsavel',$responsavel,PDO::PARAM_STR);
$query->bindParam(':nrutente',$nrutente,PDO::PARAM_STR);
$query->bindParam(':telemovel',$telemovel,PDO::PARAM_STR);
$query->bindParam(':telefone',$telefone,PDO::PARAM_STR);
$query->bindParam(':datanascimento',$datanascimento,PDO::PARAM_STR);
$query->bindParam(':nridcivil',$nridcivil,PDO::PARAM_STR);
$query->bindParam(':nacionalidadeutente',$nacionalidadeutente,PDO::PARAM_STR);
$query->bindParam(':nrsaude',$nrsaude,PDO::PARAM_STR);
$lastInsertId = $dbh->lastInsertId();
$query->execute();

if($lastInsertId)
{
$msg="Utente Adicionado Com Sucesso!";
}
else 
{
$error="Erro, tente novamente!";
}}
?>
  • What is the mysql return for executing the above code? The error, try again is your message handled. Edit the post and put the return of mysql so we can help more easily.

1 answer

1


You need to reverse the order, because if you have not yet executed the query, you have no changes.

Also, you may need the commit depending on how you are using.

Example:

...
$query->execute();
$lastInsertId = $dbh->lastInsertId();
$dbh->commit(); # caso necessário
  • It worked, thank you very much :)

  • can you please see if you can help me with this other question: https://answall.com/questions/328318/n%C3%A3o-grava-dados-com-mysql

Browser other questions tagged

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