Varma69 Posted October 19, 2006 Share Posted October 19, 2006 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 13This 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 ?> Quote Link to comment https://forums.phpfreaks.com/topic/24487-need-help-with-adding-a-value-to-a-stored-value-in-database/ Share on other sites More sharing options...
Barand Posted October 19, 2006 Share Posted October 19, 2006 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] Quote Link to comment https://forums.phpfreaks.com/topic/24487-need-help-with-adding-a-value-to-a-stored-value-in-database/#findComment-111567 Share on other sites More sharing options...
HuggieBear Posted October 19, 2006 Share Posted October 19, 2006 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]RegardsHuggie Quote Link to comment https://forums.phpfreaks.com/topic/24487-need-help-with-adding-a-value-to-a-stored-value-in-database/#findComment-111569 Share on other sites More sharing options...
Barand Posted October 19, 2006 Share Posted October 19, 2006 Sorry, I've edited my post to put in the code tags Quote Link to comment https://forums.phpfreaks.com/topic/24487-need-help-with-adding-a-value-to-a-stored-value-in-database/#findComment-111570 Share on other sites More sharing options...
Varma69 Posted October 20, 2006 Author Share Posted October 20, 2006 thanks guys am kinda new to this php and posting in forums so i should use the [code][\code] so you will know exactly where the code starts and ends going to try it out now thakns again[/code] Quote Link to comment https://forums.phpfreaks.com/topic/24487-need-help-with-adding-a-value-to-a-stored-value-in-database/#findComment-111815 Share on other sites More sharing options...
Varma69 Posted October 23, 2006 Author Share Posted October 23, 2006 Thanks Guys the code work perfectly...... Quote Link to comment https://forums.phpfreaks.com/topic/24487-need-help-with-adding-a-value-to-a-stored-value-in-database/#findComment-113090 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.