Jump to content


Photo

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


  • Please log in to reply
5 replies to this topic

#1 Varma69

Varma69
  • Members
  • PipPip
  • Member
  • 15 posts

Posted 19 October 2006 - 08:42 PM

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
  ?>

#2 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,021 posts

Posted 19 October 2006 - 09:48 PM

You don't need query3. All you need is
<?php
$query5 = "UPDATE Class SET NoEnrolled = NoEnrolled + 1 WHERE ClassId='".$ClassId."'";
mssql_query($query5) or die ("Error with Query5");
?>

If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#3 HuggieBear

HuggieBear
  • Members
  • PipPipPip
  • Advanced Member
  • 1,899 posts
  • LocationEngland, UK

Posted 19 October 2006 - 09:56 PM

Try this, and please use code tags [code] [/code] when posting your 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");
?>

Regards
Huggie
Advice to MySQL users: Get phpMyAdmin and test your queries work there first, take half the hassle out of diagnosis, also check the reserved words list.

Links: PHP Docs :: RegEx's :: MySQL :: DevGuru :: w3schools

#4 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,021 posts

Posted 19 October 2006 - 10:00 PM

Sorry, I've edited my post to put in the code tags
If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#5 Varma69

Varma69
  • Members
  • PipPip
  • Member
  • 15 posts

Posted 20 October 2006 - 01:18 PM

thanks guys
am kinda new to this php and posting in forums


so i should use the
[\code] so you will know exactly where the code starts and ends 


going to try it out now thakns again


#6 Varma69

Varma69
  • Members
  • PipPip
  • Member
  • 15 posts

Posted 23 October 2006 - 12:21 PM

Thanks Guys the code work perfectly......




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users