【问题标题】:Why does my pg_query update method skips row?为什么我的 pg_query 更新方法会跳过行?
【发布时间】:2017-06-23 23:04:01
【问题描述】:

我只是 php 的初学者,在 PostgeSQL 上更是如此。我可以说我对php、json和sql的了解非常有限。

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

示例:假设我在表上有一条现有记录并添加了一条新记录。偶然地,它返回 [["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;
         };
    
    
};

【问题讨论】:

  • 调用这段代码的代码是什么?它可能是在做异步查询还是以某种方式使用另一个数据库连接?您可能想在运行此代码之前运行“提交”,看看会发生什么。
  • 我编辑了代码sn-p。
  • 也不是这样,我只是在更新代码之前和插入/删除查询之后都尝试了“提交”。
  • 如果您想展示您的解决方案,请在下面的回复帖子中进行。请不要损坏原始问题,因为这会使未来的读者更难(或不可能)弄清楚您最初的问题。我已经回滚到较早的版本。 (我有时会自己修理这些,但这个工作量太大了)。

标签: php json postgresql


【解决方案1】:

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

【讨论】:

  • 看来情况并非如此,如果这是您的意思,我从刷新功能中完全省略了“var updateHolidays”。变成了这样: script->php->updatefunc->echo json_encode($result)-> $(post(... 我仍然遇到随机跳过。我相应地编辑了代码 sn-p。
  • 我用这种方法解决了这个问题。这是jQuery。谢谢!
猜你喜欢
  • 2017-02-01
  • 1970-01-01
  • 2014-03-01
  • 2019-06-22
  • 2015-02-26
  • 2015-06-03
  • 2020-08-12
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多