Shelley
Shelley

Reputation: 11

PHP MySQLi Check if user voted if so other users can vote also

I have this working fine(I'm a relatively new coder) as long as there is 1 vote. How can I get other users to vote? code below:.

SELECT 
    b.id,
    b.image_1_file,
    b.your_name,
    b.your_votes,
    b.image_2_file,
    b.battling_with_name,
    b.battling_with_votes
FROM
    battles b left outer join
    votes v 
    on 
        b.id = v.battle_id 
where
    v.battle_id is null
order by 
    rand()
limit 1

and I tried:

SELECT 
    b.id,
    b.image_1_file,
    b.your_name,
    b.your_votes,
    b.image_2_file,
    b.battling_with_name,
    b.battling_with_votes
FROM 
    battles b
WHERE 
    b.id NOT IN (
        SELECT 
            v.battle_id
        FROM 
            votes v 
        WHERE
            v.voters_id != '$myid'
    ) 
order by
    rand()

of course the latter will only look for b.id

other users have an id variable: $my_id I don't want the member to see the image again after they have voted.

Thanks a lot for your help and have a blessed day!

My table structures are:

my images(to vote on) table:

id                  int(10) unsigned    NO  PRI     auto_increment  
image_1_file            varchar(40) NO              
image_2_file            varchar(40) NO              
your_name           varchar(50) NO              
your_votes          int(10) NO      0   
battlingwithname    varchar(15) NO              
battlingwithvotes   int(10) NO      0   

my votes(votes are stored) table:

id                  int(10) unsigned    NO  PRI     auto_increment  
battle_id           int(10) unsigned    NO              
vote_type           tinyint(1) unsigned NO              
voters_id           varchar(30)  

Upvotes: 0

Views: 123

Answers (1)

Zaynul Abadin Tuhin
Zaynul Abadin Tuhin

Reputation: 31993

just use not in operator, you have t ignore those id from projection who is already voted, so you have to pick up those id who voted and remove from projection by using not in note:another operator exist can also be used but i used not in my query

SELECT 
    b.id,
    b.image_1_file,
    b.your_name,
    b.your_votes,
    b.image_2_file,
    b.battling_with_name,
    b.battling_with_votes
FROM
    battles b 
where b.id not in( select v.battle_id from votes v)

Upvotes: 1

Related Questions