在php mysql上更新FK
So, I have a form where i would want to update some records, all the fields are working properly but the FK (which is id_colaborador
)
I want also to input the name of the user (nome
) instead of the id
I have the following UPDATE
code but it just ignores the FK field i don't know why...
$sql = "UPDATE ativos SET ativo = ?, comentario = ?, data_aquisicao = ?, localizacao = ?, fabricante = ?, modelo = ?, imei = ?,
numero_serie = ?, ativo_sap = ?, anexo_a = ?, evento = ?, data_evento = ?, id_colaborador = ? WHERE id_ativo = ?";
And for the input of id_colaborador
I have this:
<div class="control-group <?php echo !empty($id_colaboradorError)?'error':'';?>">
<label class="control-label"><strong>Colaborador<span style="color: red">*</span></strong></label>
<div class="controls">
<input type="text" placeholder="Colaborador" value="<?php echo !empty($id_colaborador)?$id_colaborador:'';?>">
<?php if (!empty($id_colaboradorError)): ?>
<span class="help-inline"><?php echo $id_colaboradorError;?></span>
<?php endif;?>
</div>
</div>
Your form field is missing the name attribute. If the rest of your code expects the name to be id_colaborador
, you'd add it like this:
<input name="id_colaborador" type="text" placeholder="Colaborador" value="<?php echo !empty($id_colaborador)?$id_colaborador:'';?>">
For the second part of the question, the answer depends on how many users you have.
If you only have a few, you can list them all in a SELECT
box, where the value of the option is the numeric id.
If you have many users, you can use an autocomplete text field for the name and a hidden text input for the ID. When a is selected via autocomplete, you'd fill the hidden input with the ID.