Mysqli REMOVE ahref not working

I have a “Delete” link next to everyone $rows, when the mouse above them reflects the correct identifier for deletion, however, when I click DELETE, I am redirected to phpfile.php?id=4, for example, and nothing happens deleted, no errors were sent.

while ($row = mysqli_fetch_array($r,MYSQLI_ASSOC))
{
    echo '<tr><td align="left">' .
    $row['title'] . '</td><td align="left">'
    . $row['genre'] . '</td><td align="left">'
    . $row['length'] . '</td><td align="left">'
    . $row['created'] . '</td><td align="left">'
    . $row['views'] . '</td><td align="left">'
    . "<a href='newwriter_profile.php?id={$row['upload_id']}'>Delete</a></td>" .      '</tr>';
}
echo '</table>'; // Close the table

The rest of the code existing on the same page:

if(isset($_GET['id'])) {
// Get the ID
$id = intval($_GET['upload_id']);


require_once ('../mysqli_connect.php'); //Connect to the db




    $delquery = "
        DELETE 
        FROM upload
        WHERE upload_id = {$id}";
    $done = @mysqli_query ($dbc, $delquery); // Run the query

    if($done) {
        // Make sure the result is valid
        if (mysqli_num_rows($done)==1) {
        echo 'Record Deleted';
        }
        else {
            echo 'error - delete failed';
        }

        // Free the mysqli resources
        @mysqli_free_result($result);
    }
    else {
        echo "Error! Query failed:" .$mysqli_error($dbc);
    }
    mysqli_free_result($done);
    mysqli_close($dbc);
}

If I can solve this error, I will resolve a similar error, except with the download function.

+3
source share
1 answer

You pull out $idof nonexistent $_GET['upload_id']when you are going to use $_GET['id']. Since $_GET['upload_id']it is not set, its value is equal NULL, which is interpreted as 0. Your request ends as:DELETE FROM upload WHERE upload_id = 0

$id = intval($_GET['upload_id']);
// Should be
$id = intval($_GET['id']);

intval() $id. , , ?id=abc "abc", intval("abc") 0, 0 . id , - :

if (ctype_digit($_GET['id'])) {
  // ok, do your query
}
else {
  // invalid input, report error to user and don't touch your database.
}

, script, ( , ), , . , , , . URL-, . : The Spider of Doom

+2

All Articles