Jump to content

Need help with adding a value to a stored value in database


Recommended Posts

I am doing writing a script that updates an attribue in the database where the attribute needs to be updated by a value of 1.
eg. NoEnrolled = 12
when the script runs the number enrolled should say 13


This is the script that i am currently using but not getting it to update



<?php

  $query3 = "SELECT * FROM Class Where ClassId='".$ClassId."'";

  $result3 = mssql_query($query3, $db) or die ("Error with Query2");
         
  while($row2=mssql_fetch_array($result3)){
 
      $space = $row2['NoEnrolled'] + 1;
   
      $query5 = "UPDATE Class SET NoEnrolled =".$space." WHERE ClassId='".$ClassId."'";
      mssql_query($query5) or die ("Error with Query4");

 
  }//end..while
  ?>
You don't need query3. All you need is
[code]<?php
$query5 = "UPDATE Class SET NoEnrolled = NoEnrolled + 1 WHERE ClassId='".$ClassId."'";
mssql_query($query5) or die ("Error with Query5");
?>[/code]
Try this, and please use code tags [b][nobbc][code] [/code][/nobbc][/b] when posting your code...

[code]
<?php
// Select and execute the query
$query = "SELECT * FROM Class Where ClassId = '$ClassId'";
$result = mssql_query($query, $db) or die ("Error with Query:<br>\n$query<br>\n");
$row = mssql_fetch_array($result);

// Increase the count by 1
$row['NoEnrolled']++;

// Update the database with the new value
$query = "UPDATE Class SET NoEnrolled = '{$row['NoEnrolled']}' WHERE ClassId='$ClassId'";
$result = mssql_query($query) or die ("Error with Query:<br>\n$query<br>\n");
?>
[/code]

Regards
Huggie
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.