【问题标题】:UPDATE reflected in DB but not SELECT queryUPDATE 反映在 DB 中但不是 SELECT 查询
【发布时间】:2011-12-16 15:49:34
【问题描述】:

我第一次来这里。

我正在使用 PHP、MySQL、JavaScript 并运行 JQUERY 和 AJAX 函数。

我有一个运行函数的锚链接。

该函数清空 DIV 标记,然后使用 is null where 子句填充/显示(SELECT 查询)DIV 中的行表。每一行都有一个选择框,NULL 数据列将位于其中。选择框中的数据来自不同的表。

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

当您单击链接再次运行该函数时(为空,选择查询并基于空列显示),刚刚更新到数据库的行基于 is null 子句再次显示。此时检查 DB 表表明它实际上不为空,并且在第一次运行时已正确更新。

在此过程中页面永远不会刷新,也不必刷新。如果我刷新它会显示没有错误的正确数据。

非常感谢您的所有想法。

马特

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

【问题讨论】:

  • 这在某种程度上几乎可以肯定是一个缓存问题。是否可以提供一些极简的代码 sn-ps 让我们更好地了解页面是如何运作的?
  • 能否请您发布一些代码。
  • @NicholasBostaph 我已更改标题以消除页面缓存无济于事。
  • 我对 JQuery 非常熟悉,希望问题出现在我更熟悉的领域。但我倾向于同意加藤的观点;根据代码和问题的描述,JQuery 似乎正在缓存数据(无论是否应该)。我会在那里集中精力。祝你好运,很抱歉我无法提供更多帮助。

标签: php javascript jquery mysql ajax


【解决方案1】:

jQuery 缓存你的 ajax 调用。这可能是您在刷新页面时看到正确结果的原因,但在后续通过 ajax 调用时却看不到。

尝试在您的ajax call 中设置cache: false

【讨论】:

  • link jquery 说当页面通过 post 获取时它不会缓存。
  • 我将 .post 更改为 .ajax 并将缓存设置为 false。可以这么说,并没有让任何事情变得更好。
  • 也许是您的数据库代码正在缓存。你在使用 ADODB 吗?
  • 我正在使用 mysqli 进行数据库连接
猜你喜欢
  • 2018-09-01
  • 2020-03-27
  • 1970-01-01
  • 2012-01-01
  • 1970-01-01
  • 2021-02-23
  • 1970-01-01
  • 2021-11-19
相关资源
最近更新 更多