Click here to Skip to main content
15,881,248 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
The actual score restore the old value when submitted. How to get the correct value of actual score when submitted with sv1_score and sv2_score? Or do I have to create function ?


What I have tried:

case 'supervisor1':
	if ($_SESSION[AppName]['id'] == $dataAA['AA1']['id']){
				
				
		$mySQL = 'UPDATE assessment_performance SET supervisor1_general_remark = "'.$data['supervisor1_general_remark'].'" WHERE id ='.$data['id'];
		
		$myQuery = mysql_query($mySQL);
				
		//TABLE ASSESSMENT SECTION A
	   if (count($data['supervisor1_justification'] > 0)){	
			foreach ($data['supervisor1_justification'] as $key => $value){
				$isValid = false;
				$totalSV = 2;
						if (condition==1){
							$isValid = true;
						}
						else if (condition==2 ) { 
							$isValid = true;
							$totalSV = 1;
						}
					
						if ($isValid){
						
	//ACTUAL SCORE
	$mySQL2 = 'SELECT actual_score, supervisor1_score, supervisor2_score FROM assessment_performance_sectiona WHERE id ='.$key;
	$result2 = mysql_query($mySQL2);
		if ($result2){
			if (mysql_num_rows($result2) > 0){
				while($row2 = mysql_fetch_assoc($result2)){
					$data['actual_score'][$key] = ($row2['supervisor1_score'] + $row2['supervisor2_score']) / $totalSV;
				}
			}
		}
							
		$mySQL = 'UPDATE assessment_performance_sectiona SET 
		supervisor1_justification = "'.$value.'",
		supervisor1_score = "'.(array_key_exists($key, $data['supervisor1_score']) ? $data['supervisor1_score'][$key] : '').'",
		actual_score = "'.(float)(array_key_exists($key, $data['actual_score']) ? $data['actual_score'][$key] : '').'"
		WHERE id = '.$key;
		$myQuery = mysql_query($mySQL);
		}
						
	}
					
  } 
}
			
$_SESSION[AppName]['sessionmessage']['type'] = 'info';
$_SESSION[AppName]['sessionmessage']['message'][] = 'Action done.';
			
reDirect('15006-assessment-form.php);
			
break;
Posted
Updated 30-May-22 21:26pm
v2

1 solution

Don't do it like that! Never concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Always use Parameterized queries instead.

When you concatenate strings, you cause problems because SQL receives commands like:
SQL
SELECT * FROM MyTable WHERE StreetAddress = 'Baker's Wood'
The quote the user added terminates the string as far as SQL is concerned and you get problems. But it could be worse. If I come along and type this instead: "x';DROP TABLE MyTable;--" Then SQL receives a very different command:
SQL
SELECT * FROM MyTable WHERE StreetAddress = 'x';DROP TABLE MyTable;--'
Which SQL sees as three separate commands:
SQL
SELECT * FROM MyTable WHERE StreetAddress = 'x';
A perfectly valid SELECT
SQL
DROP TABLE MyTable;
A perfectly valid "delete the table" command
SQL
--'
And everything else is a comment.
So it does: selects any matching rows, deletes the table from the DB, and ignores anything else.

So ALWAYS use parameterized queries! Or be prepared to restore your DB from backup frequently. You do take backups regularly, don't you?

And when you do, the problem you have noticed will probably go away at the same time.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900