gurung
gurung

Reputation: 626

construct sql code to limit form submit

I allow people to submit images and I need to limit their submits to not more than 5 in a day. I just need your help to construct the sql code as rest of things are prepared. I would rather not use solutions based on sessions and cookies. IP based solution is not rock-solid either but for me it is okay and better than the other two. Just so you know the site is built on wordpress and I know nothing about mysql.

EDIT : I am already trying to construct the code below in EFFORTS UPDATE. Please take a look.


HERE IS WHAT I AM DOING : I already have a table in mysql for this function, named someprefix_limit_uploads. I will log the ip and date of the person who will use the form first time and save it to database on form submit, with help of hidden fields, example - <input type="hidden" name="uploader_ip" value="<?php echo $uploader_ip;?>"/>. Then I will insert the values to the table with something like below, since I am using wordpress so my code will look like so :

global $wpdb; 
    require_once('../../../wp-load.php');
    $upload_date      = strip_tags($_POST['upload_date']);
    $uploader_ip = strip_tags($_POST['uploader_ip']);
    $table_name = $wpdb->someprefix . "limit_uploads";
    $wpdb->insert( $table_name, array(
    'rec_upload_date' => $upload_date,
    'rec_uploader_ip' => $uploader_ip
    ));

HERE IS WHERE I AM STUCK : Now for the code in the page which contains the form, I need to wrap the form in if_statement. Basically the script must hide the form after 5 submits within 24 hour. Here is an example of what the final code may look like.

global $wpdb;   
$results = $wpdb->get_results("COUNT xyz xyz xyz xyz xyz xyz xyz xyz xyz  ");

if(//there are more than 5 submits from same ip on same day) {

echo "no more than 5 uploads allowed.";

} else {
<form id="upload-images" action="" method="POST"> 
   //contents of the form
</form>
} 

Bottomline: It must be evident by now that I need help with the second block of the code. As I am not even sure I should use COUNT or SELECT, if yes then how. I will appreciate a little help here, also please point out if there is something wrong in my approach.


EFFORTS UPDATE :

I figure the sql query has to be something like this but missing some elements :

SELECT COUNT(*) **missing_some_code_here** 
FROM someprefix_limit_uploads
WHERE rec_uploader_ip = $uploader_ip;
AND rec_upload_date = $upload_date;

and then in my page

if (**missing_some_code_here** > 5) {

    echo "no more than 5 uploads allowed.";
    return;
}

Upvotes: 0

Views: 152

Answers (2)

sven
sven

Reputation: 785

The Query need not to have anything else, just the count will suffice, Check your table name "someprefix_limit_uploads", don't just copy paste everything.

  global $wpdb; 
  $count = $wpdb->get_var("SELECT COUNT(*) FROM someprefix_limit_uploads WHERE rec_uploader_ip = $uploader_ip AND rec_upload_date = $upload_date;");
  var_dump($count);
  if($count > 5) {
     //rest of code goes here
   }

Upvotes: 1

sebapalus
sebapalus

Reputation: 556

@gurung you assign value from the database to the variable, so you can't use COUNT. Use this instead:

$count = $wpdb->get_var('SELECT COUNT(*)...');
if ($count > 5) {
    ...
}

Upvotes: 2

Related Questions