0
I have a combobox filled with data from the database (doctor’s name), where I choose a name, but I wanted to record in another table of the database the id_medico
and not the name.
I thought I’d use a SELECT
to get the id
, store that information in a variable and then use that variable in INSERT
, but I don’t know how to fit that into my code. You can help me?
The combobox is filled 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>";
}
}
It will be interesting for you to read: Why should we not use mysql type functions_*?
– Woss
$_POST['cbMedicos']
is the name or theid
of the doctor? In which table you need to save this value?– Woss
@Andersoncarloswoss $_POST['cbMedicos'] is the chosen name of the combobox, in the table _marked query. And I have the name of the doctors and their id in the table Medicos
– Isabel Sousa
You can’t already store the value of
id
in the combobox instead of the name? How are you doing it? There are ways to put it in the question?– Woss
I already edited the question... I want the name to appear in the combobox, the id I use in the marked query table to link between tables.
– Isabel Sousa
Okay, just do it
'<option value="'.$prod['id'].'">'.$prod['nome'].'</option>'
. Thus will be displayed the name of the doctor, but the value$_POST['cbMedicos']
will already be theid
of the selected doctor. It will avoid you having to make another query in the database.– Woss