0

I have a series of articles on my webpage. Each of these has a delete form with the same name ("article"). All of my POSTs are done through ajax and work fine! The one problem I have now is that only the last article gets deleted. The hidden input has a value of the row# in the database This is the output I get:

  'deletearticle' => 'article=31&article=27&article=21',

Here's my AJAX:

    $('.deleteForm').submit(function() {
    $.ajax({
        url: 'functions/check.php',
        type: 'POST',
        data: {
            deletearticle: $('.deleteForm').serialize()
        },
        success: function(data){
            window.location.reload(true);
        }
    });
    return false;
});

And here's my delete

    if (isset($_POST['deletearticle'])) {
    parse_str($_POST['deletearticle'], $deletearticle);
    $id = $deletearticle['article'];

    try {
        $stmt = $DB_con->prepare("DELETE FROM articles WHERE id = ".$id." ");
        $stmt->execute();
    } catch (PDOException $e) {
        echo "Fail: ".$e->getMessage();
    }
}

EDIT I do know about name=XXX[]. It just doesn't tell me which one in the array was picked by the user to delete.

0

1 Answer 1

3

You just need to use the article[] syntax as the form element name.

Then PHP automatically converts it to an array of paramaters.

Of course you need then to change your SQL to something like

WHERE articleId IN( ' . $articleIds . ' )

But read first about proper sql escaping http://php.net/manual/de/security.database.sql-injection.php

Sign up to request clarification or add additional context in comments.

1 Comment

I've tried using article[] but it's no good if I just get an array with a bunch of IDs. I need to know which one the user picked to delete. Also, if you're referring to using "$id" in the sql, I know I should use ":id" and bind it before execution. This is just for testing purposes.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.