My code updates all the records in my article table instead of updating by id. Here is my code. I'm very new to php so I will really appreciate any help at all. Thanks guys...
<div class="modal fade" id="active-id<?php echo $id;?>" tabindex="-1" role="dialog" aria-labelledby="myModalLabel8" aria-hidden="true">
<div class="modal-wrapper">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header bg-green">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<h4 class="modal-title" id="myModalLabel8">Article Title: <?php echo $article_title?></h4>
</div>
<div class="modal-body">
<p>Are you sure you want to activate this article?</p>
</div>
<div class="modal-footer">
<div class="btn-group">
<form method='post' role='form'>
<button type='submit' name='active_yes' class='btn btn-success'>Yes</button>
<button type='button' class='btn btn-success' data-dismiss='modal'>No</button>
</form>
</div>
</div>
<?php
if (isset($_POST['active_yes'])){
if ($post_active == "No") {
$sql = "UPDATE `articles` SET `post_active` = '1' WHERE `articles`.`id` = $id;";
if ($conn->query($sql) === TRUE) {
echo "<script type='text/javascript'>alert('Article has been activated successfully and is currently live on the website.')</script>";
} else {
echo "<script type='text/javascript'>alert('Cannot activate article now. Please Try Again Later!')</script>";
}
}
}
?>
</div>
</div>
</div>
</div>
$sql = "UPDATE articles SET post_active = '1' WHERE articles.id = {$id}";
That's a little problem with the query.
The php code is also wrong because when you submit the form the page tends to reload due to your code strategy. Therefore the $id is lost and the php segment will run without an $id so all data in the table will be updated.
First check to see what value is in the $id variable by using echo. It might be the reason why it is updating all since it might not be passed.
Also try editing your form to include your $id variable so that your php script can read it directly from the form;
<form method='post' role='form' action="#">
<input type="hidden" name="id" value="<?php echo $id; ?>">
<button type='submit' name='active_yes' class='btn btn-success'>Yes</button>
<button type='button' class='btn btn-success' data-dismiss='modal'>No</button>
</form>
And your $sql variable to;
$id = $_POST['id'];
$sql = "UPDATE `articles` SET `post_active` = '1' WHERE `articles`.`id` = $id";
Let me know if it helps or if you come across any problem.