Limpuls
Limpuls

Reputation: 876

The row doesn't get DELETED in MySQL and PHP returns no error

I have a code which should delete a selected ID row from table by loading a remove.php file with SQL query. The problem is that it doesn't work but I get no errors or notices. My variable seems to get transfered to remove.php in URL just fine, as '<a href="remove.php?='.$Fvalue.'">' gets interpreted as

remove.php?=1

for example, where 1 is primary id of the row.

Here is all the code related to the question only:

EDIT:

Rendered HTML for products table with REMOVE button:

    <!-- Products table -->

    <table class="table">
        <thead>
        <tr>
            <th scope='col'>id </br ></th><th scope='col'>name </br ></th><th scope='col'>price </br ></th><th scope='col'>amount </br ></th><th scope='col'>category_name </br ></th>    </tr>
        </thead>
        <tbody>

        <tr><td data-id="1"><a href="remove.php?remove_id=1"> REMOVE</a></td>
<td>iPhone 7</td>
<td>800</td>
<td>15</td>
<td>Smartphones</td>
</tr><tr>
<td data-id="42"><a href="remove.php?remove_id=42"> REMOVE</a></td>
<td>Motorola </td>
<td>3000</td>
<td>5</td>
<td>Smartphones</td>
</tr><tr><td data-id="2"><a href="remove.php?remove_id=2"> REMOVE</a></td><td>Macbook Pro 2015</td>
<td>1300</td><td>10</td>
<td>Computers</td></tr><tr><td data-id="4"><a href="remove.php?remove_id=4"> REMOVE</a></td>
<td>Dell XPS</td>
<td>1400</td>
<td>6</td><td>Computers</td>
</tr><tr><td data-id="41"><a href="remove.php?remove_id=41"> REMOVE</a></td>
<td>CHROMEBOOK</td>
<td>5600</td>
<td>8</td>
<td>Computers</td></tr></tbody>

Updated PHP:

 <?php
    foreach ($newArray as $value) {
        echo '<tr>';
        foreach ($value as $key => $Fvalue) {

            $remove = $value['id'] = " REMOVE";
            if($value[$key] == $value['id']) {
                echo '<td data-id="'.$Fvalue.'">' . '<a href="remove.php?remove_id='.$Fvalue.'">' . $remove . '</a>' . '</td>';  // will show all values.
            } else {
                echo '<td>' . $Fvalue . '</td>';
            }
        }
        echo '</tr>';
    }
    ?>

remove.php

<?php
require_once ("navigation.php");
require_once("database_connection.php");
$id = !empty($_GET['remove_id']) ? $_GET['remove_id'] : null;
if($id != null) {
    $deleteProducts = "DELETE FROM `products` WHERE `id` = '.$id.'";
    mysqli_query($dbc, $deleteProducts);
}

Looking for any help to me spot any problems as I get no errors and have no idea why the code is not deleting a row in the table. Thanks.

Upvotes: 0

Views: 98

Answers (2)

GrumpyCrouton
GrumpyCrouton

Reputation: 8621

You have to pass a key associated with the value you are sending;

<a href="remove.php?remove_id='.$Fvalue.'">' . $remove . '</a>

Notice the remove_id part.

Now, in your PHP you can retreive this value;

$id = !empty($_GET['remove_id']) ? $_GET['remove_id'] : null;
if($id !== null) {
    $deleteProducts = "DELETE FROM `products` WHERE `id`='{$id}'";
    mysqli_query($dbc, $deleteProducts);
}

In my code here, I also fixed an issue you had in your code. You had $id = (isset($_GET['$Fvalue']));, which will always set $id to true or false, not to the ID that you had passed.

You can also clean up your HTML variables by using a double quote instead of a single quote.

if($value[$key] == $value['id']) {
    echo "<td data-id='{$Fvalue}'><a href='remove.php?remove_id={$Fvalue}'>{$remove}</a></td>";  // will show all values.
} else {
    echo "<td>{$Fvalue}</td>";
}

BIG NOTE: Little Bobby says you may be at risk for SQL Injection Attacks. Learn about Prepared Statements with parameterized queries.

Upvotes: 3

DigiLive
DigiLive

Reputation: 1103

Using the get method, you need key-value pairs in your url.

remove.php?id=1

Where id is the key and 1 is the value.

Upvotes: 0

Related Questions