将删除的数据插入表中的触发器

时间:2016-09-21 12:06:34

标签: mysql triggers backup

我想创建一个将删除数据插入备份表的触发器,我已编写此代码,但它无法正常工作。当我创建此触发器时,MYSQL会抛出语法错误。我怎样才能得到预期的结果?任何帮助将不胜感激。

 BEGIN
          IF EXISTS (SELECT * 
        FROM information_schema.tables
        WHERE table_schema = 'jobportal' 
            AND table_name = 'dlt_jobs'
        LIMIT 1) THEN
            create table dlt_jobs (select *,now() as deleted_on from jobs where job_id=OLD.job_id) ;
          ELSE 
            insert into dlt_jobs (username) values ('something');
          END IF;

        END

1 个答案:

答案 0 :(得分:-1)

如果您正在删除或插入,只需添加另一行代码来执行此处的任务就是示例

<?php
 include_once '../dbconnect.php';
 $error = false;
 if ( isset($_POST['btn-signup']) ) {
   // clean user inputs to prevent sql injections
  $emails = trim($_POST['emails']);
  $emails = strip_tags($emails);
  $emails = htmlspecialchars($emails);
  //basic emails validation
  if ( !filter_var($emails,FILTER_VALIDATE_EMAIL) ) {
   $error = true;
   $emailsError = "Please enter valid emails address.";
  } else {
   // check emails exist or not
   $query = "SELECT emails FROM newsletter WHERE emails='$emails'";
   $result = mysql_query($query);
   $count = mysql_num_rows($result);
   if($count!=0){
    $error = true;
    $emailsError = "Provided emails is already in use.";
   }
  } 
  // if there's no error, continue to signup
  if( !$error ) {  
   $query = "INSERT INTO newsletter(emails) VALUES('$emails')";
   $res = mysql_query($query);   
   if ($res) {
    $errTyp = "success";
    $errMSG = "Successfully registered, you may login now";
    unset($emails);
   } else {
    $errTyp = "danger";
    $errMSG = "Something went wrong, try again later..."; 
   }  
  }
 }
?>