-1
Good evening, I’m learning php and doing a CRUD to train, I was able to select in the table and the Insert to add new data, but I wanted to click the edit button, open a modal bootstrap (I already added) with the selected data inside the inputs for update (I don’t know how to do this part). Can anyone help me with this? I’m sorry if you already have a post talking about this.
php connection.
<?php
$servidor = "localhost";
$usuario = "root";
$senha = "";
$dbname = "locadora";
$conn = mysqli_connect($servidor, $usuario, $senha, $dbname);
if(!$conn){
die("Falha na conexao: " . mysqli_connect_error());
}
function conectaBanco(){
$bd = mysqli_connect("localhost", "root", "", "locadora");
return $bd;
}
?>
Table with edit button for modal in functios.php
<table class="table">
<thead>
<tr>
<th scope="col">Código</th>
<th scope="col">CPF</th>
<th scope="col">Nome</th>
<th scope="col">Salário</th>
<th scope="col">Ação</th>
</tr>
</thead>
<tbody>
<?php
foreach ($grupo as $row) {
?>
<tr>
<th><?=$row["codigo"]?></th>
<td><?=$row["cpf"]?></td>
<td><?=$row["nome"]?></td>
<td><?=$row["salario"]?></td>
<td>
<div class="btn-group">
<form name="editar" method="POST">
<input type="hidden" name="idEditar" value="" />
<input class="btn btn-success" name="editar" type="button" value="Editar" data-toggle="modal" data-target="#exampleModal" data-whatever="Atualizar Funcionário">
</form>
<form name="deletar" action="../acao/excluir.php" method="POST">
<input type="hidden" name="idExcluir" value="" />
<input type="hidden" name="acao" value="excluir" />
<input class="btn btn-danger" name="deletar" type="submit" value="Deletar">
</form>
</div>
</td>
</tr>
<!-- Modal -->
<div class="modal fade" id="exampleModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<!-- Modal Cabeçalho -->
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLabel">Atualizar Funcionário</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Fechar">
<span aria-hidden="true">×</span>
</button>
</div>
<!-- Modal Corpo -->
<div class="modal-body">
<form method="POST" action="acao/atualizar.php" enctype="multipart/form-data">
<div class="form-group">
<input type="text" class="form-control" name="codigoFuncionario" placeholder="Código..." value="">
</div>
<div class="form-group">
<input type="text" class="form-control" name="cpfFuncionario" placeholder="CPF..." value="">
</div>
<div class="form-group">
<input type="text" class="form-control" name="nomeFuncionario" placeholder="Nome..." value="" >
</div>
<div class="form-group">
<input type="text" class="form-control" name="salarioFuncionario" placeholder="Salário" value="" >
</div>
</form>
</div>
<!-- Modal Footer -->
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Cancelar</button>
<button type="button" class="btn btn-primary">Atualizar</button>
</div>
</div>
</div>
</div>
<?php
}
?>
</tbody>
</table>
Only the function in the update.php table is missing to select the data and show inside the modal inputs.
Thanks. Great explanation and the script worked perfectly.
– Lucas
It only updates one row of the table. Let’s say there are 5 employees in the table, it only updates 1, if I click on the others, will appear the data all right in the modal, but does not update the others, only 1
– Lucas
Yes, you cannot update user data with id 2 by filling in id 1 data
– Vinicius.Silva