php - 如何使用 PHP 将嵌套数据添加到 MySQL 表中
问题描述
我有 3 张桌子,我也在使用 wordpress wp7s_g_bookings
:wp7s_g_tests
和wp7s_g_clients
. 这是一个学习项目,不卖。
我希望能够通过 X 客户和 Y 测试保存 1 个预订。
我读过,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。我试图解释一切并发表评论,以便更好地理解。
解决方案
使用 $wpdb 执行查询后,您可以访问 $wpdb->insert_id 以获取最后插入的 id。您可以在wpdb的文档页面上找到更多详细信息
如果您需要更多 id,因为您有更多 $wpdb->insert ,您只需在每次插入后存储 $wpdb->insert_id ,例如:
...
$item = shortcode_atts( $default, $_REQUEST );
$addClients = $wpdb->insert( 'wp7s_g_clients', $item );
$clientId = $wpdb->insert_id;
....
$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 );
$testId = $wpdb->insert_id;
...
rest of your code
推荐阅读
- python - APIView的POST方法中的Django Rest Framework验证
- react-native - 通过 App 的后台服务 React Native 本地通知
- java - Spring Security:未调用自定义 UserDetailsService(使用 Auth0 身份验证)
- angular - 想要验证 Angular 表单,无论出现什么错误,按钮都会提交
- java - 从 Java 使用 IBM MQ
- django - Centos nginx + django + gunicorn 403 禁止
- php - 最新的 Laravel 和最新的 WAMP 问题
- camera - libuvc_ros 错误:无法启动 [libuvc_camera/camera_node] 类型的节点
- ios - 无法通过命令行在模拟器上安装 .app
- laravel-5 - Vue自定义v-model值从axios get请求获取后不更新值