donglou8371
2015-01-27 17:20 阅读 277
已采纳

如何将单选按钮值保存和更新到数据库mysql中

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 />
            </div>
            <table>

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

                                <tr>
                                <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>
                                </tr>

                                <tr>
                                <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>
                                </tr>
                                <tr>
                                <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 />

                            </tbody>





            </table> 
        in result.php
        <?php 
    $connect =include('connection.php');
    if(isset($_POST['submit']))
    {
        if(isset($_POST['button'], $_POST['email']))
        {

        //mysqli_query($connect, "UPDATE poll SET 




        }
        else
        {
            echo"you did not select you choice";
        }

    }




    ?>
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

1条回答 默认 最新

  • 已采纳
    doudu6100 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!!

    if(isset($_POST['submit'])){
        if(isset($_POST['button'])&&isset($_POST['email'])){
            //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=?');
            $query->bind_param('s',$_POST['email']);
            $query->execute();
            $query->bind_result($count);
            $query->close();
            if($count>0)echo"You've already used this email address to vote!";
            else{
                $update=$DBH->prepare('INSERT INTO poll (email,vote) VALUES(?,?)';
                $update->bind_param('si',$_POST['email'],$_POST['button']);
                $update->execute();
                $update->close();
                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

    if(isset($_POST['button'])&&isset($_POST['email'])&&$_SESSION['alreadyVoted']!==true)
    

    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:

    $_SESSION['alreadyVoted']=true;
    

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

    点赞 评论 复制链接分享

相关推荐