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 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥15 stable diffusion
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