dozpv84422 2011-12-16 15:49
浏览 35
已采纳

UPDATE反映在DB中,但不反映在SELECT查询中

First time for me here.

I am using PHP, MySQL, JavaScript and running JQUERY and AJAX functions.

I have an anchor link that runs a function.

The function emptys a DIV tag and then fills/displays (SELECT query) a table of rows in the DIV with a is null where clause. Each row has a select box where the NULL data column would go. The data in the select box comes from a different table.

When the select box changes it immediately runs the ajax to UPDATE the table row with the new data in the select box.

When you click the link to run the function again (empty, select query and display based on null column) the row that was just updated to the DB shows up again based on the is null clause. Checking the DB table at this point shows that it is in fact not null and was UPDATEd properly the first go around.

The page is never refreshed during this process and never has to be. If I do refresh it shows the proper data without the BUG.

All your thoughts are greatly appreciated.

matt

function closeItemsBtn() { // called by a click function of a standard link/button
  $('#CloseItems').empty(); // remove all html from the DIV
  var newAppend = '';
  <?
  //[... connect to db ...]
  // select info from 2 different tables so it can be used in different locations if necessary
  // where the row has not been reviewed and therefore is null
  $query = "select * from nearmiss where reviewedId is null order by submitDate desc";
  $result = $db->query($query) or die($db->error);
  $query2 = "select * from hazardid where reviewedId is null order by submitDate desc";
  $result2 = $db->query($query2) or die($db->error);
  $num_rows = $result->num_rows;
  $num_rows2 = $result2->num_rows;
  // create html for the DIV tag. Creates a table in a DIV that collapses by clicking aCloseList.
  // each row is in tbody so it can be striped by a all purpose striping function
  // this part is the table header and opening div tags and link
  $newAppend = "<p id=\"closeList\"><a href=\"#\" id=\"aCloseList\">Show/Hide</a> {$num_rows} Near Misses requiring attention.</p><div id=\"closenearmiss\" style=\"display:none;\"><table class=\"closenearmisstable\"><tbody><tr><td>Date Submitted</td><td>Submitted By</td><td>Location</td><td>Reviewed By</td></tr><tr><td rowspan=\"2\">Type</td><td colspan=\"3\">Description / Observation</td></tr><tr><td colspan=\"3\">Action / Reinforcement</td></tr></tbody>";
  // update various foreign key information from other tables
  for ($i=0;$i<$num_rows;$i++) {
$row = $result->fetch_assoc();
$query3 = "select location from locations where locationId='{$row['locationId']}'";
$result3 = $db->query($query3);
$location = $result3->fetch_assoc();
$query3 = "select name from employees where employeeId='{$row['employeeId']}'";
$result3 = $db->query($query3);
$name = $result3->fetch_assoc();
      // here is the table itself with the select tag in the null column name=reviewed
$newAppend .= "<tbody><tr><td>{$row['submitDate']}</td><td>{$name['name']}</td><td>{$location['location']}</td><td><form name=\"nearmissreview\" action=\"\" method=\"\"><input type=\"hidden\" name=\"docId\" value=\"{$row['nearmissId']}\"><input type=\"hidden\" name=\"type\" value=\"nearmiss\"><select name=\"reviewed\"><option>Choose name to sign off</option></select></form></td></tr><tr><td rowspan=\"2\">Near Miss</td><td colspan=\"3\">{$row['description']}</td></tr><tr><td colspan=\"3\">{$row['action']}</td></tr></tbody>";
  }
$newAppend .= "</table></div>";

// this is the beginning of the second table same structure as first with collapsing but
      // different data
$newAppend .= "<p id=\"closeList\"><a href=\"#\" id=\"aCloseList\">Show/Hide</a> {$num_rows2} Hazard IDs requiring attention.</p><div id=\"closehazardid\" style=\"display:none;\"><table class=\"closehazardidtable\"><tbody><tr><td>Date Submitted</td><td>Submitted By</td><td>Location</td><td>Reviewed By</td></tr><tr><td rowspan=\"2\">Type</td><td colspan=\"3\">Description / Observation</td></tr><tr><td colspan=\"3\">Action / Reinforcement</td></tr></tbody>";
  for ($i=0;$i<$num_rows2;$i++) {
$row = $result2->fetch_assoc();
$query3 = "select location from locations where locationId='{$row['locationId']}'";
$result3 = $db->query($query3);
$location = $result3->fetch_assoc();
$query3 = "select name from employees where employeeId='{$row['employeeId']}'";
$result3 = $db->query($query3);
$name = $result3->fetch_assoc();
$newAppend .= "<tbody><tr><td>{$row['submitDate']}</td><td>{$name['name']}</td><td>{$location['location']}</td><td><form name=\"hazardidreview\" action=\"\" method=\"\"><input type=\"hidden\" name=\"docId\" value=\"{$row['hazardidId']}\"><input type=\"hidden\" name=\"type\" value=\"hazardid\"><select name=\"reviewed\"><option>Choose name to sign off</option></select></form></td></tr><tr><td rowspan=\"2\">Hazard ID</td><td colspan=\"3\">{$row['description']}</td></tr><tr><td colspan=\"3\">{$row['action']}</td></tr></tbody>";
   }
$newAppend .= "</table></div>";
echo "newAppend='{$newAppend}';";
$result->free();
$result2->free();
$result3->free();
$db->close();
?>
// put HTML of $newAppend php in the DIV
$('#CloseItems').append(newAppend);
// fill the select box with a variable set somewhere else in the code not displayed here
$('#CloseItems select[name=reviewed]').append(newAppendE);
stripePointsTable('.closenearmisstable tbody');
stripePointsTable('.closehazardidtable tbody');
// close list click options
$('#closeList > a').each(function() {
$(this).click(function() {
    if ($(this).parent().next().css('display')=='none') {
        $(this).parent().next().slideDown('slow');
    } else {
        $(this).parent().next().slideUp('fast');
    }


});
});
// select tag change function that calls ajax and displays a message in a DIV called header
$('#closenearmiss select').change(function() {
    function processDataClose(data, success) {
        if (success) {
            $('#header').prepend(data+"<br />");
            closeItemsBtn();
        } else {
            $('#header').prepend(data+"<br />");
        }
    }
    var formData = $(this).parent().serialize();
    $.post('processreviewsign.php',formData,processDataClose);
});
  • 写回答

1条回答 默认 最新

  • dounieqi6959 2011-12-16 17:46
    关注

    jQuery caches your ajax calls. This is likely why you see the correct result when you refresh the page, but not in subsequent calls via ajax.

    Try setting cache: false in your ajax call

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 在获取boss直聘的聊天的时候只能获取到前40条聊天数据
  • ¥20 关于URL获取的参数,无法执行二选一查询
  • ¥15 液位控制,当液位超过高限时常开触点59闭合,直到液位低于低限时,断开
  • ¥15 marlin编译错误,如何解决?
  • ¥15 有偿四位数,节约算法和扫描算法
  • ¥15 VUE项目怎么运行,系统打不开
  • ¥50 pointpillars等目标检测算法怎么融合注意力机制
  • ¥20 Vs code Mac系统 PHP Debug调试环境配置
  • ¥60 大一项目课,微信小程序
  • ¥15 求视频摘要youtube和ovp数据集