0
The main code is this:
$dbconn = mysqli_connect($servername, $username, $password, $dbname)or die("Failed to connect to database:" . mysqli_error($dbconn));
$query = "SELECT id_medico as txtid, nome as txtNome, email as txtEmail, telemovel as txtTelemovel, morada as txtMorada, especialidade as txtEspecialidade, NIF as txtNif, Localidade as txtLocalidade, codigo_postal as txtCodigo, data_nascimento as txtnascimento, observacoes as txtObservacao FROM Medicos";
$data = mysqli_query($dbconn, $query);
$result = mysqli_num_rows($data);
$meujson = array();
?>
<div id=Corpo>
<br>
<br>
<form name="medicos" method="post" action="">
<label for="cbMedicos">Selecione um Médico</label>
<select id="cbMedicos" name="cbMedicos">
<option>Selecione...</option>
<?php while($prod = $data->fetch_assoc()) {
echo '<option value="'.$prod['txtid'].'">'.$prod['txtNome'].'</option>';
$meujson[$prod['txtid']] = $prod; // coleciona as linhas no $meujson
$_SESSION[$prod['txtid']] = $prod['txtid'];
}
?>
</select>
<br> <br> <br>
    <label for="txtid">Nome:</label>
<input type="text" id="txtNome" name="txtNome" value="" size=90 /> <br> <br>
   <label for="txtid">NIF:</label>    
<input type="number" id="txtNif" name="txtNif" value="" />      
 
   <label for="txtid">Data de Nascimento:</label>  
<input type="date" id="txtdata" name="txtdata" value="" /> <br> <br>
   <label for="txtid">Email:</label>
<input type="text" id="txtEmail" name="txtEmail"value="" size=30 />  
 
   <label for="txtid">Telemóvel:</label>
<input type="number" id="txtTelemovel" name="txtTelemovel" value="" /> <br>
<br>
   <label for="txtid">Morada:</label>
<input type="text" id="txtMorada" name="txtMorada" value="" size=90 /> <br>
<br>
   <label for="txtid">Código Postal:</label>
<input type="number" id="txtCodigo" name="txtCodigo" value="" />
   <label for="txtid">Localidade:</label>
<input type="text" id="txtLocalidade" name="txtLocalidade"value="" /> <br>
<br>
   <label for="txtid">Especialidade:</label>
<input type="text" id="txtEspecialidade" name="txtEspecialidade" value="" /><br> <br>
   <label for="txtid">Observações:</label>
<input type="text" id="txtObservacao" name="txtObservacao" value="" size=90/><br> <br>
<script type="text/javascript">
var dados = <?php echo json_encode($meujson); ?>;
document.getElementById("cbMedicos").addEventListener("change", function ()
{
var linha = dados[this.value];
for (var ch in linha) {
(document.getElementById(ch) || {}).value = linha[ch];
}
});
</script>
<script type="text/javascript">
document.getElementById("cbMedicos").addEventListener("change", function ()
{
document.getElementById("txtid").value = this.value;
});
</script>
<br>
<br>
<br>
    <input type="submit" value="Novo" class="botaoNovo" name="botao_novo">
      <input type="reset" value="Apagar" class="botaoApagar" name="botao_apagar">
      <input type="submit" value="Editar" class="botaoEditar" name="botao_editar">
</div>
</form>
And then the code below is called to the front page. And I want the query update to compare the name of the database with the name chosen from the combobox, I had put so:
....
$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>";
}
?>
<?php
}
mysqli_close($dbconn);
?>
but it’s not working, someone knows how to do?
Your
query
seems to be right, try to write it directly onSQL
and see if any errors return, or anything out of the ordinary.– Francisco
@Francisco already did this, but no sql can not put with the names of Abels and I think the problem will be in WHERE
– Isabel Sousa
Give an Insert just for testing and run the query simulating some values.
– Francisco
@Francisco edited the question and put all the code... He always does the Insert, never does the update.
– Isabel Sousa
$Row['txtid_medico']; Wouldn’t it be $Prod['txtid_medico'];? Something else who is txtid_medico?
– user60252
I had never used this line of code, they gave me this line to save a variable and to be called in another PHP file, I adapted to the combobox, but the id_medico is wrong is txtid... I already edit the question, but in this case this line is not called to this code...
– Isabel Sousa
continues wrong is not $Row['txtid'] and yes $Prod['txtid']
– user60252
$dbconn != $dbcon You are using mysqli_query($dbconn and when you close the connection you are mysqli_close($dbcon
– user60252
@Leocaracciolo already edited the code but it’s still the same...
– Isabel Sousa
I’m not saying that these errors would fix the update, they’re just details. I’m creating an environment on my server/bank to test your script
– user60252
I could post HTML as well
– user60252
@Leocaracciolo already edited my code with what I have
– Isabel Sousa