如何以表格形式向我的数据库添加投票? [英] How do I add a vote to my database in a form?

查看:114
本文介绍了如何以表格形式向我的数据库添加投票?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,问题是我无法使数据库在投票页面上为其添加一个投票,或者在上一页中的某人单击提交对该问题的投票后,我的数据库无法对其进行投票.我已经尝试过在PHP代码中使用$vote = $vote + 1之类的东西,然后通过$command用SQL将其显示出来,但这是行不通的,而我尝试过的其他许多事情都像使变量= 1($ avote = 1)这样.我有点被困住了,有人告诉我我可以这样加1- $votes = $row['Votes' . $i] + 1;,然后我必须以某种方式显示它,但我无法弄清楚,这是我页面中的代码,用于检索信息并将其添加到数据库和页面中(我已经取出了不必要的HTML表标签以更清晰地显示):

alright,The problem is I can't get my database to add a vote to it on the votes page or my database after an individual on the previous page clicks submit for there vote to a question. I've tried things like $vote = $vote + 1 in my PHP code and then displaying it with SQL through $command but that doesn't work and many other things I've tried like making a variable = 1 ($avote = 1) so I'm kinda stuck, someone told me that I can just add 1 by doing this - $votes = $row['Votes' . $i] + 1; and then I'd have to display it under somehow but I couldn't figure it out, here's my code within my page to retrieve the information and add it to my database and page (I've taken out unneccesary HTML table tags for a clearer view):

        // if $row is false, then the pollid must have been wrong
        if ($row) {
            // display the poll
            echo '<h1>' . $row['Title'] . '</h1>';
            echo '<p><b>' . $row['Question'] . '</b></p>';

            for ($i = 1; $i <= 4; $i++) {
                $answer = $row['Answer' . $i];
                $votes = $row['Votes' . $i];
                $avote = 1;
                $command = "UPDATE FROM polls SET $answer WHERE $answer='$answer+$avote'";

                $stmt = $dbh->prepare($command);
                $stmt->execute(array($_SESSION['Votes']));

如果我的朋友对$votes = $row['Votes' . $i] + 1是正确的;有人可以让我知道如何在$command SQL代码中显示它吗: $command = "UPDATE FROM polls SET $answer WHERE $answer='$answer+$avote'";

If my friend is right about the $votes = $row['Votes' . $i] + 1; can someone please let me know how I'd display it within my $command SQL code: $command = "UPDATE FROM polls SET $answer WHERE $answer='$answer+$avote'";

请多谢您的见解.

推荐答案

是否收到错误消息?

听起来您正在尝试显示民意测验的结果(在用户提交自己的投票之后),但是您在检索民意测验的结果时遇到了麻烦.由于轮询结果需要在用户和会话之间持续存在,因此您必须将其存储在某个位置.我想这就是数据库中的$answer吗?

It sounds like you're trying to show the results of a poll (after the user submits their own vote), but you're having trouble retrieving the results of the poll. Since the poll results need to persist across users and sessions, you have to store it somewhere. I guess that's what $answer is in your database?

您的UPDATE查询有点中断.首先,您应该确保它在没有变量的情况下也能正常工作,我喜欢使用命令行客户端或诸如phpMyAdmin之类的图形工具.它可能看起来像:

Your UPDATE query is a bit broken. First, you should make sure it works properly without the variables, I like to go the the command line client or a graphical tool like phpMyAdmin. It might look more like:

UPDATE polls SET result = result + 1 WHERE poll_id = 1;

其中poll是表,而resultpoll_id是表中的列.

Where poll is your table and result and poll_id are columns in your table.

似乎您要向用户询问多个问题,因此每个问题都有不同的poll_id,并使用隐藏的表单字段获取poll_id的值.您现在似乎正在使用$row数组,但是它似乎很脆弱,并且随着您构建更多轮询选项而无法扩展(从技术上讲,它将扩展,但是您永远无法删除问题或摆脱旧的民意测验.

It appears as if you're trying to ask the user multiple questions, so you would have a different poll_id for each one, and use a hidden form field to get the value for the poll_id. You appear to be using the $row array for that now, but it seems fragile and it won't scale as you build more poll options (well, technically it will scale up, but you won't ever be able to remove a question or get rid of old polls.

您似乎正在使用PDO(因为面向对象的样式" mysqli execute带有void参数,并且与SQL Server通讯的PHP库似乎都不具有该确切语法),但是我希望您使用prepare语句对变量有问号,而不是直接替换.如果您不清楚正确的语法,请参阅 PHP手册了解详细信息,但是希望您在来这里之前已经经历过.

You seem to be using PDO (since the "object oriented style" mysqli execute takes a void parameter and none of the PHP libraries that speak to SQL Server seem to have that exact syntax), but then I would expect your prepare statement to have question marks for the variables rather than direct substitution. See the PHP manual for details if you're unclear about the proper syntax, but hopefully you've already been through that before coming here.

一旦您解决了这些问题,如果还有其他问题,应该可以更容易地跟踪发生的情况.

Once you get those issues cleaned up, if you have further problems it should be a bit easier to trace through what's going on.

这篇关于如何以表格形式向我的数据库添加投票?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