I'm having some trouble trying to build a very simple inventory management system.
What I'm doing is showing the data from a database in a html table and in each row a create two buttons: one to edit and one to delete the item. The problem is that I'm not being able to call these buttons with the isset()
function and I can't understand why. I've tried to create a specific function for these but still doesn't work. Anybody has any idea?
Here is the code:
P.S.: Don't mind small english erros or a lack of of brackets. I had to change the code a little bit.
function searchTablet(){
if(isset($_POST['btnSearchTablet'])){
global $connection;
$query="SELECT * FROM tablet";
$run=mysqli_query($connection, $query);
echo "<table class='table table-striped'>";
echo "<thead>";
echo "<tr>";
echo "<th>ID</th>";
echo "<th>Brand</th>";
echo "<th>Model</th>";
echo "<th>Color</th>";
echo "<th>Price</th>";
echo "<th>Fabrication Date</th>";
echo "<th>Provider</th>";
echo "<th>Registration Date</th>";
echo "<th>Edit</th>";
echo "<th>Delete</th>";
echo "</tr>";
echo "</thead>";
while($obj=mysqli_fetch_object($run)){
echo "<tr>";
echo "<td>$obj->id</td>";
echo "<td>$obj->idBrand</td>";
echo "<td>$obj->idModel</td>";
echo "<td>$obj->idColor</td>";
echo "<td>$obj->price</td>";
echo "<td>$obj->fabricationDate</td>";
echo "<td>$obj->idProvider</td>";
echo "<td>$obj->registrationDate</td>";
echo "<td><a href='resultTablet.php?btnEditTablet{$obj->id}'class='btn btn-primary' name='btnEditTablet'>Alterar</a></td>";
echo "<td><a href='resultTablet.php?btnDeleteTablet{$obj->id}' class='btn btn-danger' name='btnDeleteTablet'>Excluir</a></td>";
echo "</tr>";
if(isset($_POST["btnDeleteTablet{$obj->id}"])){
$idTablet=$obj->id;
$delQuery="DELETE FROM tablet WHERE id='$idTablet'";
$delRun=mysqli_query($connection, $delQuery);
if($delRun){
echo "<div class='alert alert-success' role='alert'>Device was successfuly deleted.</div>";
}else{
echo "<div class='alert alert-danger' role='alert'>Error.</div>";
}
}
}
If you are passing parameters via URL you have to use $_GET
not $_POST
method.
Also, I would change
<a href='resultTablet.php?btnEditTablet{$obj->id}'class='btn btn-primary' name='btnEditTablet'>Alterar</a>
to
<a href='resultTablet.php?btnEditTablet={$obj->id}'class='btn btn-primary' name='btnEditTablet'>Alterar</a>
So you could do this:
if(isset($_GET["btnDeleteTablet"])){
and you would get id via $_GET
like this:
$idTablet=$_GET["btnDeleteTablet"];
After this change, you can close while loop
you used before
this if(isset($_GET["btnDeleteTablet"]))
line,
also you wont need $obj->id
anymore, because you will $_GET
data decoupled from while loop
or any other code you wrote before.
On another note, I see you forgot <tbody> </tbody>
in your table.
EDIT:
Also, what are you doing with
if(isset($_POST['btnSearchTablet'])){
This wont work after delete button click.
You shouldn't use it like that, because after the delete button click your page will go to URL with $_GET
parameters and that if logic will prevent
if(isset($_GET["btnDeleteTablet"])){
logic working. So move whole
if(isset($_GET["btnDeleteTablet"])){
...
}
out of if(isset($_POST['btnSearchTablet'])){
Read up about $_POST
and $_GET
methods also, read about forms
you really need it.
Also, I recommend you to get program to profile requests so you could see how post and get data moves.
I recommend you to install Fiddler
program, so you would be able to see how post
, get
data moves.
Ok, I will try to fix your code at last so it could work:
<?php
function searchTablet(){
global $connection;
if(isset($_GET['btnDeleteTablet'])){
deleteTablet();
}
//I dont why are you using it so I commented it out.
//if(isset($_POST['btnSearchTablet'])){
//}
displaySearchTablet();
}
function displaySearchTablet(){
global $connection;
$query = "SELECT * FROM tablet";
$run = mysqli_query($connection, $query);
while($obj = mysqli_fetch_object($run)){
//Combine all rows into one variable
$table_rows .= "
<tr>
<td>{$obj->id}</td>
<td>{$obj->idBrand}</td>
<td>{$obj->idModel}</td>
<td>{$obj->idColor}</td>
<td>{$obj->price}</td>
<td>{$obj->fabricationDate}</td>
<td>{$obj->idProvider}</td>
<td>{$obj->registrationDate}</td>
<td>
<a href='resultTablet.php?btnEditTablet={$obj->id}' class='btn btn-primary' name='btnEditTablet'>Alterar</a>
</td>
<td>
<a href='resultTablet.php?btnDeleteTablet={$obj->id}' class='btn btn-danger' name='btnDeleteTablet'>Excluir</a>
</td>
</tr>";
}
$result_table =
"<table class='table table-striped'>
<thead>
<tr>
<th>ID</th>
<th>Brand</th>
<th>Model</th>
<th>Color</th>
<th>Price</th>
<th>Fabrication Date</th>
<th>Provider</th>
<th>Registration Date</th>
<th>Edit</th>
<th>Delete</th>
</tr>
</thead>
<tbody>
$table_rows
</tbody>
</table>";
echo $result_table;
}
function deleteTablet(){
global $connection;
$id = $_GET['btnDeleteTablet'];
$query = "DELETE FROM tablet WHERE id = '$id'";
$run = mysqli_query($connection, $query);
if($run){
echo "<div class='alert alert-success' role='alert'>Device was successfuly deleted.</div>";
}else{
echo "<div class='alert alert-danger' role='alert'>Error.</div>";
}
}
I kept it very basic, so you would be able to understand. I didn't pass parameters or returned anything so it would be more understandable to you.