Jump to content

[SOLVED] Php code suggestions


odisey

Recommended Posts

Here I have a snippet of code.  It is part of a form which sends it variable values of question number, score, score description.  One of the score options is NA (Not Applicable).  It has a score of "0" and a score description of "1."  The are 45 questions consturucted like this code.  I am coding a check into this code.  It checks for the number of NAs already in the db.  There can be a total of 3 or less for the year.  I am checking each question individually as the db is populated.  This way if questions 1, 2, 3, and 4 are all selected as NA on the submit - questions 1,2,3 will go to the db and 4 will return the error "A limit of 3 NAs." 

 

$sc1 = score for question 1.

$de1 = score description for question 1.

 

As you can see I am runnning a query and checking for the number of description_ids that = 1.  This is the count on NAs in the db.   

 

In this example if three NAs are entered and the user TRIES to enter a 4th NA the error returns accurately.  The problem is this.  If the user TRIES to enter a different score other than NA (Proficient for example - which has a score of 3 and a score description or description_id of 4) the NA error is ALWAYS returned, and not the accurate error of Record question 1.1.2 was not inserted. Record exists in data bank - use student edit page to alter this record.  Last - if this question has never before been entered -- it does enter accurately and the return message Record question 1.1.2 was inserted does print. 

 

 

        // Test for duplicate record. 

	if ($sc2 != 'n') {

	  $query = "SELECT student_id FROM student_score WHERE q_id = '$qu2' AND year = '$ye' AND mid_final = '$mf' AND student_id = '$s_id'"; 
	  $result1 = mysql_query($query);
	  if (mysql_num_rows($result1) == 0)  { // If no duplicate record add the new record.
	  
	  
	  	        // Test for # NAs. 

	  $query = "SELECT description_id FROM student_score WHERE description_id=1 AND year = '$ye' AND student_id = '$s_id'"; 
	  $result2na = mysql_query($query);
	  if (mysql_num_rows($result2na) <= 3)  { // If 3 or less NAs.
	  

            
	 $query = "INSERT INTO student_score (q_id, student_id, year, mid_final, score, description_id) VALUES ('$qu2', '$s_id', '$ye', '$mf', '$sc2', '$de2')";    

		            $result = mysql_query ($query) or trigger_error("Query: $query\n<br />MySQL Error: " . mysql_error());

					    if (mysql_affected_rows() == 1) { // If it ran OK.

						//Print message 
						   echo '<p><h3>Record question 1.1.2 was inserted.</h3></p>';


                       } 
				   
				   } else {   // If it did not run OK - duplicate record exists.
				   
				        //Print message 
						   echo '<p class="error"><font color="red">Record question 1.1.2 was not inserted. Record exists in data bank - use student edit page to alter this record.</font></p>'; 
						   
						   }
						   
						   
						   
							} else {

		   echo '<p class="error"><font color="red">Record question 1.1.2 was not inserted. There is a limit of 3 Not Applicable evaluations for a student during a given year. <b> This student has three for ' . $ye . ' already.</b> Please modify your evaluation form.</font></p>';    
		   
		   }				   
						   
						   
						   
						   
						   
 }  // END RECORD ENTRY





 

 

I been working on this site for days....  Any help here is welcome, appreciated...

 

Marc

Link to comment
Share on other sites

