1

Sorry for my English ! I have a poll system with one question and 4 answers :

Answer_id   Question_id   answers     vote
   10           1           ball        1
   11           1           radio       3
   12           1           tv          5
   13           1           car         8

I have 4 input text to input the answers for users using the poll to vote:

<input type="text" name="ans1" id="ans1"/>
<input type="text" name="ans2" id="ans2"/>
<input type="text" name="ans3" id="ans3"/>
<input type="text" name="ans4" id="ans4"/>

When I want to Update the answers, How can I query ? Thanks for answer my questiong :D

2
  • 1
    what do you want to update ? how do you want to update ? and where do you want to update ? Commented Oct 25, 2012 at 5:23
  • +1 for properly formatting your first SO question! Commented Oct 25, 2012 at 6:13

2 Answers 2

1

You should have all the inputs along with a submit button inside the form tag. Also inside the form tag, have a hidden input which will contain the id of the question as value. When you click submit, it will post the all the inputs including the hidden input. In the processing page (form action page), get the value of the hidden input(you will need this in the where clause of your query). Then write an update query in the below way.

You definately dont want to update all the 4 answers but only one answer as every question will have only one answer. Assuming your table name is questions:

$ans1 = $_POST['ans1'];
$ans2 = $_POST['ans2'];
$ans3 = $_POST['ans3'];
$ans4 = $_POST['ans4'];

if(!empty($ans1))$answer = $ans1;
else if(!empty($ans2))$answer = $ans2;
else if(!empty($ans3))$answer = $ans3;
else if(!empty($ans4))$answer = $ans4;
$id = $_POST['id']; //This is coming from the hidden input box
$updateSQL = mysql_query("UPDATE questions SET answers = '$answer' WHERE Question_id= '$id');

Also it will be good if you can put a check if the user is not inputting all 4 answers through javascript. Are you sure you need an update query and not an insert query ? This can also be done through jquery (ajaxed way), but you didnt mention how you want to do it.

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

Comments

0

In the id of the input text field, rewrite it with the table's primary key (AnswerId)+sometext When you save or update the answer the query will be like:

update table set answers    =$$text where answerId =AnswerId(id of the input text)

Comments

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.