0

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>";
                        }    
                    }
}
                    
Martin
  • 22,212
  • 11
  • 70
  • 132
  • What does "call these buttons with the isset() function" mean? What, specifically, are you expecting to happen, and what is actually happening? – Greg Schmidt Apr 17 '21 at 02:39
  • 1
    Try !empty() instead of isset(). This will evaluate to true only if there is something other than null, false, 0, or the empty string ''. You probably have empty strings being submitted. – svikramjeet Apr 17 '21 at 08:30
  • 1
    **Warning:** You are wide open to [SQL Injections](https://php.net/manual/en/security.database.sql-injection.php) and should use parameterized **prepared statements** instead of manually building your queries. They are provided by [PDO](https://php.net/manual/pdo.prepared-statements.php) or by [MySQLi](https://php.net/manual/mysqli.quickstart.prepared-statements.php). Never trust any kind of input! Even when your queries are executed only by trusted users, [you are still in risk of corrupting your data](http://bobby-tables.com/). [Escaping is not enough!](https://stackoverflow.com/q/5741187) – Dharman Apr 17 '21 at 11:12

2 Answers2

0

As always... looks like StackOverflow users are toxic and unfriendly and don't even try to help new people who want to get into programming.

So I will try to help you a little bit.

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.

Good luck on your journey to programming.

Matas Lesinskas
  • 414
  • 6
  • 13
-1

isset() will return false if an empty string is being submitted, so try using !empty() instead.

Elon Zito
  • 2,872
  • 1
  • 24
  • 28
  • You have this the wrong way round: isset() will return _true_ for an empty string, !empty() will false. I'm not sure if that's the OP's problem, though, because I don't really understand the question, and I'm hoping they'll edit it to be clearer. – IMSoP Apr 18 '21 at 17:33