mysql - Voting system and PHP session is not working -


i developing voting system in php mysql, in user enter choice through radio button , go in database. on same page, percentage of user votes appears after clicking submit button.

but code not work more 1 time. please me sort out query.

further detail

in program use database table poll in make 2 columns named yes , no , set value 0. then, use 2 variables $i , $j updating value of yes or no in database on click submit button.

if "yes" selected $i increments value , using update query saved. fetch value both yes , no columns calculate percentage.

i set $_session['yes'] store result of percentage of voters voted "yes", , same variable $_session['no']. finally, fetch values database store in $i , $j next click, values not changing.

php code:

<?php     ob_start();     session_start();     $conn=mysql_connect('localhost','root','');     mysql_select_db('oop',$conn);       global $i,$j;       if(isset($_post['btnsav']) && isset($_post['vote']))       { if($_post['vote']=='yes')      {   $y=$i++;     $query="update poll set yes='$y'";     $res=mysql_query($query) or die(mysql_error());             if($res>0)             {                 $qry="select yes poll";                 $res=mysql_query($qry) or die(mysql_error());                 $r=mysql_fetch_array($res) or die(mysql_error());                 $k=$r[0];                  $qry="select no poll";                 $res=mysql_query($qry) or die(mysql_error());                 $r1=mysql_fetch_array($res) or die(mysql_error());                 $k1=$r1[0];                  $y=$k/($k+$k1);                 $y1=$y*100;                 $_session['yes']=$y1;               }           }                else            {              $n=$j++;                     $query="update poll set no='$n'";              $res=mysql_query($query) or die($query);              if($res>0)             {                  $qry="select no poll";                 $res=mysql_query($qry) or die(mysql_error());                 $r=mysql_fetch_array($res) or die(mysql_error());                 $k=$r[0];                  $qry="select yes poll";                 $res=mysql_query($qry) or die(mysql_error());                 $r1=mysql_fetch_array($res) or die(mysql_error());                 $k1=$r1[0];                  $y=$k/($k+$k1);                 $y1=$y*100;                 $_session['no']=$y1;            }             }        }                      $qry1="select yes poll";                 $res1=mysql_query($qry1) or die(mysql_error());                 $r1=mysql_fetch_array($res1) or die(mysql_error());                 $i=$r1[0];                   $qry2="select no poll";                 $res2=mysql_query($qry2) or die(mysql_error());                 $r2=mysql_fetch_array($res2) or die(mysql_error());                 $j=$r2[0];                    echo ceil($i);                    echo ceil($j);     ?> 

html code:

<html>     <head>         <style>             .main             {                 margin-top: 50px;             }         </style>     </head>     <body>         <div class="main">         <center>             <h2><font color="blue">vote india</font></h2>         <form method="post">             <tr>                 <td>ques: support rahul gandhi pm candidate??<br /></td>             </tr>             <tr>                 <td>yes<input type="radio" name="vote" value="yes" /><br />                 no<input type="radio" name="vote" value="no" /><br /></td>             </tr>             <tr>                 <input type="submit" name="btnsav" value="vote" /><br />             </tr>            <?php                 if(isset($_session['yes']))                 {                     echo ceil($_session['yes']);                 }                 elseif (isset($_session['no']))                  {                    echo 'non supportrs'.ceil($_session['no']);                 }                       ?>         </form>         </center>         </div>     </body> </html> 

php code:

<?php ob_end_flush(); session_destroy(); ?> 

there number of misunderstandings in code, , few things not wrong improvement.

firstly, there no need declare global $i,$j - outside of function or method in global context. wondering whether intend these variables "for users" i.e. every time clicks "yes", $i incremented, , every time clicks "no", $j incremented. unfortunately doesn't work - 0 every run of script (technically start off uninitialised, , when increment them emit "variable undefined" notice, , assume original value zero).

secondly, don't need sessions program, unless want remember on per-user user basis, , data not stored in database. far can tell thing need store on per-user basis vote, , since stored in database, there nothing else remember. (to fair, may wish remember user has voted, can thank them voting , disable voting buttons, weak protection against intent on voting more once).

it not clear using ob_start() , ob_flush() for. suggest removing them.

now, reason people found question hard understand data types of columns had not been made clear. normally, voting code stores row per voter, 1 can have some certainty votes not dishonest. mean yes , no booleans. however, i've worked out intend there 1 row, , these values integer counts.

in case, need update using current value. here how yes:

update poll set yes = yes + 1; 

as say, better redesign this, each voter's choice recorded - otherwise vote many times in order make preferred candidate appear more popular case.

lastly, general tips:

  • make indentation accurate. doesn't matter if use spaces or tabs, keep consistent. switch on invisible characters in editor if helps.
  • use meaningful variable names. don't use $qry , $res, use $query , $result. single letter names alright small loops, not here - use variables represent mean. not make performance saving having shorter variables.
  • when things don't work expected, run code , examine database. put in exit() commands in intermediate places , examine interim results. essence of debugging - work out problem is, , narrow down until find source of bad result.
  • when asking questions databases, offer create table sql in question, readers can see data structure, , show sample data too. have cleared lot of initial confusion.

Comments

Popular posts from this blog

php - regexp cyrillic filename not matches -

c# - OpenXML hanging while writing elements -

sql - Select Query has unexpected multiple records (MS Access) -