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

时间:2015-01-27 17:20:33

标签: php mysql

我试图将单选按钮投票的结果保存到数据库mysql中。 html如下所示,在数据库中,我在测试数据库中有一个轮询表。 1.将单选按钮结果保存在已经投票的顶部的数据库中如果投票是1则通过点击第二次它应该是2个电子邮件地址并不重要如果只有一个用户可以投票一次也许cookie请帮助

 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 个答案:

答案 0 :(得分:0)

为什么不添加电子邮件地址,然后检查他们是否已经使用电子邮件地址投票? 你可以使用cookies或会话但是现在它们对你来说有点复杂:)保持简单!

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";
}

您可以随时向您的脚本添加会话,但请记住会话只持续约20分钟或服务器设置的任何内容。

只需将第二个条件编辑为

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

另外!将另一个if语句添加到脚本末尾另一个语句

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

另外!在“echo"you have successfully voted! thank you!";”下添加以下行:

$_SESSION['alreadyVoted']=true;

你完成了:如果你需要任何进一步的帮助或者我可以解释一下,请告诉我:)