FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


a new mysql table...





ftv_flung
Okay, so I've got a user table, and I've got a page where users of a certain level can access a page. What I'm trying to do is have them vote on something, but put that in a new table. How should I go about doing this? The table will be empty until they vote, so it'll work with "INSERT INTO" in the MySQL part... but...

What I'd like to have is an IF statement for the page that either says "You voted *blah* on this poll" (when they have voted) or "Vote for this, this or that" if they haven't....


thanks for any help! Smile Very Happy
kv
Since you have user table, I am assuming the voting is only for authenticated users. In that case, it is very simple. Have a username (or user id) field in voting table. Before rendering voting page, do a select on voting table with that username/id (and voting id if you have multiple polls) to see if user has already voted.
something like

Code:

select count(*) from voting where username='$username' and voting_id='$votingid'


if count is > 0, then show "You have already voted". Else display voting form.
bernardnapoleon
Or you could store the IP address of the user, if they are not registered, though it is not very reliable.
Code:
$_SERVER['REMOTE_ADDR'];
rvec
better not use that, most users have a dynamic ip. And I don't think someone has a dynamic user account.
themit
You must use remote_addr or use a cookie. You could also just get the user to login to your site. These are really the only ways. Each has downfalls:
Remote addr doesn't work for dynamic addresses.
Cookies don't work on machines with cookies off, the user can delete the cookie and vote again etc.
Logging in makes annoyed users who are less likely to vote.

If you want coding ideas to implement any one of those just ask.
ftv_flung
Okay, sorry if I didn't properly explain this.

I have a user table, with a vote thing going already, working fine - already with what you suggested.

But what I'm trying to do is have a separate table for something different. It'll have mulitple items to vote on, like this...



It's just getting the PHP and MySQL stuff to work together alright. So it should be... if the user doesn't have a row in the new table, it should ask to perform the vote, otherwise, display what's been done Smile - I hope this is possible!

thanks,
ftv_flung
kv
Yes of course, it is possible. Just have 3 tables.

Table1
Code:

Users
--------
Username
userid
....



Table2
Code:

Polls
--------
Pollname
pollid
pollquestion
....



Table3
Code:

Voting
--------
userid
pollid
voted_option
....



That's it. When you create a poll, it goes into poll table. There can be as many polls as you want. So all the polls are listed in polls table. Voting is a table which holds poll-voting versus user. So you can see if a user has voted for a given poll or not by this query

Code:

SELECT * FROM voting WHERE userid='$currentuserid' and pollid='$currentpollid'


If it returns a row, the user has already voted. You can retrieve the voted option of that user from the result returned. If result is void, then user has yet to vote.
Related topics
mysql table keyword search
Export and Import Mysql Table
Has anyone installed Moodle successfully?
E-Cards with PHP & MySQL
HTTP AUTH with PHP and mySQL
A very good PHP MySQL Tutorial
Can't create mySQL table
how to selecting this from a mysql table ?
PHP/MySQL challenge, take it if you dare
Data Mining From tables and then insert into MySQL
cannot add data to mysql from PHP form
Subtrat values to a mysql table script
What to learn first?
Chatroom
Reply to topic    Frihost Forum Index -> Scripting -> Php and MySQL

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.