donglou8371 2015-01-27 17:20
浏览 340
已采纳

如何将单选按钮值保存和更新到数据库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 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 :)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示