0

我知道这个问题已经以各种形式在这里提出,但我已经经历了所有这些并且无法解决我的问题。我不明白为什么我在这个脚本上不断收到这种语言错误。我已经按照我在这里看到的两个不同的例子来接近它,但它似乎一遍又一遍地给我同样的错误。我真的很难让这个触发器功能工作,请在这里感谢 PostgreSQL 专家的任何积极帮助。

   <?php
      include ("connection.php");
    $query = 'select * from fieldtally order by pipeno asc'; 
$result = pg_query($db_handle,$query); 
while ($row = pg_fetch_row($result))
{
    // Creates Arrays to use in dropdowns
    $pipeno_array[] = $row[0];
   } 
 // This function creates dropdowns that can be used in your forms
function dropdown($field_name, $num){
    // Creates the Dropdown
    echo "<select name=\"".$field_name."\" id=\"".$field_name.$num."\"\n";
    echo "<option value=\"\"> --- Select --- </option>\n";
    // Chooses which array to use for Dropdown options
   global $pipeno_array;
     $name_array = ($field_name == 'pipeno') ? $pipeno_array : $wthick;
    // Creates the Dropdown options based off the array above
    foreach($name_array as $k){
        echo "<option value=\"$k\">$k</option> \n"; }
    // Ends the Dropdown
    echo "</select>\n";
}
?>

<html>
<head><title>UG Pipeline Fiedl Data Capture</title></head>
<body>
<form action="fieldtally.php" method="post">
<table width="800" cellpadding= "10" cellspacing="1" border="1">
<tr align="center" align="top">
<td align="center" colspan="1" rowspan="1" bgcolor="#64b1ff">
<h3>Input Field Tally Information</h3>
Select Wall Thickness:<select name="wthick" id="wthick">
   <option value=""> --Select-- </option> 
  <option value="9.80">  9.80  </option>
  <option value="13.50"> 13.50 </option>
  <option value="15.90"> 15.90 </option>
  </Select> 
Select Pipe No:<?php dropdown('pipeno', 1); ?>   Select Pipe Manufacturer:<select name="manufacturer" id="manufacturer">
 <!-- #7.1 -->
  <option value=""> --Select-- </option> 
  <option value="KWH Pipe">KWH Pipe </option>
  <option value="Atlas Tube">Atlas Tube</option>
  <option value="Imperial Steel">Imperial Steel</option>
  <option value="Lakeside">Lakeside</option>
  <option value="Spiralco">Spiralco</option>
  <option value="Muri metals">Muri metals</option>
  <option value="WestLake">WestLake</option>
  <option value="Westman Steel">Westman Steel</option>
     <option value="Lockerbie & Hole">Lockerbie & Hole</option>
      <option value="Nardei">Nardei</option>
  </Select><br /><br />  
Joint No: <input type="text" name="djointno"> Input measured Length: <input type="text" name="measuredlength"><br><br>
Input Serial No: <input type="text" name="serialno"><br><br> 
<input type="Submit" name="submit" value="Submit">
</td></tr></table></form><p></p>
</form>
</body>
</html>

字段tally.php脚本如下图

<?php
 $PGHOST = "localhost:25376";
 $PGDATABASE = "Pipeline";
 $PGUSER = "postgres";
 $PGPASSWORD = "Casa2009";
 $PGPORT = 5432;
 $db_handle = pg_connect("dbname=$PGDATABASE user=$PGUSER
 password=$PGPASSWORD");
 if ($db_handle) {
 echo 'Connection attempt succeeded.';
 } else {
 echo 'Connection attempt failed.';
 }
 $pipeno = pg_escape_string( $_POST['pipeno']);  
 $wthick = pg_escape_string($_POST['wthick']);
 $djointno = pg_escape_string($_POST['djointno']);
 $manufacturer = pg_escape_string($_POST['manufacturer']);
 $measuredlength = pg_escape_string($_POST['measuredlength']);
 $serialno = pg_escape_string($_POST['serialno']); 
  $query = "CREATE FUNCTION upsert (sql_update TEXT, sql_insert TEXT)
                 RETURNS VOID
                 LANGUAGE plpgsql
            AS $$
            BEGIN
               LOOP
                  EXECUTE sql_update;
                  IF FOUND THEN
                     RETURN;
                  END IF;
                  BEGIN
                     EXECUTE sql_insert;
                     RETURN;
                     EXCEPTION WHEN unique_violation THEN
                  END;
              END LOOP;
            END;
            $$";

   $result = pg_query($query);
 if (!$result) {
 $errormessage = pg_last_error();
 $message = "Error with query: " . $errormessage;
 }
 $message = sprintf ("These values were inserted into the pipeline database - %s %s %s %s %s %s",$wthick,$pipeno,$manufacturer,$djointno,$measuredlength,$serialno);
 pg_close();
?>
4

1 回答 1

1

这两种说法都相当乱码。您需要阅读PL/PgSQL 触发器文档,包括那里给出的示例,以了解语法。

本质上,您首先:

CREATE OR REPLACE FUNCTION func_name() RETURNS trigger AS $$
BEGIN
   -- Trigger body here
END;
$$ LANGUAGE plpgsql;

然后在表上使用该触发器功能:

CREATE TRIGGER trigger_name
BEFORE INSERT OR UPDATE OR DELETE ON tablename
FOR EACH STATEMENT EXECUTE PROCEDURE func_name();

此外,代码中的触发器主体将永远无法工作。PostgreSQL 不支持ON DUPLICATE KEY UPDATE. 那是 MySQL 主义。您正在寻找MERGEUPSERT喜欢行为,不幸的是,这在 PostgreSQL 中很难做好。看到这个问题

于 2012-10-31T12:37:30.293 回答