PHP counting and verifing

I'm trying to write a code that is triggered by an ajax script to either write a 0 or 1 to both "feature" or "active" column. The script does do the job but what I want it to do first is check to see if user's "membership_type" to limit the amount of "features" depending on the user's "membership_type" This is what I have so far and underneath is the ajax code. My code is not counting the amount of features in the table and limiting the amount based on the user's membership. Could someone please help me and let me know what I am doing wrong,

Thanks

<?php 
error_reporting(E_ALL); 
$dbhost = 'localhost';
$dbuser = '####';
$dbpass = '####';
$db_name="####_users"; // Database name 

$con = mysql_connect($dbhost,$dbuser,$dbpass);
	if (!$con)
 	{
 	 die('Could not connect: ' . mysql_error());
 	 }
$user = $_SESSION["jigowatt"]["username"];
$db = mysql_select_db("####_users", $con);
$sql = "SELECT * FROM login_users WHERE email='".$user."'";
$exe = mysql_query($sql) or die(mysql_error());
$result = mysql_fetch_assoc($exe);
$membership = $result['membership_type'];

if (isset($_GET['update'])) {
	if (isset($_GET['type']) && isset($_GET['id']) && isset($_GET['value'])) {

		if ($_GET['type'] == "feature" && $_GET['value'] == 1) {			
			$q = "SELECT * from units where feature = 1 OR 0";
			$count = mysql_query($q);
			$num = mysql_num_rows($count);			
			if ($membership == "Basic" && $num < 1)   {
				die("limit");
			}
			elseif ($membership == "Bronze" && $num < 2)   {
				die("limit");
			}
			elseif ($membership == "Silver" && $num < 3)   {
				die("limit");
			}
			elseif ($membership == "Gold" && $num < 4)   {
				die("limit");
			}
			elseif ($membership == "Platinum" && $num < 5)   {
				die("limit");
			}
		}

		$q = "UPDATE units SET ".$_GET['type']."=".$_GET['value']." WHERE id=" . $_GET['id'];
		$count = mysql_query($q);
		if ($count) {
			echo "1";
		}
		else {
			echo "0";
		}
	}
}

Open in new window


<script type="text/javascript">
	jQuery(document).ready(function(){
		jQuery(".readClick").click(function(){
			var ele = $(this);
			ele.after("<span class='waitingresponce'>Procesing...</span>");
			//alert("step 1");
			var type = ele.attr("data-type");
			var id = ele.attr("data-id");
			var value = (ele.is(':checked') ) ? "1" : "0";
			$.ajax({
				url : "feature_active_ajax_handler.php",
				data : {
					update : 1,
					type : type,
					value : value,
					id : id
				},
				success : function (data) {
					if (data == 'limit') {
						ele.next('span').remove();
						ele.after("<span class='successResponce'>Feature limit exceed...</span>");
						setTimeout(function () {
							ele.next('span').remove();
						},1000);
						return 0;	
					}
					ele.next('span').remove();
					ele.after("<span class='successResponce'>Updated...</span>");
					setTimeout(function () {
						ele.next('span').remove();
					},1000);
					//console.log(data);
				},
				error : function () {
					//console.log("error");
				}

			});
		});
	});
</script>

Open in new window

Genesis5150Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Ray PaseurCommented:
The first thing I see wrong is that you're using the obsolete MySQL extension.  You have to get off that; PHP is doing away with support for MySQL.  This article can help you.
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/PHP_Databases/A_11177-PHP-MySQL-Deprecated-as-of-PHP-5-5-0.html

The second thing you might want to address is the PHP session.  You're referring to $_SESSION on line 13, but the session has not been started yet, so you're probably getting an undefined variable.  Here is how to use the PHP session.
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_11909-PHP-Sessions-Simpler-Than-You-May-Think.html

The third thing worth understanding and correcting is that the script uses external variables in a query string without sanitizing them first.  This is a security exposure, and while security is a full-time, four year college major, there are some things that all PHP programmers, even those new to PHP, need to know about security.  The PHP web site has this page.  The page and all of its links are required reading for anyone who writes even one line of PHP code.
http://php.net/manual/en/security.php

Also, when you post questions at E-E, you're allowed to use more than one topic area.  This application spans multiple topics, so you might consider using JavaScript, jQuery, CSS topic areas, too.  I think overall the combination of the AJAX and PHP makes the question harder to understand and answer because you have two interacting systems.  On the client side you have the browser, using HTML, JavaScript, and CSS.  On the server side, you have the scripting engine using PHP and SQL.  You might make faster progress if you separate this into (at least) two different questions so you don't have to eat the elephant all in one bite!

If you're new to PHP, and from the use of a MySQL extension I would guess you are, this article can help you find some good "getting started" learning resources.
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_11769-And-by-the-way-I-am-new-to-PHP.html
Julian HansenCommented:
I am trying to understand your logic here

You are returning the number of results from your query and then checking based on a < (less than)

If number of rows is < 1 and membership type is basic - limit reached.
...
If number of rows is < 5 and membership is premium - limit reached.

Surely those < (less than's) should be > (greater than's) ?
Genesis5150Author Commented:
First off thanks for responding. I've tried > (greater than's) before and the result was still the same. It doesn't seem to check. What I posted was the latter. I also tried using <?php if (!isset($_SESSION)) session_start(); ?> on the top of the page just now and it's still not performing the task.
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

Dave BaldwinFixer of ProblemsCommented:
Don't use a conditional with session_start();  It is actually preventing it from working.  session_start() is designed and intended to be used at the top of Every page involved in a 'session'.  Just put this Always.
<?php session_start(); ?>

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Dave BaldwinFixer of ProblemsCommented:
From http://php.net/manual/en/function.session-start.php
session_start() creates a session or resumes the current one based on a session identifier passed via a GET or POST request, or passed via a cookie.
Slick812Commented:
greetings Genesis5150, , I looked at your PHP code work, and you have too many places in that, where you are not using workable code. I would suggest that you do NOT use the AJAX to begin getting this to work, and to debug this PHP in your browser (not ajax), just place the url as -
     "http://domain.com/feature_active_ajax_handler.php?update=1&type=feature&value=1&id=abc"
into the address bar of your browser. Be sure to change the  http://domain.com   to the actual URL path to your php script.

in your browser, you will likely see many PHP warnings and errors, that will help you to get this working


You HAVE TO have the -
   session_start();
no matter what, the session will NOT start without it,
 this -
   <?php if (!isset($_SESSION)) session_start(); ?>
is ineffective to get the existence of a session

and you might should add debug output to the important data spaces of code  like -
var_dump($_SESSION);
var_dump($result);

to see what and where your errors may be in the lines of code.

Once you get your PHP to have the database access you need, ,  and the output you need, then you can start working on using the AJAX with it. Your Ajax for javascript and the PHP could use some improvement I think.

also you have -
"SELECT * FROM login_users WHERE email='".$user."'"

maybe that should be -
"SELECT * FROM login_users WHERE user='".$user."'"

as the email and user are usually different things.
Genesis5150Author Commented:
Thanks for the advice everyone the issue was with <?php session_start(); ?> and not <?php if (!isset($_SESSION)) session_start(); ?>

Thanks everyone!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.