【发布时间】: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