I have searched 12 pages on here to find the solution without avail.

This is the situation.

I have a MySQL table as follows:


| id | username | CreditsLeft |

| 1 | testuser | 10 |

create table account
(
id int(11) not null auto_increment,
username text not null,
CreditsLeft smallint(6) not null,
)

The code I am trying to use is:

$MinusCredit = "1";
$DBTotalToMinusFrom = $rows[CreditsLeft];

$DBTotalToMinusFrom - $MinusCredit = $newcredits;

$query = "UPDATE account SET CreditsLeft='$newcredits' WHERE username='$username'";
$result = mysql_query($query, $connection);

When testuser submits an online form, the value in the CreditsLeft column for the particular user needs to be the value minus one and then update the table with the new information. Everytime the user submits the form, the value is minus one again.

EG:

submit form once
50 - 1 = 49
insert into table 49

etc...

However, when I try my code, regardless of what it has in the cell, the new value inserted is 0 (zero)

Please advise.

I just need to make sure that every time a user submits the form, it minuses one from that value and then updates with the new value.

Thanks

    Your equation is backwards. The assignment operator should be on the left.

    i.e.

    $newcredits = $DBTotalToMinusFrom - $MinusCredit;

      Originally posted by Kudose
      Your equation is backwards. The assignment operator should be on the left.

      i.e.

      $newcredits = $DBTotalToMinusFrom - $MinusCredit;

      [/B]

      I put 2 credits in the CreditsLeft cell, submitted the form and it went from 2 to -1. I have no clue why this is not working.

      $MinusOne = "1";
      $NewCredits = $CreditsLeft - $MinusOne;
      
      $query = "UPDATE account SET CreditsLeft='$NewCredits' WHERE username='$username'";
      $result = mysql_query($query, $connection);
      

        Can you post for detailed code?

          Originally posted by Kudose
          Can you post for detailed code?

          this is the complete code for the php file.

          <?
          
          //// ALL FIELDS MUST CONTAIN SOMETHING
          
          if($username=='' OR $SenderFirstName=='' OR $SenderLastName=='' OR $SenderAddress=='' OR $RecipientFirstName=='' OR 
          $RecipientLastName=='' OR $RecipientAddress=='' OR $LetterContent=='')
          {
           echo "All fields must be completed before you can send this form.";
          }
          elseif (!eregi("^[A-Za-z]+$", $SenderFirstName)) {
          echo "Senders first name field can only contain letters A-Z or a-z with no spaces.";
          }
          elseif (!eregi("^[A-Za-z]+$", $SenderLastName)) {
          echo "Senders last name field can only contain letters A-Z or a-z with no spaces.";
          }
          elseif (!eregi("^[A-Za-z]+$", $RecipientFirstName)) {
          echo "Recipients first name field can only contain letters A-Z or a-z with no spaces.";
          }
          elseif (!eregi("^[A-Za-z]+$", $RecipientLastName)) {
          echo "Recipients last name field can only contain letters A-Z or a-z with no spaces.";
          }
          
          //// IF THE FORM FIELD LABELED BrailleType IS EQUAL TO GRADE ONE BRAILLE
          //// THEN THE FORM FIELD LABELED LetterContent CAN ONLY HAVE 880 CHARACTERS MAXIMUM
          
          elseif($BrailleType=='Grade one Braille' && strlen($LetterContent) > 880) 
          {
          echo "One page of transcribed grade one Braille is equal to a maximum of 880 characters, including spaces.";
          }
          
          //// IF THE FORM FIELD LABELED BrailleType IS EQUAL TO GRADE TWO BRAILLE
          //// THEN THE FORM FIELD LABELED LetterContent CAN ONLY HAVE 1760 CHARACTERS MAXIMUM
          
          elseif($BrailleType=='Grade two Braille' && strlen($LetterContent) > 1760) 
          {
          echo "One page of transcribed grade two Braille is equal to a maximum of 1760 characters, including spaces.";
          }
          
          //// IF EVERYTHING FOLLOWS THE RULES, THEN SUBMIT 
          //// THE PAGE FOR REVIEW BY A CERTIFIED BRAILLE 
          //// TRANSCRIBER 
          
          else
          {
          include("../config.php");
          $connection = mysql_connect("$server", "paddy_transcript", "transcription");
          $db = mysql_select_db("paddy_transcription", $connection);
          $query = "INSERT INTO $transcribe_tbl (`username`,`SenderFirstName`,`SenderLastName`,`SenderAddress`,`RecipientFirstName`,`RecipientLastName`,`RecipientAddress`,`BrailleType`,`LetterContent`) VALUES ('$username','$SenderFirstName','$SenderLastName','$SenderAddress','$RecipientFirstName','$RecipientLastName','$RecipientAddress','$BrailleType','$LetterContent')";
          $result = mysql_query($query, $connection);
          
          ///THE CODE BELOW FOR THE SUBTRACTION IS NOT WORKING
          
          $MinusOne = "1";
          $NewCredits = $CreditsLeft - $MinusOne;
          
          $query = "UPDATE account SET CreditsLeft='$NewCredits' WHERE username='$username'";
          $result = mysql_query($query, $connection);
          
          echo "<H1>Confirmation!</H1>Hi $SenderFirstName $SenderLastName,<br><br>Thank you for giving us the opportunity to transcribe a letter for you.  We will send the letter free matter for the blind to $RecipientFirstName $RecipientLastName as soon as the letter is transcribed into $BrailleType.<br><br>Cordially,<br> Braille School Transcription Service.<br><a href=\"mailto:transcription@brailleschool.com\">transcription@brailleschool.com</a><hr>";
          }
          ?>
          

          table structure is as follows:

          CREATE TABLE account (
          id int(11) NOT NULL auto_increment,
          username text NOT NULL,
          CreditsLeft text NOT NULL,
          PRIMARY KEY (id)
          ) TYPE=MyISAM AUTO_INCREMENT=4 ;

            How about instead of:

            $MinusOne = "1";
            $NewCredits = $CreditsLeft - $MinusOne;
            
            $query = "UPDATE account SET CreditsLeft='$NewCredits' WHERE username='$username'"; 

            Using:

            $query = "UPDATE account SET CreditsLeft='CreditsLeft-1' WHERE username='$username'"; 

              Originally posted by Kudose
              How about instead of:

              $MinusOne = "1";
              $NewCredits = $CreditsLeft - $MinusOne;
              
              $query = "UPDATE account SET CreditsLeft='$NewCredits' WHERE username='$username'"; 

              Using:

              $query = "UPDATE account SET CreditsLeft='CreditsLeft-1' WHERE username='$username'"; 

              [/B]

              already tried that and it does CreditsLeft-1 regardless of what the initial value is.

              so if your initial value is 50, and you submit the form, the new value will be CreditsLeft-1 (regardless of what the initial value is)

                I see that you're adding an account, and then updating the account's credit record. Where do you create the account in the record table? Or is the $transcribe_tbl variable set to account?

                  Originally posted by Kudose
                  I see that you're adding an account, and then updating the account's credit record. Where do you create the account in the record table? Or is the $transcribe_tbl variable set to account?

                  $transcribe_tbl is the name of the table in the database (set in config.php)

                  I changed

                  $query = "UPDATE account SET CreditsLeft='CreditsLeft-1' WHERE username='$username'";
                  

                  to

                  $query = "UPDATE account SET CreditsLeft=CreditsLeft-1 WHERE username='$username'";
                  

                  and its now working

                    thanks so much for taking the time to assist. most appreciated.

                      Write a Reply...