Do i need to select before delete in mysql?:

In my websites admin page, i can delete products from the webshop, with this php code below. (Ajax calls this php file).

My question is, that do i need to select and check the num rows, or drop that, and just keep and run the delete queries?

    <?php
include_once("../../files/connect.php");
if($_POST)
{
    $id = mysqli_real_escape_string($kapcs, $_POST['id']);
    $check = mysqli_query($kapcs, "SELECT termek_id, termek_thumb, termek_big FROM termek WHERE termek_id='$id' LIMIT 1");
    if(mysqli_num_rows($check) > 0 )
    {


        /*Galéria törlése, ha van*/
        $check_gallery = mysqli_query($kapcs, "SELECT * FROM gallery WHERE gallery_termek_id = '$id'");
        if(mysqli_num_rows($check_gallery) > 0 )
        {
            while($gall = mysqli_fetch_assoc($check_gallery))
            {
                $DestinationDirectory = "../../images/gallery/";
                if(file_exists($DestinationDirectory.$gall['gallery_thumb']))
                {
                    unlink($DestinationDirectory . $gall['gallery_thumb']); 
                }
                if(file_exists($DestinationDirectory.$gall['gallery_big']))
                {
                    unlink($DestinationDirectory . $gall['gallery_big']);   
                }
                mysqli_query($kapcs, "DELETE FROM gallery WHERE gallery_termek_id = '$id'") or die(mysqli_error($kapcs));
            }
        }

        /* Címkék törlése, ha van */
        $check_cimke = mysqli_query($kapcs, "SELECT termek_cimke_termek_id FROM termek_cimke WHERE termek_cimke_termek_id = '$id'");
        if(mysqli_num_rows($check_cimke) > 0 )
        {   
            mysqli_query($kapcs, "DELETE FROM termek_cimke WHERE termek_cimke_termek_id = '$id'");
        }



        mysqli_query($kapcs, "DELETE FROM hir_termek_kapcsolo WHERE hir_termek_kapcs_termek = '$id' ") or die("Delete error 2 - " . mysqli_error($kapcs));


        $del = mysqli_query($kapcs, "DELETE FROM termek WHERE termek_id = '$id'");
        if($del)
        {
            echo 'Delete ok';
        }
        else
        {
            echo 'Delete error;
        }
    }
    else
    {
        echo 'Nincs ilyen azonosítójú termék.';
    }
}
?>

2 answers

  • answered 2018-01-13 16:58 Gordon Linoff

    You do not need to select before you delete.

    If no rows match the conditions, then the delete will not delete any rows.

  • answered 2018-01-13 17:07 Qirel

    You do not need to select anything before deleting any rows. Just use the same condition as you would selecting the rows.

    Run the delete query, and you can check the number of affected rows to see if anything was deleted or not, and if so, how many rows. This function returns and integer with the number of deleted rows for a DELETE query.

    mysqli_query($kapcs, "DELETE FROM hir_termek_kapcsolo WHERE hir_termek_kapcs_termek = '$id' ") or die("Delete error 2 - " . mysqli_error($kapcs));
    echo mysqli_affected_rows($kapcs)." rows deleted";
    

    WARNING
    You're already using an API that supports prepared statements with bounded variable input, you should utilize parameterized queries with placeholders (prepared statements) to protect your database against SQL-injection! Get started with mysqli::prepare() and mysqli_stmt::bind_param().