Jump to content

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


Varma69

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.