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 |