-1
I want to delete the Rows that I select with my checkbox and delete also in the database so that is giving pq in this part of the code :
<?php
include "conexao.php";
if (isset($_POST['sel'])) {
foreach ($_POST['sel'] as $id) {
$sql="DELETE FROM lojas WHERE id = $id";
$res=mysqli_query($con,$sql);
}
}else{
echo "form nao submetido";
}
?>
Is appearing on the screen the Else or is "not submitted form" and not what is in the if
Here the whole code:
<form class="customform" method="POST" action="fabrica_conLoja2.php" >
<div class="s-12 l-7">Nome Loja<input name="nloja" placeholder="Nome Loja" type="text" /></div>
<div class="s-12 l-7">Nome Gerente<input name="ngerente" placeholder="Nome Gerente" type="text" /></div>
<div class="s-12"></div>
<div class="s-12 m-6 l-4"><button name="cadastrar" type="submit" value="cadastrar" onclick="">Pesquisar</button></div>
<title></title>
</head>
<body>
<table>
<tr>
<th>Id</th>
<th>Nome Loja</th>
<th>Nome Gerente</th>
<th>Rua</th>
<th>Bairro</th>
<th>Número Estabele.</th>
<th>Estado</th>
<th>Cidade</th>
<th>Telefone</th>
<th>Selecionar</th>
</tr>
<br><br><br><br><br><br><br><br><br>
<?php
include "conexao.php";
$nomeLoja=$_POST["nloja"];//"nloja");
$nomeGerente=$_POST["ngerente"];//"nome_gerente");
$sql = "SELECT * FROM lojas WHERE nome LIKE '%$nomeLoja%' and nome_gerente LIKE '%$nomeGerente%' ";
$res=mysqli_query($con,$sql);
$li = mysqli_num_rows($res);
echo "               $li loja(s) encontrada(s)<br/>";
while ($vloj=mysqli_fetch_row($res)) {
$vid=$vloj[0];
$vnome=$vloj[1];
$vgerente=$vloj[2];
$vrua=$vloj[3];
$vbairro=$vloj[4];
$vnumero=$vloj[5];
$vuf=$vloj[6];
$vcidade=$vloj[7];
$vtel=$vloj[8];
echo"<tr>
<td>$vid</td>
<td>$vnome</td>
<td>$vgerente</td>
<td>$vrua</td>
<td>$vbairro</td>
<td>$vnumero</td>
<td>$vuf</td>
<td>$vcidade</td>
<td>$vtel</td>
<td align = center><input type='checkbox' value='$vid' name='sel[]'></td>
</tr>";
}
?>
</table> <br>
</form>
<form method="POST" action="fabrica_conLoja2.php">
<?php
include "conexao.php";
if (isset($_POST['sel'])) {
foreach ($_POST['sel'] as $id) {
$sql="DELETE FROM lojas WHERE id = $id";
$res=mysqli_query($con,$sql);
}
}else{
echo "form nao submetido";
}
?>
Just one observation: I think
$id
should be in single quotes in the query, like this:...WHERE id = '$id'
.– Sam
@Davidsamm Not necessarily depends on how he created the table. If Id is INT it should not be in quotes, if varchar should.
– Renato Junior
You could report the error that appears?
– Renato Junior
@Renatosilva Legal!
– Sam
@Renatosilva The error in the code is structural. In my answer is working normal. It was not sending a second <form> with the checkbox.
– Sam