Why does my pg_query update method skip a line?

I am just starting with php, especially on PostgeSQL. I can say that my knowledge of php, json and sql is very limited.

I created an update method to update the returned data whenever I create or delete a record from the database. But sometimes, when I create a new record, the new record does not return with php until I use the update function twice. Sometimes it happens the same way when I delete a record, I get results with one click. This happens randomly, sometimes on the first click, sometimes on the 4th click.

Example: suppose I have an existing entry on the table and a new entry is added. By chance, he returned [["" 6 "," 1 "," Jason Smith "," & id "," 89 "] when he was to return [[" "6", "1", "Jason Smith", "& id "," 89 "] [[" 6 "," 1 "," King Sczhult "," & id "," 90 "], but when I check the database or refresh the page with a browser, there is a record.

//The function that calls php
function refresh() {
   $(".scell").html(""); 
   $("#holidayCell").html("");
  
var updateTable = {
                        
  semSchedule: $("#semesterselect").val(),
  operation: "updateTable"
}

  $.post( "scheduleengine.php", updateTable).done(function( response ) {
  if (response.val != 0){
  //This is where I decode returned table array 
  }else {
  };
     
 

                
});
 };
//This is the php switch-case.

switch($_POST["operation"]){
               
 case "updateTable":
             
  echo updateTableFunc(post("semSchedule"));
       
 break; 

//Update Function

function updateTableFunc($semID = null){
    
    
    
    $result = "";
   
    $scqrysql = "SELECT id, tutor, hour, day FROM schedule WHERE semester='$semID'";
    $scqry = pg_query($scqrysql) or die(pg_result_error());
        
    while ($row = pg_fetch_array($scqry)) {
                
    $scTutorID = $row['tutor'];
    $tqry = pg_query("SELECT id, tname, tsurname FROM tutors WHERE id='$scTutorID'") or die(pg_result_error()); 
    while($tutorrow=pg_fetch_array($tqry)){
                
    $scTutor = $tutorrow['tname'] . " " .$tutorrow['tsurname'];
                
                                          };
            
     $scHourRow = $row['hour'];
     $scDay = $row['day'];
     $scID = $row['id'];
                
     $scHour = substr($scHourRow, 2, 1);
     $scDayNameArray = array("Monday", "Tuesday", "Wednesday", "Thursday", "Friday");
     $scDayNumberArray = array("2", "3", "4", "5", "6");
     $scDayNameReplace = str_replace($scDayNameArray, $scDayNumberArray, $scDay);
            
     $resultArrays = array($scDayNameReplace, $scHour, $scTutor, "&id", $scID);
     $result[] = $resultArrays;
                

                                        };
    
     if(is_null($result)){
     $result = "";
     echo json_encode($result);
     }else{
                
     $json = json_encode($result);
     echo $json;
         };
    
    
};
Run code
+4
source share
1 answer

. , jquery , - , , ( ). "" , . , . "A" ( ), "B".

+1

Source: https://habr.com/ru/post/1668978/


All Articles