douju7765 2013-11-06 06:34
浏览 30
已采纳

构造sql代码以限制表单提交

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;
}
  • 写回答

2条回答 默认 最新

  • dongxiang5879 2013-11-06 07:54
    关注

    @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) {
        ...
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?