Here's the problem:
I got a row (voters) in my sql database containing nicknames of people who already voted like this:
"Nickname1", "Nickname2", "Nickname3",
But, if I try to use this row in an array to check if the logged in user already voted:
$voters = array($data['voters']);
if (!in_array($_SESSION['user'], $voters)) { // count his vote // } else { echo 'already voted' };
It simply doesn't work. If the user already voted but try anyway, his vote is counted and he's added again in my voters row.
What's wrong with my code ? Thanks
Your code is wrong because you don't have to use a single row to store multiple voters but instead you should have multiple rows each to store a single voter. Learn about database normalization or your work will be hard and fruitless
Anyway, here's your solution
$voters = explode(",",$data['voters']);
if (!in_array('"'.$_SESSION['user'].'"', $voters)) { // do something... }
You need like a table-in-table system. For it, you need a new table. Like that:
|question_id|voter|
223 |nickname1
223 |nickname3
225 |nickname1
And thus you can get an array of the voters for a spesific question.
It seems that you are creating an array over an array.
if print_r($voters) shows something like
Array
(
[0] => "Nickname1", "Nickname2", "Nickname3"
)
try avoiding the array.
If not and $data['voters'] is a separated by commas string try using the explode function to generate an array
$voters = explode(",", $data['voters']);