mardi 19 décembre 2017

updating checkbox value to database via Ajax php pdo for each array updating but facing facing error on second time

I was trying to publish but facing problem on second time

means when we published initially the values are updating properly in database but when we add new post and trying to to publish all the db values are updated to '0' from '1(prev value)'

<button class="btn btn-secondry" id="publish" >Publish</button>

$queryNum = $pdo->prepare("SELECT FROM news where intNewsType=0");

$postID = $row['intNewsId'];

<td><input type="checkbox" name="checkbox" class="checkbox" value="<?php echo $postID; ?>" /></td>

$(document).ready(function(){ $("#publish").click(function() { var data = [];
$('.checkbox').each(function(){ data[$(this).attr('value')]=$(this).prop('checked')? 1:0; });

$.ajax({
    type: "POST",
    url: "publish.php",
    data: {fields : data},
    success: function(){
        alert('Page published successfully!');


    }
});

});

`   });`

publish.php

$pdo = Database::connect(); $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); foreach($_POST['fields'] as $key=>$val)
{

$sql = $pdo->prepare("UPDATE news SET intNewsType ='$val' WHERE intNewsId ='$key'"); $sql->execute();

}




Aucun commentaire:

Enregistrer un commentaire