2015-01-27 17:20
浏览 297


I am trying to save the result of the radio button voting into the database mysql. html is as follow while in the database i have one poll table in test database. 1. save the radio button result in the database on the top of already voted if the vote is 1 by clicking the second time it should be 2 email address is not important if only one user can vote one time maybe cookies please help

 foodID (int primary Auto increment)
    foodName (names of four food)
    foodRate(rating to be updated if a user clicked +1)
    email(email address)

        <form id="form_poll" action="includes/result.php" method="post">

            <div id="email">
           <label><span>Email : </span>  <input type="text" name="email" class="email" size="50px"  placeholder="Your Email here"  required></label><br />

                            <label class="rad"> <input type="radio" class="btn" name="button" value="1" alt=""><i><img src="images/burger.png" alt="bulls burger" id="image1">

                                <td ><label class="rad"> <input type="radio" class="btn" name="button" value="2" alt=""><i><img src="images/pizza.png" alt="pizza" id="image2"></i></label></td>

                                <td ><label class="rad"> <input type="radio" class="btn" name="button" value="3" alt=""><i><img src="images/bol.png" alt="pasta b" id="image2"></i></label></td>
                                <td ><label class="rad"> <input type="radio" class="btn" name="button" value="4" alt=""><i><img src="images/lasania.png" alt="pasta l" id="image2"></i></label></td>
                                </tr><br />


        in result.php
    $connect =include('connection.php');
        if(isset($_POST['button'], $_POST['email']))

        //mysqli_query($connect, "UPDATE poll SET 

            echo"you did not select you choice";


  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • doudu6100 2015-01-27 18:09

    Why not add the email address as well and then check if they've already voted using the email address? you could use cookies or sessions but they're a little more complicated for you at the moment :) Keep it simple!!

            //not sure what you're using to add to database but I use mysqli object
            //$DBH being your database handler
            $query=$DBH->prepare('SELECT COUNT(*) FROM poll WHERE email=?');
            if($count>0)echo"You've already used this email address to vote!";
                $update=$DBH->prepare('INSERT INTO poll (email,vote) VALUES(?,?)';
                echo"you have successfully voted! Thank you!";
        }elseif(!isset($_POST['email']))echo"No email address enter";
        elseif(!isset{$_POST['button']))echo"You've not selected a vote";

    you can always add a session to your script but bare in mind sessions only last about 20 minutes or whatever your server is set to.

    Just edit the second condition to


    Also! Add to the group of else if statements towards the end of the script another else statement

    elseif($_SESSION['alreadyVoted']===true)echo"You've already voted! :O using sessions";

    Also! Under 'echo"you have successfully voted! thank you!";' add this line:


    And ya done :d if you need any further help or for me to explain anything, let me know :)

    点赞 打赏 评论

相关推荐 更多相似问题