Php 插入SQL后更新
我的最终目标是使用插入新促销后创建的新PK ID更新经销商表中的现有列(促销ID)。我想保留已设置的现有ID。这可能吗?如果是这样的话,在正确的方向上的任何一点都是很好的。我对SQL语句还是相当陌生的。这是我目前必须添加新促销的代码Php 插入SQL后更新,php,mysql,wordpress,Php,Mysql,Wordpress,我的最终目标是使用插入新促销后创建的新PK ID更新经销商表中的现有列(促销ID)。我想保留已设置的现有ID。这可能吗?如果是这样的话,在正确的方向上的任何一点都是很好的。我对SQL语句还是相当陌生的。这是我目前必须添加新促销的代码 public function add_new_promo() { global $wpdb; $result = $wpdb->query( $wpdb->prepare(
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 );
}
是的,它是通过在
升级
表上使用插入后触发器
(或者通过将插入
和更新
语句包装在存储过程内的事务块中来实现的。如果ID
列是AUTO\u INCREMENT
列,则可以使用LAST\u INSERT\u ID()
有关这方面的更多信息,请参阅MySQL文档。在else块中,添加:
$new_pk_id = $wpdb->insert_id;
$update_stmt = "UPDATE Dealers SET promos_id = %d WHERE dealer_id = %d";
$wpdb->query($wpdb->prepare($update_stmt, array($new_pk_id, $specific_dealer_id));
这看起来会用新的促销ID更新经销商表中的每个经销商,同时删除所有现有的促销ID。