How to add amount in sql

I have managed to get the following to work using the switch command but how come, I can’t get the amount to be added with these codes…

<!DOCTYPE html>
<html>
<head>
	<title></title>
	<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>

<?php
session_start();


if(!isset($_SESSION['u_uid']) || !isset($_POST['submit'])) {
header("Location: update.php?subscription=notset");
exit ();
} else {
include_once 'includes/dbh.php';

$subscriptionplan = mysqli_real_escape_string($conn, $_POST['subscriptionplan']);
$subscriptionplan2 = mysqli_real_escape_string($conn, $_POST['subscriptionplan2']);
$subscriptionplan3 = mysqli_real_escape_string($conn, $_POST['subscriptionplan3']);
                                      

         $sql = "SELECT * FROM memberships WHERE user_uid = '".$_SESSION['u_uid']."'";
$result = mysqli_query($conn, $sql);
$resultCheck = mysqli_num_rows($result);
if ($resultCheck > 0) {
  while($row = mysqli_fetch_assoc($result)) {
   

// Select from the database

		// insert into plan A

$fees = 100;
$fees2 = 150;
$fees3 = 200;


      if ($row['subscriptionplan'] == '' && $subscriptionplan == 'Level 1') {
         $totalfees ='$fees';
      	 $sql = "UPDATE memberships
                 SET subscriptionplan = 'Level 1', fees = '$fees'
                 WHERE user_uid = '".$_SESSION['u_uid']."'

      	        ";
      	 mysqli_query($conn, $sql);
      	 header("Location: update.php?subscription=success");
      	 exit();

         } else {
         	 if ($row['subscriptionplan'] == '' && $subscriptionplan == 'Level 2') {
            
      	      $sql = "UPDATE memberships
                      SET subscriptionplan = 'Level 2', fees = '$fees2'
                      WHERE user_uid = '".$_SESSION['u_uid']."'

      	              ";
      	      mysqli_query($conn, $sql);
      	      header("Location: update.php?subscription=success");
      	      exit();

         } else {
         	  if ($row['subscriptionplan'] == '' && $subscriptionplan == 'Level 3') {
               $totalfees ='$fees3';
      	 $sql = "UPDATE memberships
                 SET subscriptionplan = 'Level 3', fees = '$fees'
                 WHERE user_uid = '".$_SESSION['u_uid']."'

      	        ";
      	 mysqli_query($conn, $sql);
      	 header("Location: update.php?subscription=success");
      	 exit();

         } else {
         	  if ($row['subscriptionplan2'] == '' && $subscriptionplan2 == 'Level 1') {
              $totalfees ='$fees2';
      	      $sql = "UPDATE memberships
                 SET subscriptionplan2 = 'Level 1', fees2 = '$fees2'
                 WHERE user_uid = '".$_SESSION['u_uid']."'

      	        ";
      	     mysqli_query($conn, $sql);
      	     header("Location: update.php?subscription=success");
      	     exit();

         } else {
         	if ($row['subscriptionplan2'] == '' && $subscriptionplan2 == 'Level 2') {
           
      	     $sql = "UPDATE memberships
                     SET subscriptionplan2 = 'Level 2', fees2 = '$fees2'
                    WHERE user_uid = '".$_SESSION['u_uid']."'

      	            ";
      	     mysqli_query($conn, $sql);
      	     header("Location: update.php?subscription=success");
      	     exit();

         } else {
         	   if ($row['subscriptionplan2'] == '' && $subscriptionplan2 == 'Level 3') {
              
      	       $sql = "UPDATE memberships
                      SET subscriptionplan2 = 'Level 3', fees2 = '$fees2'
                      WHERE user_uid = '".$_SESSION['u_uid']."'

      	              ";
      	     mysqli_query($conn, $sql);
      	     header("Location: update.php?subscription=success");
      	     exit();

         } else {
         	 if ($row['subscriptionplan3'] == '' && $subscriptionplan3 == 'Level 1') {
             
      	      $sql = "UPDATE memberships
                 SET subscriptionplan3 = 'Level 1', fees3 = '$fees3'
                 WHERE user_uid = '".$_SESSION['u_uid']."'

      	        ";
      	     mysqli_query($conn, $sql);
      	     header("Location: update.php?subscription=success");
      	     exit();

         } else {
         	 if ($row['subscriptionplan3'] == '' && $subscriptionplan3 == 'Level 2') {
             
      	     $sql = "UPDATE memberships
                 SET subscriptionplan3 = 'Level 2', fees3 = '$fees3'
                 WHERE user_uid = '".$_SESSION['u_uid']."'

      	        ";
      	     mysqli_query($conn, $sql);
      	    header("Location: update.php?subscription=success");
      	    exit();

         } else {
         	if ($row['subscriptionplan3'] == '' && $subscriptionplan3 == 'Level 3') {
           
      	    $sql = "UPDATE memberships
                 SET subscriptionplan3 = 'Level 3', fees3 = '$fees3'
                 WHERE user_uid = '".$_SESSION['u_uid']."'

      	        ";
      	   mysqli_query($conn, $sql);
  
                                    
      	   header("Location: update.php?subscription=success");
      	   exit();

         } else {

         	  header("Location: update.php?subscriptionplan=exists");
            exit();

            $sql = "SELECT * FROM memberships WHERE user_uid = '".$_SESSION['u_uid']."'";
         $result = mysqli_query($conn, $sql);
         $resultCheck = mysqli_num_rows($result);
         if ($resultCheck > 0) {
         while($row = mysqli_fetch_assoc($result)) {
          $totalfees = $row['totalfees'];
          $fees = $row['fees'];
          $fees2 = $row['fees2'];
          $fees3 = $row['fees3'];
          $totalfees = '$fees' + '$fees2' + '$fees3';

           $sql2 = "UPDATE memberships
                   SET totalfees = '$totalfees'
                   WHERE user_uid = '".$_SESSION['u_uid']."'

                   ";

          mysqli_query($conn, $sql2);


         }
     }
 }
}
}
}
}
}
}
}

}

}
}
}

