Stack Exchange
Deleting one row from the database using the ID
I have problems with deleting a record from the screen using MSQLI.
Here you can see the code i'm using.
<?php
        include_once("assets/classes/connection.php");
        include_once("assets/classes/article.class.php");
        while($test = $allArticles->fetch_assoc())
            {
                if($test['titel']=="")
                {
                    echo "<div class='zonderfoto'>";
                    echo "<h5>"."geen titel hier, aparte opmaak geslaagd" . "<br /></h1>";
                    echo "<p>" . $test['article'] . "</p>";

                    echo "<input type='submit' name='verwijderee' value='verwijder'>";

                    echo "</div>";
                }
                else
                {
                    echo "<div class='metfoto'>";
                    echo "<h1>".$test['titel'] . "<br /></h1>";
                    echo "<p>" . $test['article'] . "</p>";
                    echo "<form method='post' action=''>";
                    echo "<input type='submit' name='verwijder' value='verwijder'>";
                    echo "</form>";
                    echo "<h1>".$test['id']."</h1>";
                    echo "</div>";


                }
            }
        $vArticle = new Article;
        $vArticle -> Key = $test['id'];
        if (isset($_POST['verwijder']))
        {
            $vArticle -> deleteArticle();
            echo ("shit");
        }
        ?>

I'm using a while function to print all the DB records on the screen. The if function is just a function to give some design with css so nothing more. With the delete button i want to delete the record from the screen. With the $test['id'] variabele you receive the ID of the record in the DB
Ok here is my code from the class.
public function deleteArticle()
{
    include("connection.php");
    $sSql = "DELETE FROM tblArticles WHERE id = '".$this->m_sKey."'";

        if (!$mysqli -> query($sSql))
        {
            throw new Exception("Something went wrong");
        }
}

EDIT
There is something wrong with the key, i replaced the where statement with title ="", so i deleted one title in the database, and when i click on delete then, he delete one row, but this is not happening at runtime. so i click delete, one row deleted, BUT the content only dissapear with a page refresh. The solution is using ajax?

Syntax looks good you your query,
$query = "DELETE FROM <TABLENAME> WHERE <COLUMN>= '". $YOURVARIABLE ."'";

Can you print $this->m_sKey properly? if you echo it before
mysqli->query()?

@user1399238 no, i receive nothing, i tried to run this all on a server because i received no errors on local host. Now online, i receive this error
Fatal error: Call to undefined method Article::deleteArticle() in /var/www/vhosts/.../httpdocs/project/saved.php on line 69

You should always escape variables you put into a query, or use prepared statements (you're using mysqli, so I don't get why you are not using prepared statements). SQL Injection is not fun.

public function deleteArticle()
{
    include("connection.php");
    $sSql = "DELETE FROM tblArticles WHERE id = '".$this->m_sKey."'";
    $mysqli->query($sSql);
    $aff_rows = $mysqli->affected_rows;
    if ($aff_rows){
        print("Affected rows (DELETE): %d\n", $aff_rows);
    }
    else{
        print("Nothong Happend ?");
        if ($err = $mysqli->error){
              print("Error: %s\n", $err);
        }
    }
}


I used your code and i received, nothing happend, so maybe there is somehting wrong whith de keys?

Still vulnerable to SQL Injection.

@SarveshKumarSingh, it was a mistake with the " and ', but when i run the query, nothing happened. But that was because of the key, then i replaced the key with title = "" and something happened, but the content only disapears after the page refresh

Maybe i'm going to start a new topic because ajax is not this topic
