How do I add a vote to my database in a form?

前端 未结 1 1650
后悔当初
后悔当初 2021-01-28 14:12

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 q

相关标签:
1条回答
  • 2021-01-28 14:35

    Do you get error messages?

    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?

    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;
    

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

    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.

    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.

    0 讨论(0)
提交回复
热议问题