How to pass information chosen in the combobox to the database?

Asked

Viewed 230 times

0

I have a database that is filled with data coming from the database and I wanted that after pressing the button, that chosen information to go to the database. Whenever I try to send the data I have in the textbox without using the combobox works, from the moment in use the combobox no longer works. I tried to send the value of the combobox to a textbox and from there record, but I also could not make it work.

This is the code called, to the main page:

....
if (isset($_POST['botao_marcar']))
{

     $data = trim($_POST['data_marcacao']); 
     $hora = trim($_POST['hora_marcacao']);
     $especialidade= trim($_POST['cbEspecialidade']);
     $observacoes= trim($_POST['observacoes']);


     $sql = "INSERT INTO Consulta_marcada (data, hora_inicio, especialidade observacoes) VALUES ('".$data."', '".$hora."', '".$especialidade."','".$observacoes."')";

....

the main code is:

<form action="<?php echo $_SERVER['PHP_SELF'];?>" method="post" >
<label>Data:</label>
<input type="date" name="data_marcacao"> &nbsp &nbsp &nbsp 

<label>Hora:</label>
<input type="time" name="hora_marcaçao">

....
$query = "SELECT especialidade FROM Medicos";
$data = mysqli_query($dbconn, $query);
$result = mysqli_num_rows($data);       

?>

<form name="especialidades" method="post" action="">
<label for="cbEspecialidade">Selecione um Medico</label>
<select id="cbEspecialidade" name="cbEspecialidade">
<option>Selecione...</option>

<?php while($prod = $data->fetch_assoc()) { 
    echo '<option 
value="'.$prod['especialidade'].'">'.$prod['especialidade'].'</option>';
}
?>    
</select>

....

<div id="observacoes">
&nbsp &nbsp <label><b><font size=4>Observa&ccedil;&otilde;es:</font></b>
</label> <br>
&nbsp &nbsp  <textarea type="text" id="observacoes" value="" rows="3" cols="87" maxlength="50"> </textarea>
</div>

<br>
<br>
<div id="botao">
  <input type="submit" name="botao_marcar" value="MARCAR CONSULTA" class=botaoEnviar /> 
</div>  
</form>

1 answer

0


From what I see in your code, the Specialty select does not have the name attribute, and in PHP you are searching with a different name that is in HTML.

Include the name attribute in select and call in PHP with the same value, for example "cbEspeciality" as it is in the ID attribute

  • I’ve already edited the code on top, for how I’m using it now but it still doesn’t... It’s not connecting to the database and I can’t figure out why, if I take the information part of the combobox it’s working...

  • There are a few things to consider, for example: Does the bank have the 'specialty' column? Accept the amount you are passing?

  • It is already solved.. Another thing, I have an input type time, but it is not compatible with the time attribute of the database table... Is there any way around that?

  • If you want to convert to timestamp, normally the field already returns in the default value for this, type: $data = strtotime($_POST['data']);. If not, you need to make the field value YYYY-MM-DD to use this command. If you want to convert this timestamp into datetime, just make $dt = date('Y/m/d h:i:s', $data);

  • I recommend not using the time field as not all browsers have support (read microsoft)

  • I have another way to do this, but I needed to know how to do a query where I want what is not in the database, I mean, I have a few hours marked in the database and I want a query where I see all the other hours that are not yet scheduled.. This is possible?

  • You will have to make a temporary table of possible hours, and do a NOT IN of the hours that is in your target table, type: SELECT TIME FROM #tabletime NOT IN (SELECT DISTINCT TIME FROM tabletarget)

Show 2 more comments

Browser other questions tagged

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