I thought that the logic should be there in pulling out each individual amount and then adding it at the end…

Before I even start to answer:

  1. Multiple people have told you multiple times you should change your database schema. Why haven’t you changed it?

  2. Multiple people have told you several times to stop using mysqli_real_escape_string and use prepared queries instead. Why are you still using mysqli_real_escape_string?

  3. You’ve asked me if you could reuse the $sql variable and I recommended against it. Yet you’re still using it. I’m curious why.

I have no problem helping people, but if you get the same advise from multiple people and just plain ignore it I honestly wonder if you really want to learn this stuff or if you’re just trying to get us to code stuff for you. In which case I don’t want to help you anymore.

3 Likes

My apologies but i am slowly trying to understand how it works first before going to prepare statement. . I know how to use it but the database makes sense for what i am working on

Okay, but if you’re trying to understand, starting with the right thing makes a lot more sense than starting off with outdated techniques and then re-learn everything using proper techniques. Don’t you agree?

1 Like

What do you think these lines are going to do? Not what you want, I imagine.

$totalfees ='$fees';

and

     $totalfees = '$fees' + '$fees2' + '$fees3';

Although as there’s an exit() before the last one, it probably won’t matter.

On a similar theme, you call header() even though you’ve output HTML before you opened the PHP tags, so that won’t work either. More to the point, you call session_start() after you’ve sent browser output, too. Don’t you get an error message?

I can’t follow the complex logic of the code. If you add lots of debugging echo() statements, can’t you see how it tracks through the code and where / when it fails to update fees? But maybe the problem is that session_start() failing, which will lose the $_SESSION values that you rely on for the rest of the code. Maybe.

1 Like

Indeed. That is zero. Regardless of the value of any of those variables.

I think i got it… it should be an int not a string

Exactly. You can’t do maths with strings.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.