Add data in another table in db
-
Is it possible to write data to another table during registration? How can this be implemented?
-
You can try using the “um_registration_set_extra_data” action hook.
https://docs.ultimatemember.com/article/1235-umregistrationsetextradata
I can’t understand my mistake. I use that code to insert:
add_action( 'um_registration_set_extra_data', 'my_registration_set_extra_data', 10, 2 ); function my_registration_set_extra_data( $user_id, $args ) { global $wpdb; $table_name = $wpdb->prefix . "customers"; $fio = $_POST['first_name-151']; $phone = $_POST['phone_number-151']; $email = $_POST['user_email-151']; $partner = $_POST['partner']; $fio_resp = $_POST['responsible-151']; $inn = $_POST['innn-151']; $comp_name = $_POST['name-comp-151']; $address = $_POST['comp-adr-151']; $kpp = $_POST['kpp-151']; $add_date = date("m.d.y"); $wpdb->insert( 'wp_customers', array( 'fio' => $user_id, 'phone' => $phone, 'email' => $email, 'partner' => $partner, 'responsible' => $fio_resp, 'inn' => $inn, 'company_name' => $comp_name, 'address' => $address, 'kpp' => $kpp, 'add_date' => $add_date) ); }And new row not created in table. What am I doing wrong?
You must use the
$table_namein the DB Insert not'wp_customers'You should also sanitize the $_POST values.
https://developer.ww.wp.xz.cn/reference/classes/wpdb/insert/
https://developer.ww.wp.xz.cn/themes/theme-security/data-sanitization-escaping/
-
This reply was modified 3 years, 11 months ago by
missveronica.
Hey @prytkova
Please feel free to re-open this thread by changing the Topic Status to ‘Not Resolved’ if any other questions come up and we’d be happy to help. 🙂
Regards,
Hello. I use this code, but the data does not come to the table:
add_action( 'um_registration_set_extra_data', 'my_registration', 10, 2 ); function my_registration( $user_id, $args ) { global $wpdb; $table_name = $wpdb->prefix . "customers"; $fio = sanitize_text_field($_POST['first_name-151']); $phone =sanitize_text_field( $_POST['phone_number-151']); $email = sanitize_text_field($_POST['user_email-151']); $partner = sanitize_text_field($_POST['partner']); $fio_resp = sanitize_text_field($_POST['responsible-151']); $inn = sanitize_text_field($_POST['innn-151']); $comp_name = sanitize_text_field($_POST['name-comp-151']); $address = sanitize_text_field($_POST['comp-adr-151']); $kpp = sanitize_text_field($_POST['kpp-151']); $add_date = 55; $wpdb->insert( $table_name, array( 'id'=>$user_id, 'fio' => $fio, 'phone' => $phone, 'email' => $email, 'partner' => $partner, 'responsible' => $fio_resp, 'inn' => $inn, 'company_name' => $comp_name, 'address' => $address, 'kpp' => $kpp, 'add_date' => $add_date) ); }https://developer.ww.wp.xz.cn/reference/classes/wpdb/insert/
If omitted, all values in $data will be treated as strings unless otherwise specified in wpdb::$field_types.
$add_date = 55;might be a problem as this is an integer not a string
also user_id which you get as an integer and should be defined asbigint(20)in your customers table.Try this code snippet:
add_action( 'um_registration_set_extra_data', 'my_registration', 10, 2 ); function my_registration( $user_id, $args ) { global $wpdb; $table_name = $wpdb->prefix . "customers"; $fio = sanitize_text_field( $_POST['first_name-151']); $phone = sanitize_text_field( $_POST['phone_number-151']); $email = sanitize_text_field( $_POST['user_email-151']); $partner = sanitize_text_field( $_POST['partner']); $fio_resp = sanitize_text_field( $_POST['responsible-151']); $inn = sanitize_text_field( $_POST['innn-151']); $comp_name = sanitize_text_field( $_POST['name-comp-151']); $address = sanitize_text_field( $_POST['comp-adr-151']); $kpp = sanitize_text_field( $_POST['kpp-151']); $add_date = 55; $wpdb->insert( $table_name, array( 'id' => $user_id, 'fio' => $fio, 'phone' => $phone, 'email' => $email, 'partner' => $partner, 'responsible' => $fio_resp, 'inn' => $inn, 'company_name' => $comp_name, 'address' => $address, 'kpp' => $kpp, 'add_date' => $add_date ), array( '%d', '%s', '%s','%s', '%s', '%s', '%s', '%s', '%s', '%s', '%d' )); }Hi, if i use that code in my custom form it’s work. But with plugin registration form in hook that doesn’t work.
Maybe the problem is related to what the administrator registers and the filter is used:add_filter( 'um_registration_after_auto_login', '__return_false' );You must make the code snippet independent of the form-id if code should be used for another form.
Change using the form-id 151 in all the $_POST statements
$fio = sanitize_text_field( $_POST['first_name-151']);to
$fio = $args['submitted']['first_name'];$_POST['partner']might be an exception?The last option you sent doesn’t work either. We need the data from the registration form to get into the ‘customers’ table.
All registration fields are found in the array
$args['submitted']You can see this variable values in WP Users -> All Users
and the Info display for each user.Try this code snippet and replace 9999 with your second form’s ID
also correct the innn index which seems to be incorrect and will disturb the insert.add_action( 'um_registration_set_extra_data', 'my_registration', 10, 2 ); function my_registration( $user_id, $args ) { global $wpdb; if( is_array( $args ) && isset( $args['form_id'] ) && in_array( $args['form_id'], array( '151', '9999' ))) { if( is_array( $args['submitted'] )) { $add_date = 55; $wpdb->insert( $wpdb->prefix . "customers", array( 'id' => $user_id, 'fio' => $args['submitted']['first_name'], 'phone' => $args['submitted']['phone_number'], 'email' => $args['submitted']['user_email'], 'partner' => $args['submitted']['partner'], 'responsible' => $args['submitted']['responsible'], 'inn' => $args['submitted']['innn'], 'company_name' => $args['submitted']['name-comp'], 'address' => $args['submitted']['comp-adr'], 'kpp' => $args['submitted']['kpp'], 'add_date' => $add_date ), array( '%d', '%s', '%s','%s', '%s', '%s', '%s', '%s', '%s', '%s', '%d' )); } } } -
This reply was modified 3 years, 11 months ago by
The topic ‘Add data in another table in db’ is closed to new replies.