I've modified this code.  There are two parts here.  In the first code section is the working code to insert a new record.  In the second is the part I would like to add to the first - a check for number of NAs.  I cannot integrate it for some reason....  help is appreciated.

 

	      //BEGIN RECORD ENTRY   2
   
        // Test for duplicate record. 

	if ($sc2 != 'n') {   // BEGIN IF  OPEN 1   

	        $query = "SELECT student_id FROM student_score WHERE q_id = '$qu2' AND year = '$ye' AND mid_final = '$mf' AND student_id = '$s_id'"; 
	                   $result1 = mysql_query($query);
	                   if (mysql_num_rows($result1) == 0)  { // If no duplicate record add the new record. OPEN 2 Nest 1

                                    $query = "INSERT INTO student_score (q_id, student_id, year, mid_final, score, description_id) VALUES ('$qu2', '$s_id', '$ye', '$mf', '$sc2', '$de2')";    

                                           $result = mysql_query ($query) or trigger_error("Query: $query\n<br />MySQL Error: " . mysql_error());

                                           if (mysql_affected_rows() == 1) { // If it ran OK.  OPEN 4 Nest 3
																															                                            //Print message 
                                           echo '<p><h3>Record question 1.1.2 was inserted.</h3></p>';
						                        
                                            } 
		                          }			  
						   		   
				                     else {   // If it did not run OK - more than 3 NAs  OPEN 5 Nest 3
				   
				                          //Print message 
	   
	                                   echo '<p class="error"><font color="red">Record question 1.1.2 was not inserted. Record exists in data bank - use student edit page to alter this record.</font></p>'; 
	   							   
		                                 }					   						   							   
						   
                }  // END RECORD ENTRY

 

 

 

 


  
// Test for # NAs. 

    $query = "SELECT description_id FROM student_score WHERE description_id=1 AND year = '$ye' AND student_id = '$s_id'"; 
   $result2na = mysql_query($query);
    if (mysql_num_rows($result2na) <= 3)  {    // If 3 or less NAs.  OPEN 3 Nest 2

echo '<p class="error"><font color="red">Record question 1.1.2 was not inserted. There is a limit of 3 Not Applicable evaluations for a student during a given year. <b> This student has three for ' . $ye . ' already.</b> Please modify your evaluation form.</font></p>';  	  
		 		  }
		  



Link to comment
Share on other sites

Thanks for at least looking at the post....  I've finally solved it 98%,,,  one small glitch I should fix. 

 

Code that works for any that are interested:

 

      //BEGIN RECORD ENTRY   2
   
        // Test for duplicate record. 

	if ($sc2 != 'n') {   //  Check for score submission 

	//  Check for NAs --------------------------------------------------

        if ($de2 == '1')   {    // Begin check for NAs 	

		$query = "SELECT description_id FROM student_score WHERE description_id=1 AND year='$ye' AND student_id='$s_id'"; 
			$result2na = mysql_query($query);
			if (mysql_num_rows($result2na) >= 3)  { // If 3 or less NAs.  OPEN 3 Nest 2

	       echo '<p class="error"><font color="red">Record question 1.1.2 was not inserted. There is a limit of 3 Not Applicable evaluations for a student during a given year. <b> This student has three for ' . $ye . ' already.</b> Please modify your evaluation form.</font></p>';  	

			} else {

	 /// Check for NAs ---------------------------------------------------		


	        $query = "SELECT student_id FROM student_score WHERE q_id = '$qu2' AND year = '$ye' AND mid_final = '$mf' AND student_id = '$s_id'"; 
	                   $result1 = mysql_query($query);
	                   if (mysql_num_rows($result1) == 0)  { // If no duplicate record add the new record. OPEN 2 Nest 1

                                    $query = "INSERT INTO student_score (q_id, student_id, year, mid_final, score, description_id) VALUES ('$qu2', '$s_id', '$ye', '$mf', '$sc2', '$de2')";    

                                           $result = mysql_query ($query) or trigger_error("Query: $query\n<br />MySQL Error: " . mysql_error());

                                           if (mysql_affected_rows() == 1) { // If it ran OK.  OPEN 4 Nest 3
																															                                            //Print message 
                                           echo '<p><h3>Record question 1.1.2 was inserted.</h3></p>';
						                        
                                            } 

		                    }					   						   							   
						   
               }  // END RECORD ENTRY


	}  		// Close NA check

	 else {   // If it did not run OK 
				   
			  //Print message 
	   
	           echo '<p class="error"><font color="red">Record question 1.1.2 was not inserted. Record exists in data bank - use student edit page to alter this record.</font></p>'; 			

               }			

}  // Close last IF

Link to comment
Share on other sites

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.