简体   繁体   中英

Syntax errors when creating triggers

I know this question has been thrown here in various forms, but I have gone through them all and not able to get solution to my issue. I don't understand why I keep getting this language error on this script. I have approached it following two different examples I saw here but it seems to be giving me the same error over and over. Im really having a hard time getting this trigger function to work, please I will appreciate any positive help from the PostgreSQL gurus here.

   <?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>

The field tally.php script is as shown below

<?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();
?>

Both those statements are pretty garbled. You need to read the PL/PgSQL trigger documentation , including the examples given there, to get an idea of the syntax.

Essentially, you first:

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

then use that trigger function on a table:

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

Additionally, the trigger body in your code will never work. PostgreSQL doesn't support ON DUPLICATE KEY UPDATE . That's a MySQL-ism. You're looking for MERGE or UPSERT like behaviour, which is unfortunately rather difficult to do well in PostgreSQL. See this question .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM