How to join edit/new button?

Asked

Viewed 46 times

-1

I would like, if there is already $nome registered in the table, it gives an update and if it does not exist, it gives an input. I do not understand what the problem in my code.

$dbconn = mysqli_connect($servername, $username, $password, $dbname)or die("Failed to connect to database:" . mysqli_error($dbconn));
if (isset($_POST['botao_editar']))
{

$Nome = trim($_POST['txtNome']); 
$NIF = trim($_POST['txtNif']); 
$bday = trim($_POST['txtnascimento']); 
$email = trim($_POST['txtEmail']); 
$telemovel = trim($_POST['txtTelemovel']); 
$morada = trim($_POST['txtMorada']); 
$codigo = trim($_POST['txtCodigo']); 
$localidade = trim($_POST['txtLocalidade']); 
$especialidade = trim($_POST['txtEspecialidade']); 
$observacao = trim($_POST['txtObservacao']); 
$cbnome= trim($_POST['cbMedicos']);

 $query = "UPDATE Medicos SET nome= '".$Nome."', NIF='".$nif."', data_nascimento='".$bday."', email='".$email."', telemovel='".$telemovel."', morada='".$morada."', codigo_postal= '".$codigo."', Localidade= '".$localidade."', especialidade= '".$especialidade."', observacoes= '".$observacao."' WHERE nome = '$cbnome'";  
$data = mysqli_query($dbconn, $query);
    $result = mysqli_num_rows($data);

    if ($result == 0) 
    {
        $query = "INSERT INTO Medicos (nome, NIF, telemovel, data_nascimento, email, morada , observacoes, codigo_postal, especialidade, Localidade )  VALUES ('".$Nome."', '".$nif."', '".$telemovel."', '".$bday."', '".$email."', '".$morada."', '".$observacao."', '".$codigo."', '".$especialidade."', '".$localidade."')";

    $data = mysqli_query($dbconn, $query);

    echo  "<br>Dados do pacientes gravados com sucesso <br>";
}
        else
        {
                echo "<br> O novo paciente foi inserido com sucesso <br>";
            }

else
    {

        echo  "<br>Falhou a inserir os dados <br>";
        ?>

    <?php
    }
    mysqli_close($dbcon);
    ?>
  • Welcome to Stackoverflow! Please explain the problem better, and if possible include a example of code that reproduces what is happening, because your question is not noticeable. See Help Center How to Ask.

  • @Francis I have no example, but I can try to explain better... I fill in the fields and then press the button, when this happens it does an update in case there is a name =cbname, if it does not exist it is because it is a new entry and so does the Insert.

  • I understand what you want, but in your code, what is not working? The update? The Insert? The 2?

  • think q is not necessary you do an update, and if the return is 0, give an Insert... has other ways to do this rsrs

  • @Francisco I believe that the querys are correct, I think the problem is in the following code... I just can’t figure out what, if it’s some parentheses or if there’s a line of code missing...

  • If any response has helped you, don’t forget to mark as correct!

Show 1 more comment

2 answers

0


I believe the countryside $name in this Insert should be $Nome.

And a mysqliquery was missing right after the Insert:

    if ($result == 0) 
    {
      $query = "INSERT INTO Medicos (nome, NIF, telemovel, 

      data_nascimento, email, morada , observacoes, codigo_postal,

      especialidade, Localidade ) 

      VALUES ('".$name."', '".$nif."','".$telemovel."', '".$bday."',

              '".$email."', '".$morada."', '".$observacoes."', 

              '".$codigo_postal."', '".$especialidade."', '".$localidade."')";

        $data = mysqli_query($dbconn, $query);

        echo  "<br>Dados do pacientes gravados com sucesso <br>";
    }

0

I recommend checking already within SQL, with the INSERT ... ON DUPLICATE KEY UPDATE.

$query = "INSERT INTO Medicos (nome, NIF, telemovel, 
      data_nascimento, email, morada , observacoes, codigo_postal,
      especialidade, Localidade ) 
      VALUES ('".$name."', '".$nif."','".$telemovel."', '".$bday."',
              '".$email."', '".$morada."', '".$observacoes."', 
              '".$codigo_postal."', '".$especialidade."', '".$localidade."')";
      ON DUPLICATE KEY UPDATE
              nome= '".$Nome."', NIF='".$nif."', data_nascimento='".$bday."',
              email='".$email."', telemovel='".$telemovel."',
              morada='".$morada."', codigo_postal= '".$codigo."',
              Localidade= '".$localidade."', especialidade= '".$especialidade."',
              observacoes= '".$observacao."'";

I don’t know if you’re right, any doubt you can look at original documentation.

Browser other questions tagged

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