Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

224
Views
PHP code updating all records instead of updating by id

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">&times;</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>
over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

$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.

over 4 years ago · Santiago Trujillo Report

0

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.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!