【发布时间】:2021-07-06 14:41:46
【问题描述】:
我有 3 张桌子,我也在使用 wordpress:wp7s_g_bookings、wp7s_g_tests 和 wp7s_g_clients。这是一个研究项目,不卖。
我希望能够保存 1 个与 X 客户和 Y 测试的预订。
我读到了LAST_INSERT_ID(),但我认为它在这种情况下不起作用,我迷路了。
可能的情况:
- 您是合作伙伴,那么数据正在被拉取,所以我们有ID,因为它已经存在;
在这种情况下,我们将在
name="client_birth[]" ...等可重复字段中添加其他用户数据 //client_vat仅在合作伙伴不存在时才显示/存在。 - 你是一个普通的客户,在这种情况下你填写相同的客户数据加上
client_vat(所以我确定在添加到数据库时只有client_vat的人是booking_client)
if (!empty($_POST)) {
$wpdb->query('START TRANSACTION');
//All this fields are repeatable inputs, except client_vat that can only be the 1st record. I think it's missing a foreach
$default = array(
'client_name' => $_POST['client_name'],
'client_birth' => $_POST['client_birth'],
'client_sns' => $_POST['client_sns'],
'client_vat' => $_POST['client_vat'],
'client_city' => $_POST['client_city'],
'client_email' => $_POST['client_email'],
'client_phone' => $_POST['client_phone']
);
$item = shortcode_atts( $default, $_REQUEST );
$addClients = $wpdb->insert( 'wp7s_g_clients', $item );
$default = array(
'test_client' => ???, //ID of previous client that have client_vat filled [0]
);
$item = shortcode_atts( $default, $_REQUEST );
$addTests = $wpdb->insert( 'wp7s_g_tests', $item );
$collectDate = date('Y-m-d H:i:s', strtotime($_POST['booking_collect_date']));
$default = array(
'booking_status' => 1,
'booking_partner' => $_POST['booking_partner'], //ID of the partner, if not empty
'booking_client' => ???, //If partner don't exist -> ID of client that have client_vat filled
'booking_type' => $_POST['booking_type'],
'booking_clients' => ???, //Array of all IDs previously added in wp7s_g_clients table
'booking_city' => $_POST['booking_city'],
'booking_address' => $_POST['booking_address'],
'booking_collect_date' => $collectDate,
'booking_payment' => $_POST['booking_payment'],
'booking_amount' => $_POST['booking_amount'],
'booking_obs' => nl2br($_POST['booking_obs']),
);
$item = shortcode_atts( $default, $_REQUEST );
$addBookings = $wpdb->insert( 'wp7s_g_bookings', $item );
if($addClients && $addTests && $addBookings) {
$wpdb->query('COMMIT');
$msg = "Success";
wp_redirect( esc_url( get_page_link( 6 ) ) );
exit;
}
else {
$wpdb->query('ROLLBACK');
$msg = "Error";
}
}
我的问题是将此正确添加到数据库中的可重复字段并使用以前创建的 ID。 我试图解释一切并发表评论,以便更好地理解。
【问题讨论】:
-
booking_clients - 你真的不应该在一个字段中存储多个值。
-
我以另一种方式思考,但我没有找到..有什么建议吗?
-
在 SO 上查看 dollowing 问题:stackoverflow.com/questions/3653462/…