-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.
– Francisco
@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.
– Isabel Sousa
I understand what you want, but in your code, what is not working? The update? The Insert? The 2?
– Francisco
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
– Rovann Linhalis
@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...
– Isabel Sousa
If any response has helped you, don’t forget to mark as correct!
– Francisco