PDA

View Full Version : Help me fix this...


leon2u
01-13-2003, 01:48 PM
Hai I have made a form on my forum
http://www.inventorusers.nl/prijsvraag.php

But now i want that all users can only send once the form and if they do it a seconde that they will be redirected to a template.

I tried this programming but does not work...I have created a table called prijsvraag en this has a userid en voted column
I want to look in the database if a userid has already voted and if so then they get an already voted form. If not then they can send the from and their userid will be inserted in the database.
So what I need is that the current logged in user will be checked with the userid in the database that has already send the form.
For the total code look the attachment!

THIS IS THE CODE FOR CHECKING but does not work what is wrong???

$prijsvraaginfo=$DB_site->query("SELECT userid FROM prijsvraag WHERE gestemd=1");
while($array=$DB_site->fetch_array($prijsvraaginfo)){
$userids=$array['userid'];
}
if (in_array ($bbuserinfo[userid], $userids)){eval("dooutput(\"".gettemplate('contact_gestemd')."\");");}
else{

appalcore.org
01-14-2003, 07:39 AM
just a quick glance, but i think you may need to change the line that says

$userids = $array['userid']


to

$userids[] = $array['userid']


That way the $userids are actually an array.

leon2u
01-14-2003, 01:43 PM
It did the trick!!! Thanx but what does it exactly and why did the other not work? So what is the difference between $userid en $userid[]?

Thanx ....

appalcore.org
01-14-2003, 08:26 PM
$userid creates a variable..and then everytime you were cycling through the results, you were replacing the previous entry you put into it.

by using $userids[], you are creating an array, with $array['userid'] being pushed onto the end of the array.

You might also wanna think about rewriting your code. instead of getting all the userids and putting them in an array, just check the database against that userid, like
if ($voted = $DB_site->query_first("SELECT * FROM prijsvraag WHERE userid=$bbuserid")) {
// Display already voted code
} else {
// Display not voted code
}


This saves you the time of cycling through all the userids and simply checks to see if there's an entry in the database already for their userid.

hope that helps

-john

leon2u
01-14-2003, 08:41 PM
Well I am quite now to PHP and sometimes I use a hack from here and read code from the forum and the hack to understand what it exactly does. Well I try to retrace it...do you know a good site for PHP-coding?

I tried the above one also but did not work...but I think I understand it now....you can check the database directly by assigning a query to a variabel with in one result the logged in user is in the database then it is true or else false.

You could also use a fetch_array and then assing a variabele array like userid[] which fills an array of information out of the database...

This is correct or not?

Yours is much faster indeed but my database is very small for 150 users.

Thanx for your effort!

appalcore.org
01-14-2003, 08:49 PM
yeah, the idea you have is correct
;)

(the query_first function in the vB queries the database and automatically grabs the first result row as an array, so you skip the fetch_array step. this is especially handy when you're only expecting one result set, i.e., they voted or they didn't)

NTLDR
01-14-2003, 09:43 PM
Originally posted by appalcore.org
if ($voted = $DB_site->query_first("SELECT * FROM prijsvraag WHERE userid=$bbuserid")) {


You should use $bbuserinfo[userid] and not $bbuserid to be safe, if the user disables cookies then they can submit the form as many times as they want. Also if using $bbuserid a check should be done to see if its set, otherwise your get DB errors if it isn't.

leon2u
01-14-2003, 09:51 PM
$prijsvraaginfo=$DB_site->query("SELECT userid FROM prijsvraag WHERE gestemd=1");
while($array=$DB_site->fetch_array($prijsvraaginfo)){
$userids[]=$array['userid'];
}
if (in_array ($bbuserinfo[userid], $userids)){eval("dooutput(\"".gettemplate('contact_gestemd')."\");");}
else{

The above was the first solution taken so this should be better then?

NTLDR
01-15-2003, 12:57 PM
if ($bbuserinfo[userid]!=0) {
if ($voted = $DB_site->query_first("SELECT * FROM prijsvraag WHERE userid='$bbuserinfo[userid]'")) {
// Display already voted code
} else {
// Display not voted code
}
}

Would be the best method IMO.