php – 为什么我的pg_query更新方法会跳过行?

我只是一个使用PHP的初学者,在Postgesql上更是如此.我可以说我对PHP,json和sql的了解非常有限.

每当我从数据库创建或删除记录时,我都创建了一个更新方法来刷新我返回的数据.但有时,当我创建一个新记录时,新记录不会从PHP返回,直到我使用刷新功能两次.有时,当我删除记录时也会发生同样的情况,我会在一次点击后收到结果.这是随机发生的,有时是第一次点击,有时是第四次点击.

示例:假设我在表上有现有记录并添加了新记录.一次偶然的机会,它返回[[“6”,“1”,“Jason Smith”,“& id”,“89”],它应该返回[[“6”,“1”,“Jason Smith”] ,“& id”,“89”] [[“6”,“1”,“King Sczhult”,“& id”,“90”],但是当我通过浏览器检查数据库或刷新页面时,记录就在那里.

//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;
         };
    
    
};

解决方法:

这就是问题所在.看起来你正在使用jquery来调用这两个东西,并且你正在异步地执行它 – 这意味着它们几乎立即启动并且任何一个都可以先完成(竞争条件).当“A”赢得比赛时,你就是金牌……当B赢得比赛时,你必须再次刷新.
你需要的是调用“A”(更新查询)然后在它完成时调用“B”.

相关文章

项目需要,有个数据需要导入,拿到手一开始以为是mysql,结果...
本文小编为大家详细介绍“怎么查看PostgreSQL数据库中所有表...
错误现象问题原因这是在远程连接时pg_hba.conf文件没有配置正...
因本地资源有限,在公共测试环境搭建了PGsql环境,从数据库本...
wamp 环境 这个提示就是说你的版本低于10了。 先打印ph...
psycopg2.OperationalError: SSL SYSCALL error: EOF detect...