-1

With the following code, the vote value is incrementing & getting inserted to the table as a new row but I need the same cell to be updated after incrementing by checking the id posted. But I don't know how to do this. Please help me. My ultimate aim is to store the sum of votes, received for a particular name, in db by adding all votes to the same column.

<body>

    <form method="POST" id="Formid" name="formnm">
             <input type="hidden" id = "page" name="page"/>
        <div class="row m-b-2">
              
           <div class="col-sm-12">
              <div class="form-group col-sm-6">
                      
                  <h4 class="demo-sub-title">Who is your favourite author?</h4>

<input type="radio"  name="name"  id="name" value="Miguel de Cervantes" required="required"/>Miguel de Cervantes
<input type="radio"  name="name"  id="name" value="Charles Dickens" required="required"/>Charles Dickens
<input type="radio"  name="name"  id="name" value="J.R.R. Tolkien" required="required"/>J.R.R. Tolkien     
<input type="radio"  name="name"  id="name" value="Antoine de Saint-Exuper" required="required"/>Antoine de Saint-Exuper
                </div>  
           </div>  
<center> <input type="submit" name="submit" class="btn btn-primary" value="Submit"></center>
              
      </div>
         <button onclick="window.location='chart.php' ">View Poll Results</button>
  </form>

</body>
</html>

<?php
    
    if(isset($_POST["submit"]))
    {
        $name=$_POST['name'];

        $sql = "SELECT * FROM opinions ";
        $sl2=$db->prepare($sql);
        $sl2->execute();
        $res2=$sl2->fetch(PDO::FETCH_ASSOC);

        $vote=$res2['opinions_vote'];   
        $vote=$vote+1;

        $statement = $db->prepare('UPDATE opinions SET opinions_vote="$vote" WHERE opinions_author_name="$name"');

    $statement->execute();
        
    
            echo "Your response has been successfully recorded";
    

    }
?>

table

opinions_id opinions_author_name opinions_vote
1 Miguel de Cervantes 0
2 Charles Dickens 0
3 J.R.R. Tolkien 0
4 Antoine de Saint-Exuper 0
Miya Mand
  • 17
  • 3
  • It seems to me that you need to update your opinions table, instead of inserting into it. See https://www.w3schools.com/sql/sql_update.asp for reference – ChrisC May 03 '21 at 10:18
  • you need to write a update query like UPDATE opinions SET opinions_vote = "YOUR INCREMENTED VALUE " where opinions_author_name = "AUTHERE NAME FOR WHICH THIS VALUE IS". if you want to update overall Incremented value than just remove the where condition from update query. – RWS.Piyush May 03 '21 at 10:26
  • @ADyson i know it can be done with update query. But i want to get it updated without redirecting to any other page. – Miya Mand May 03 '21 at 10:28
  • @RWS.Piyush , I have tried like u said, but it's not working. echo $vote & echo $name results correct values however. i have updated the code. Can u please check? – Miya Mand May 03 '21 at 10:50
  • @RWS.Piyush , ur answer worked.Thanks – Miya Mand May 03 '21 at 11:06
  • But, May i know how can i prevent the echo statement that gets printed here when the page loads? I want to to be printed only when the update is done. – Miya Mand May 03 '21 at 11:24
  • 1
    `want to get it updated without redirecting to any other page.`..then why didn't you say that in the original question?? What you asked before doesn't even mention that requirement. If you want to run something on the server without redirecting or refreshing the page, you need to use AJAX. That's true for any server side operation, not just updating the database – ADyson May 03 '21 at 11:56
  • @ADyson, ok.i did it with the update query. Can u tell me how can i prevent my form from getting auto submitted when the page refreshed.?My datbase values are getting changed now every time when the page refreshed. – Miya Mand May 03 '21 at 12:07
  • There's nothing in the code you've shown which would make it automatically submit. Perhaps you have some JavaScript which you haven't shown us – ADyson May 03 '21 at 13:20
  • @ADyson, No. I haven't used any scripts here. And this page contains only this much code. Can u tell me how to prevent the name 'submit' from getting posted when the page loads...using any javascript or something.. – Miya Mand May 03 '21 at 17:56
  • 1
    The code you have shown does not cause the data to be automatically submitted. Someone needs to press the submit button before that can happen. What you're describing does not match the code you've provided, so it's very difficult to help you. – ADyson May 03 '21 at 18:07
  • 1
    Btw your code is not logical, it will not update the vote value correctly. You are taking the vote value from the first row in the table, and then updating a different row with that value plus one. It makes no sense. You don't even need the SELECT query in order to get the current votes. Just write `UPDATE opinions SET opinions_vote = opinions_vote + 1 WHERE opinions_author_name="$name"');` – ADyson May 03 '21 at 18:10

0 Answers0