0

I have a simple form, and a MySQL table set up.

All I am trying to do is every time the form is submitted add 1 onto the selected row amount.

Table is as simple as:

    answer | amount
------------------------
    Yes    | 5
    No     | 12
    Maybe  | 1

And the form is:

<form action="submit.php" method="post">
Is this a cool pie chart?:
<input type="radio" name="group1" value="Yes"> Yes<br>
<input type="radio" name="group1" value="No"> No<br>
<input type="radio" name="group1" value="Maybe"> Maybe<br>
<input type="submit">
</form>

Then in submit.php I have the following so far, but doesn't seem to be working

 <?php
    $sql = "UPDATE results SET amount= amount + 1 where answer = ?";
    $stmt = mysqli_prepare($conn, $sql);
    mysqli_stmt_bind_param($stmt, "s", $selected_option);   

    if (isset($_POST['submit'])) {
         $selected_option = $_POST['group1'];
         if ($selected_option == 'Yes') {
                mysqli_stmt_execute($stmt);
          }else if ($selected_option == 'No') {
                mysqli_stmt_execute($stmt);
          } else if ($selected_option == 'Maybe')
             mysqli_stmt_execute($stmt);
    }
?>

So if someone answers yes the amount row for yes will increase to 6 etc etc.

I have managed to call the data and even add it into the Google Visualization API using tutorials, but seem to failing at this part.

Thanks in advance everyone.

3
  • Can we see your submit.php code? Commented Aug 6, 2013 at 10:37
  • what you want a mysql query? Commented Aug 6, 2013 at 10:37
  • I have made an edit to include the submit.php code so far, not even sure if i'm on the right path :/ Commented Aug 6, 2013 at 10:51

1 Answer 1

1

If you issue the query

UPDATE results SET amount = amount + 1

you will update the amount-value of every line in the table. So you have to specify the row with a WHERE-predicate:

UPDATE results SET amount = amount + 1 WHERE answer = ?

The value for ? should be the posted value of the group1-variable. In order to prevent SQL injection do this as a prepared statement ( see http://php.net/manual/de/pdo.prepared-statements.php )

Sign up to request clarification or add additional context in comments.

2 Comments

I have updated my code as shown above but doesn't actually update the table. Must be something minor now thats stopping it from working :/
Ok got it working! was purely the brackets wrapped around the amount = amount + 1 that stopped it... don't know why I had that! Thanks for your help

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.