Forum Moderators: coopster

Message Too Old, No Replies

Mysql query

         

Snowman671

9:14 pm on Mar 4, 2011 (gmt 0)

10+ Year Member



Hello,

I have been searching all over for a answer as I am not the best at mysql .. I am building a form that a person would fill out but I want to restrict how many people can fill it out. I have tried all kind of things but I am completely lost now.

What I need to do is when the page loads it needs to check a number from a row within in a table in a specific field on a specific record if that number is less than say 50 i want to increase the number by 1 and then load the form.

If the number is more than 50 I want it to not load the form and load a different page.

I have the connection built to the mysql server and have the form all tested to save the info they fill out but I am completely lost as how to get it to check the number and then make decision.

Any help here is greatly appreciated.
Thanks In Advance

JAB Creations

4:44 pm on Mar 5, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



It sounds like you're trying to limit the number of people who fill out a survey in example then you just check the number of rows in the table with the survey...or if you're on a higher level check the number of rows for the survey's id.

So let's say you've got four surveys and you want to limit the number of people who have filled out survey number 4, just count the number of stored entries by each person who filled out the survey that is associated with the survey's number...

$query1 = 'SELECT count(id_survey) AS count FROM table_survey WHERE id_survey='."'4'";
$result1 = mysql_query($query1);

if ($result1)
{
$row1 = mysql_fetch_assoc($result1);

if ($row1['count']>50) {echo 'already have 50, do not display form'}
else {echo 'display form for this survey';}
}



- John

Snowman671

3:23 pm on Mar 7, 2011 (gmt 0)

10+ Year Member



Thanks for the reply.. I understand what you have here.

My issue is I will probably have someplace in the neighborhood of 2000 people trying to be the first one in and if I just count records then more than 50 could possibly fill it out before the actual record count gets to 50.

So I figured I would want to query a single field in a single table and then increment that field by 1 to streamline the process and guarantee the 50 people only. thought was that to query would take a few milliseconds and then the page would load

My thought pattern was as soon as they hit it would load the query and check that field for what the count was and if less than 50 then show the page with the form, the form will save to a different table once they are complete.

this way as long as they were within the first 50 they could actually complete the form without having to worry about the record getting written after there was 50 people already in the survey.

Maybe this is not the best method ? I already know for example that 2000 people my try to hit the same link at the same time. I figured if i can get the query to run as fast as possible then I could eliminate the chance of more than 50 getting the form.

JAB Creations

3:30 pm on Mar 7, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I'm not sure how you're storing your data because it doesn't seem like you're doing so in the database though I would highly recommend storing the data in the database. Trust me, once you get the hang of working with SQL you'll wonder how you ever managed otherwise.

You could ++ a number in the database though that would be a waste. Store the data from each survey in it's own row for your records and if you truly feel the need to limit the entries to 50 just count how many rows are in the SQL table. However I think in the vast majority of situations that more data is desirable in the long term as in example on tech news sites the first comments tend to be, "First!" while the more constructive comments tend to be drowned out.

- John

Snowman671

3:40 pm on Mar 7, 2011 (gmt 0)

10+ Year Member



lets see maybe I can explain better.

the site takes the first fifty people and they get $100 gift card. now imagine I have 2000 people trying to be first to that site at 3am in the morning when they are available.

I figured I would query my database in the first line and check my "Counter" table in the "total" field for what number we are at if it was less than 50 then i would increment it by 1 and then load the form. (because it will take them time to fill out I want to ensure the first 50 can do it).

once they fill the form out they hit submit then the form data would get written to my "form data" table un the records there.

I figured this would be easiest way


after your post I thought well maybe I will make then fill the form out first then check the count . because really who would know if they were in the first 50 or not ? The better part of that would be if they are not in the first 50 I still capture their information. maybe your idea is a better one.. Just don't tell them till after they fill out the form. and then can count data rows instead.

JAB Creations

4:03 pm on Mar 7, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Exactly, you have an invisible limit of 50...they may or may not know that publicly though they have no idea if they'll finish before others do. However that may reduce the quality of the information you collect hence why I would recommend collecting more than 50 surveys. To ensure quality I think if you still want speedy feedback you should require a minimum length of characters typed in the form fields and discard any that try using fillers just to be within the first 50 entries. You may hint that using filler may disqualify an entry, some people will just check off the first option for every answer in example. You can also probably record the amount of time via a PHP session variable to ensure that the time taken to fill out the form is at least a minimal amount of time. There are so many ways to detect fraud it's not even funny.

- John

jspeed

4:05 pm on Mar 7, 2011 (gmt 0)

10+ Year Member



it's more about what direction you want to take. if you want it to be about the first 50 to the site, or the first 50 that can type fast...

Snowman671

4:10 pm on Mar 7, 2011 (gmt 0)

10+ Year Member



John,

Thanks for the in site. My ultimate task is a simple one. They don't fill out a survey or anything the for m contains only Name address and email. I am doing some checking on the email field against dns query so if the email is not valid it tells them etc. I also do zip code checking and it must be a us address that is. there is a terms and conditions they have to put a check box in and will be incorporate captcha as well to stop the robots.

the page is set to do auto roll at random times and the actual page name changes to a generated string daily so users cant bookmark the form page and such.. I'm trying to be as secure as I can I am thinking of also moving it directly over to my SSL server to do a bit more but want to see how it goes at first.