I have a combobox filled with database (doctor's name) data, from where I choose a name, but I wanted to write the id_medico
and not the name in another database table.
I thought about using SELECT
to fetch id
, save this information in a variable and then use that variable in INSERT
, but I do not know how to fit it into my code. Can you help me?
The combobox is populated with this code:
....
$dbconn = mysqli_connect($servername, $username, $password, $dbname)or die("Failed to connect to database:" . mysqli_error($dbconn));
$query = "SELECT nome FROM Medicos";
$data = mysqli_query($dbconn, $query);
$result = mysqli_num_rows($data);
?>
   <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['nome'].'">'.$prod['nome'].'</option>';
}
?>
</select>
My code for now is:
$dbconn = mysql_connect($servername, $username, $password, $dbname);
mysql_select_db($dbname);
if (isset($_POST['botao_marcar_consulta']))
{
$hora_inicio= trim($_POST['txthora_inicio']);
$hora_fim = trim($_POST['txthora_fim']);
$medicos = trim($_POST['cbMedicos']);
$sql = "INSERT INTO Consulta_marcada (hora_inicio, hora_fim ) VALUES ('".$hora_inicio."', '".$hora_fim."')";
if(mysql_query($sql))
{
echo "<script>alert('Dados inseridos com sucesso');</script>";
}
else
{
echo "<script>alert('FAILED TO INSERT ".mysql_error()."');</script>";
}
}