2017-03-13 32 views
0

下面的代码在我的本地服务器上运行时没有任何问题。但是,当我尝试在预期的服务器上运行它时,我的两个查询不起作用 - 它们不是INSERT,因为它们应该是这样。我标记了两个不支持评论的查询,其余的都有效。目标服务器在PHP 5.6.30-0 + deb8u1上运行。插入查询在服务器上不起作用

UPDATE:感谢aynber,我已经跟踪误差。这是第一个查询错误:准备好的声明\“editRecord \”不存在”我不明白为什么这个工程的本地服务器上,但不能在预期的一个

更新2:之间错误准备的语句和执行:语法错误处或附近\ “ON \” \ n线段3:

case "editRecord": 

$id = openPandoraBox(post("id")); 
$tutorAbsence = post("tutorAbsence"); 
$clientAbsence = post("clientAbsence"); 

if($tutorAbsence == "1") { 

    if(post("tutor") != "0") { 
     // ------------this query does not work.----------- 
     $absUpsSql = "INSERT INTO tutorabsence(id, tutorid, reason) 
         VALUES ($1, $2, $3) 
         ON CONFLICT (id) 
         DO UPDATE SET tutorid=$2, reason=$3"; 

     $absUpsPrep = pg_prepare($conn, 'editRecord', $absUpsSql); 
     $absUpsQry = pg_execute($conn, 'editRecord', 
           array($id, post("tutor"), post("tutorreason")) 
        ); 
    } else { 
     $tutorAbsence = "0"; 
    }; 
} else { 
    $absDelSql = "DELETE FROM tutorabsence WHERE id=$1"; 
    $absDelPrep = pg_prepare($conn, 'absDel', $absDelSql); 
    $absDelQry = pg_execute($conn, 'absDel', array($id)); 
}; 

if($clientAbsence == "1"){ 
    if(post("client") != "0") { 
     // ------------this query does not work.----------- 
     $absUpsSql = "INSERT INTO clientabsence(id, clientid, reason) 
         VALUES ($1, $2, $3) 
         ON CONFLICT (id) 
         DO UPDATE SET clientid=$2, reason=$3"; 

     $absUpsPrep = pg_prepare($conn, 'absUps', $absUpsSql); 
     $absUpsQry = pg_execute($conn, 'absUps', 
           array($id, post("client"), post("clientreason")) 
        ); 
    } else { 
     $clientAbsence = "0"; 
    }; 
} else { 
    $absDelSql = "DELETE FROM clientabsence WHERE id=$1"; 
    $absDelPrep = pg_prepare($conn, 'absDelOne', $absDelSql); 
    $absDelQry = pg_execute($conn, 'absDelOne', array($id)); 
}; 

$resultSql = "UPDATE appointments 
       SET hour=$1, tutorid=$2, 
        clientid=$3, purpose=$4, 
        tutornotshown=$5, clientnotshown=$6 
       WHERE appid=$7"; 
$resultPrep = pg_prepare($conn, 'resultSql', $resultSql); 
$result = pg_execute($conn, 'resultSql', 
        array(post('hour'), post("tutor"), post("client"), 
          post("purpose"), $tutorAbsence, $clientAbsence, $id 
        ) 
     ); 

echo json_encode(array("success" => 1)); 
break; 
+0

你检查了[错误](http://php.net/manual/en/function.pg-last-error.php)吗?什么是$ 1,$ 2和$ 3? – aynber

+0

我无法从控制台收到任何错误。我在跟踪错误时遇到了问题,我是一个初学者。 $ 1,$ 2,$ 3是准备好的陈述的占位符。 – Ahmet

+1

查看我发布的链接(点击单词错误),它会帮助你检查错误。 – aynber

回答

0

PostgreSQL低于9.5的Upsert太复杂了。我时间很短,所以我只会使用SELECT COUNT(*),如果是。

1

更新2:准备语句和执行之间的错误:在或接近\语法错误 “ON \” \ nLINE 3:

如果它适用于本地服务器,但不适用于生产服务器,则很可能它们不运行相同版本的PostgreSQL。 ON CONFLICT是使用PostgreSQL 9.5(https://www.postgresql.org/docs/9.5/static/sql-insert.html)发布的功能,它仍然相当新。

你应该运行在生产服务器上此查询检查出它使用的版本的PostgreSQL:

SELECT version(); 

您的服务器可能运行PostgreSQL的9.5或9.6,而生产服务器可能在较旧版本。