Dear friends,
I am developin g online survey form. Please, suggest me i have a question.and three options for answer.for example-:
What is your favirite Country?
A-Pakistan
B-India
C-USA
now i put redio buttons against them.Users will come and vote for countries. and he can view result after vote that which country got more voteI just want to know how add +1 to to that country database record?
Answer form code is below-:

<h1 style="background: #97E2E1; width: 30%">Answer Section</h1>
<?PHP

 $sql="SELECT * FROM qtab ORDER BY Qid DESC Limit 1";
        $result=$conn->query($sql);

        if(!$result){
                  die($conn->error);
                  }
           While($row=$result->fetch_assoc()){


            echo "<BR>"."Question: ".$row["Question"]." ?"."<BR>"."<BR>";




 ?>
<form action="result.php" method="POST">

<P>
&nbsp;&nbsp;&nbsp;<?PHP echo $row['AnsA']; ?>:<input type="radio" name="q" value="a" /> <BR></br>
&nbsp;<?PHP echo $row['AnsB']; ?>:<input type="radio" name="q" value="b" /><BR></br>
&nbsp;&nbsp;<?PHP echo $row['AnsC']; ?>:<input type="radio" name="q" value="c" /> <BR></br>
&nbsp;&nbsp;&nbsp;<input type="submit" name="submit" value="Submit" />

 </p>
</form>
</body>
       <?php
              }
      $conn->close();
 ?>

</html>

and result.php code is below..

<?PHP include "conn.php";   ?>
<!DOCTYPE HTML>

<html>

<head>
  <title>Answer Section</title>
</head>

<body>




<?php
include "conn.php";
   if (isset($_POST['submiit'])&& isset($_POST['q'])){
   $answer=$_POST['q'];
   $ques="hello";
   $sql="UPDATE atab SET Question=$ques,$answer=$answer+1";
   $result=$conn->query($sql);
   if ($result){
   echo("user ahs voted");

   }
   else {
     echo("Error") ;
   }

            }


?>

awaiting ur promt reply...

Recommended Answers

All 14 Replies

I think your update query should be an insert query. What is the structure of your atab table?

My table structure is

// sql to create table
$sql = "CREATE TABLE Atab (
Qid INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
Question VARCHAR(50) NOT NULL,
AnsA INT(6) NOT NULL,
AnsB INT(6) NOT NULL,
AnsC INT(6) NOT NULL
)";

ok....

UPDATE atab SET $answer = $answer + 1 WHERE qid = $id

And reference the correct question id in your form. I assume that both qtab and atab are using the same id's for the same questions, and every question is already present in both tables.

Off-topic: am not a fan of constructing such queries, as they can be manipulated by changing the form (admittedly, not very useful in this particular case).

<?php
include "conn.php";
   if (isset($_POST['submiit'])&& isset($_POST['q'])){
   $answer=$_POST['q'];

      $sql1="SELECT Qid FROM qtab ORDER BY Qid DESC Limit 1";
        $lastid=$conn->query($sql1);
        if(!$lastid){
          die($conn->error);
        }
        else{
          while($row=$conn->query($sql1)>0){

        $sql="UPDATE atab SET $answer=$answer+1 WHERE Qid=$lastid";
   $result=$conn->query($sql);
   if ($result){
   echo("user ahs voted");

   }
   else {
     echo("Error") ;
   }

            }
     }

?>

Please check this code.... there is no any response in result.php page and data is also not transfered...yes you are right i also dont like these queries ,if you have anu other idea please, share with me.

Dear friend,
here i changed my code for result.php code is below

<?php
include "conn.php";

   if(isset($_GET['submit']) && isset($_GET['q'])){

   $answer=$_GET['q'];

      $sql="SELECT Qid FROM qtab ORDER BY Qid DESC Limit 1";
        $lastid=$conn->query($sql);
if(!$lastid){ echo( "error");}
else{
          while($row=$sql->num_rows($lastid)>0){

        $sqlu="UPDATE atab SET $answer=$answer+1 WHERE Qid=$lastid";
   $result=$conn->query($sqlu);
   if ($result){
   echo("user has voted");

   }
   else {
     echo("Error") ;
   }

            }
       }}

?>

but error comes on while($row=$sql->num_rows($lastid)>0)

( ! ) Fatal error: Call to a member function num_rows() on a non-object in
C:\wamp\www\Surv\result.php on line 14
please, check this code and suggest me ..
and how can change $answer in INT datatype bcz $answer=$_POST['q']; gives us the value of clicked redio bitton the value of clicked redion button is AnsA or AnsB orAnsC this is sring..how can we add +1 in string?

dear friends,
I solved to increasing numer in database of redio button,but i can not get last Qid from database to specify record.
code is here.

<?php
include "conn.php";

   if(isset($_GET['submit']) && isset($_GET['q'])){

   $answer=$_GET['q'];

      $sql="SELECT Qid FROM qtab ORDER BY Qid DESC Limit 1";
      $lastid=$conn->query($sql);
      $Lid=$sql->num_row($lastid);
if(!$lastid){ echo( "error");}
else{
        //if($row=($sql->num_rows()>1)){

   $sqlu="UPDATE atab SET $answer=$answer+1 WHERE Qid=$Lid";
   $result=$conn->query($sqlu);
   if ($result){ echo("user has voted");}
   else {
     echo("Error") ;
   }

            }
  }

?>

It would be easier if you stored the ID in a hidden input field in your question form, and pass it along to the update script.

thnaks alot friend,I got it,now issue is that if the same user votes again and again how prevent this condition?

You can't disallow this completely, unless they have to login. You can store something in a cookie though, but that can be deleted.

ok,i can ask user to enter his email address (which is unique) then vote.when user click any option afterenter his email address first system check his email address if there is email existed then he get error msg for again vote if not vote process will go ahead.how is that idea?

If you already have a logged in user, I suggest storing his vote in the database somewhere. That way he could change his vote if you allow it. You can just have a table that stores the question id, user id and the answer.

ok ,,thx for suggetion ....

dear friend,
why this code does not exicute and shows error...

<?php
include "conn1.php";

   if(isset($_GET['submit']) && isset($_GET['q'])){

   $answer=$_GET['q'];
   $hid=$_GET['hid'];
   $useremail=$_GET['useremail'];

       //if($row=($sql->num_rows()>1)){

   $sqlu="UPDATE atab
    SET email=$useremail,
    $answer=$answer+1
    WHERE Qid=$hid";
   $result=$conn->query($sqlu);
   if ($result){
       echo("user has voted");}
   else {
     echo("Error") ;
   }

            }


?>

does not transfer data to database .

All is going wel but Email address isnot get in $email
What is wrong in

$email=$_GET['email'];

???????????????????????????????
value of email textbox not transfer in $_GET['email']
please guide me?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.