vendredi 1 juillet 2016

PHP change multiple values of database through checkboxes boolean

I am trying to make some sort of simple blogging platform and it has been going pretty well untill now, my page has a list of articles on it in a table it gets that list from my sql database and there is also a column wich states if the article is public or isnt. The problem is I cant get the writing of all these checkboxes as a boolean to work (if checked input 1 else 0). The part I think it goes wrong is

<?php
if (isset($_POST['submit'])) {
    try {
        $stmt = $db->prepare('SELECT postID FROM blog_posts') ;
        $idArray = $stmt->fetch();

        for($i = $idArray; $i > 0; $i--){
            if(document.getElementById($i).checked){
                $public = 1;
            } else {
                $public = 0;
            }

            try {
                $stmt = $db->prepare('UPDATE blog_posts SET public = :public WHERE postID = '$i) ;
                $stmt->execute(array(
                    ':public' => $public
                    ));
            }
        }
    }
?>The entire code can be found here on [Hastebin][1] Thanks in advance




Aucun commentaire:

Enregistrer un commentaire