Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

218
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda