插入SQL后更新

时间:2017-01-03 19:52:54

标签: php mysql wordpress

我的最终目标是在Dealers表中更新现有列(promo_ids),并在插入新促销后创建新的PK ID。我想保留已经设置的现有ID。这可能吗?如果是这样,正确方向上的任何一点都会很棒。我仍然是SQL语句的新手。这是我目前要添加新促销的代码。

public function add_new_promo() {
      global $wpdb;

      $result = $wpdb->query(
                  $wpdb->prepare(
                    "
                    INSERT INTO $wpdb->gdp_promos
                    (promo, short_desc, long_desc, rebate_url, legal_copy, image_id, promo_headline, promo_line1, promo_desc, linkback_url, share_image_id, dealer_ids)
                    VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)
                    ",
                    $this->promo_data['promo'],
                    $this->promo_data['short_desc'],
                    $this->promo_data['long_desc'],
                    $this->promo_data['rebate_url'],
                    $this->promo_data['legal_copy'],
                    $this->promo_data['image_id'],
                    $this->promo_data['promo_line1'],
                    $this->promo_data['promo_headline'],
                    $this->promo_data['promo_desc'],
                    $this->promo_data['linkback_url'],
                    $this->promo_data['share_image_id'],
                    $this->promo_data['dealer_ids']
                  )
                );
      if ( $result === false ) {
          $state = 'error';
          $msg = __( 'There was a problem saving the new promo details, please try again.', 'gdp' );
      } else {
          $_POST = array();
          $state = 'updated';
          $msg = __( "promo {$this->promo_data['promo']} successfully added.", 'gdp' );
      }
      add_settings_error ( 'add-promo', esc_attr( 'add-promo' ), $msg, $state );
    }

2 个答案:

答案 0 :(得分:0)

是的,它是通过在AFTER INSERT TRIGGER表(OR)上使用Promotion来将insertupdate语句包装在存储过程内的事务块中。如果LAST_INSERT_ID()列是ID列,您可以使用AUTO_INCREMENT获取新插入的PK ID列值。

有关详细信息,请参阅MySQL文档。

答案 1 :(得分:0)

在您的else块中,添加:

<form>