Jump to content

Limit number of entries into DB


Recommended Posts

I have a function on my website where users can enter their interests and the information is stored in a DB, this is all done via AJAX.

 

My insert script is the following

 

<?php require 'config.inc.php'; ?>

<!-- Verify if user exists for login -->
<?php
if(isset($_GET['useridint']) && isset($_GET['interest'])){

$url= mysql_real_escape_string($_GET['useridint']);
$sitename= mysql_real_escape_string($_GET['interest']);

$insertSite_sql = "INSERT INTO user_interests (user_id, interest) VALUES('{$url}' , '{$sitename}')";
$insertSite= mysql_query($insertSite_sql) or die(mysql_error());

echo $sitename;
} else { 
echo 'Error! Please fill all fileds!';
}
?>

 

Is it possible I can limit the number of entries into the DB each user can have? So user X can only enter 3 interests as opposed to hundreds?

 

Thanks in advance!

Link to comment
Share on other sites

Why are you using HTML comments to comment your PHP code? That means those comments will be sent to the user's browser. Bad idea.

 

Anyway, I modified the structure of your code. I didn't like the flow. I always do error conditinos first and the success condition at the end.

<?php

require 'config.inc.php';

//Verify if user exists for login
if(!isset($_GET['useridint']) || !isset($_GET['interest']))
{
    echo 'Error! Please fill all fileds!';
}
else
{
    //Parse user input
    $user_id  = mysql_real_escape_string(urldecode($_GET['useridint']));
    $interest = mysql_real_escape_string(urldecode($_GET['interest']));

    //Get current count of interests for user
    $query = "SELECT COUNT(*) FROM user_interests WHERE user_id = '{$user_id}'";
    $result = mysql_query($query) or die(mysql_error());
    if(mysql_result($result, 0)>2)
    {
        //There are at least three interests
        echo 'Error! Only three interests are allowed!';
    }
    else
    {
        //Perform the insert
        $query   = "INSERT INTO user_interests (user_id, interest) VALUES('{$user_id}' , '{$interest}')";
        mysql_query($query) or die(mysql_error());
        echo $interest;
    }
}

?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.