i have a piece of code that finds the sum of the credits of the courses that were chosen in the multiple drop down list. my code works fine when there is a value that was selected and i can easily assign the variable to the session variable. however i an having some problems when there was nothing that was selected from the multiple select drop down list. when nothing is selected the session variable is still storing the previous value when i want it to be storing zero
here is my code below

$q="SELECT sum(credits) FROM courses WHERE course_code='$val'";
							if ($result2=mysql_query($q))
									$count2= mysql_num_rows($result2);		
							$row = mysql_fetch_row($result2);
							$first=$row[0];
							echo $first;
							print_r($count2);
							$sum= $sum+$first;	
							echo "$count2";
							//$sum=0;
							
						$_SESSION['credit_total'] = $sum;

i tried UN setting the session variable but i was also having some trouble doing that since the $sum variable is just blank when nothing is selected. can some offer some assistance on how i can achieve this
thanks

Recommended Answers

All 3 Replies

Then you would need to do something like

if ($_POST['val']!='0' && $_POST['val']!=0) {
$q="SELECT sum(credits) FROM courses WHERE course_code='$val'";
if ($result2=mysql_query($q))
$count2= mysql_num_rows($result2);		
$row = mysql_fetch_row($result2);
$first=$row[0];
} else {
$first=0;
}

I don't know what you mean by this:

i tried UN setting the session variable but i was also having some trouble doing that since the $sum variable is just blank when nothing is selected.
thanks

It would be better if you skipped everything and unset the session variable if mysql_num_rows returned 0 but if you want to unset the session variable if $sum is blank then you could do this

if(trim($sum) == "")
{
      $_SESSION['credit_total'] = NULL;
      unset($_SESSION['credit_total']);
}
else
{
      $_SESSION['credit_total'] = $sum;
}

a way to avoid the error would be to check if it exists before unset() - also setting the value to NULL or FALSE.

if(isset($_SESSION)){
unset($_SESSION);
//$_SESSION = false;
//$_SESSION = NULL;
}

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.