liamloveslearning Posted July 3, 2011 Share Posted July 3, 2011 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 https://forums.phpfreaks.com/topic/241009-limit-number-of-entries-into-db/ Share on other sites More sharing options...
Psycho Posted July 3, 2011 Share Posted July 3, 2011 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 https://forums.phpfreaks.com/topic/241009-limit-number-of-entries-into-db/#findComment-1237937 Share on other sites More sharing options...
liamloveslearning Posted July 3, 2011 Author Share Posted July 3, 2011 Brilliant, Thankyou Link to comment https://forums.phpfreaks.com/topic/241009-limit-number-of-entries-into-db/#findComment-1237953 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.