Submit data to another db
-
Hello there,
I’ve been trying to catch submitted form data to post it into another sql db, but with no success.
I’ve already seen this, but since I’m not a professional I’m getting some troubles to make thing works.
I just want to take the form’s fields and transpose them into another sql, on the same server.
Little help?
Thanks in advance
-
Hi @desmogiec
I hope you are doing well.
You will need to use that hook + native PHP DB functions for that.
Here is a small example, using:
https://www.w3schools.com/php/php_mysql_insert.asp
https://www.php.net/manual/en/book.pdo.php
So let’s say you are trying to add a name field inside the table student.<?php add_action( 'forminator_custom_form_submit_before_set_fields', function( $entry, $form_id, $field_data_array ){ // Can be replaced using $entry variable, to find out // what is included in entry variable, use error_log( print_r($entry, true ) ); then check debug.log // This is a simple way $name = (string) $_POST['name-1']; $email = ( string ) $_POST['email-1']; $servername = "locahost"; $username = "userName"; $password = "password"; $dbname = "dbName"; //Connect to the DB https://www.w3schools.com/php/php_mysql_insert.asp, try { $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password); // set the PDO error mode to exception $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); $sql = "INSERT INTO Student (firstname, email ) VALUES (?,?)"; $stmt= $conn->prepare($sql); $stmt->execute([$name, $email]); } catch(PDOException $e) { // Add a validation in case of error } $conn = null; }, 10, 3);
I tested the above code and it worked fine, but note, it is only a simple example, I suggest checking PHP docs for the PDO and SQL docs in case you have doubts, extending the code would be out of our support scope.
Best Regards
Patrick FreitasThank you!
I’ve tried the code, but an error returns…
“An error occurred while processing the form. Please try again”
From your code I’ve added other vars from the form, but I’ve followed your code in order to add them.
<?php add_action( 'forminator_custom_form_submit_before_set_fields', function( $entry, $form_id, $field_data_array ){ $user = (string) $_POST['hidden-1']; $evento = (string) $_POST['text-2']; $hotlap = (string) $_POST['text-1']; $hotstint = (string) $_POST['text-3']; $discordID = (string) $_POST['hidden-5']; $datepost = (string) $_POST['hidden-3']; $timepost = (string) $_POST['hidden-4']; $servername = "locahost"; $username = "xxx"; $password = "xxx"; $dbname = "xxx"; //Connect to the DB https://www.w3schools.com/php/php_mysql_insert.asp, try { $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password); // set the PDO error mode to exception $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); $sql = "INSERT INTO Times (User, Event, HotLap, HotStint, Data, Time, DiscordID ) VALUES (?,?,?,?,?,?,?)"; $stmt = $conn->prepare($sql); $stmt->execute([$user, $evento, $hotlap, $hotstint, $datepost, $timepost, $discordID]); echo "New record created successfully"; } catch(PDOException $e) { echo $sql . "<br>" . $e->getMessage(); // Add a validation in case of error } $conn = null; }, 10, 3);
I know that maybe is a “basic php” issue, I’ve tried to read your link but I didn’t get where I’m getting stuck at…
I’ve checked inside my web configuration, pdo in enabled with its module pdo_mysql, so I don’t think it’s a server issue.
I’ve also doublechecked my table and it’s ok, user and password are ok too, I already use them in a nodejs app in order to access the db so it’s not an access problem.
Dumb question, I’ve created a mu-plugin php file where I’ve inserted this code, is that right…. right?
I’ve tried some changes, the error “An error occurred while processing the form. Please try again” was occurring cause of the “echo” I’ added in the code.
Once I removed them, the error didn’t show up anymore.
Still, the entries aren’t written in the second database.
So I’ve created a new table “Student”, with “firstname” “email” columns, and used the code as is which you provided, but even doing that didn’t work.
Any hint on what I should check server side?
I have no clues ??
Ok.
I just saw it.
In your code you wrote $servername = “locahost”.
Since I didn’t change that I didn’t notice until now..
Changed to locaLhost, now all is working like a charm.
Thanks for the help!
Much appreciated ??
Hi @desmogiec
Thanks for updates and sorry for late response!
Yes, that was a typo and I’m sorry about that. I’m glad you’ve already fixed that and that it’s working for you.
Have a great day,
AdamHello there!
I’m bringing up this discussion because I have a, I think, related issue.
I’ve added three “calculation” field, and I want to retrieve data from these.
So, I’ve added this in my php:$var1 = (string) $_POST['calculation-1']; $var2 = (string) $_POST['calculation-2']; $var3 = (string) $_POST['calculation-3'];
But it returns this
[03-May-2023 19:31:38 UTC] PHP Warning: Undefined array key "calculation-1" in /home/xkchthzl/public_html/wp-content/mu-plugins/form_to_mysql.php on line 121 [03-May-2023 19:31:38 UTC] PHP Warning: Undefined array key "calculation-2" in /home/xkchthzl/public_html/wp-content/mu-plugins/form_to_mysql.php on line 125 [03-May-2023 19:31:38 UTC] PHP Warning: Undefined array key "calculation-3" in /home/xkchthzl/public_html/wp-content/mu-plugins/form_to_mysql.php on line 129
What’s happening here?
- This reply was modified 1 year, 6 months ago by desmogiec.
Hi @desmogiec
I hope you’re fine today!
It is actually expected as calculation field is handled in a bit different way. in fact, there’s also a different way to fetch the fields and, actually, more secure than just reading $_POST data.
You would need to add just a slight modification to the code that you already have and instead of entire block where you set your variables from form data, so instead of that part
$user = (string) $_POST['hidden-1']; $evento = (string) $_POST['text-2']; $hotlap = (string) $_POST['text-1']; $hotstint = (string) $_POST['text-3']; $discordID = (string) $_POST['hidden-5']; $datepost = (string) $_POST['hidden-3']; $timepost = (string) $_POST['hidden-4'];
you would need to use this
foreach ( $field_data_array as $key=>$field ) { if ( $field['name'] == 'hidden-1' ) { $user = $field['value']; } if ( $field['name'] == 'text-2' ) { $evento = $field['value']; } if ($field['name'] == 'calculation-1' ) { $var1 = $field['value']; } }
I only included three fields example here but I think you get the idea. Basically, for each fields that you want to add to your custom variables, you just need to add this part inside the “foreach” loop
if ($field['name'] == 'calculation-1' ) { $var1 = $field['value']; }
replacing calculation-1 with your field ID and $var1 with your custom variable name.
This is because all the field data is already passed over to the hook (so to your function) and there’s no need to read them from $_POST.
The rest of the code remains the same.
Best regards,
AdamOh!
It’s working now!But now, my %replacements% are broken.
I logged the result for each data, and I get:
[04-May-2023 17:26:03 UTC] hidden-1: %nickname% [04-May-2023 17:26:03 UTC] hidden-5: %DiscordID%
Now, I’ve tried to assign a %replacement% to a text input field, and it is working.
I’m assuming that only with hidden fields there are problems..?
I’ve always used hidden fields though, and now that I’m looking into it I see that other forms are not replacing texts within hidden fields.. maybe an update caused that?
Can you check for me..?
Thanks for now, much appreciated ??- This reply was modified 1 year, 6 months ago by desmogiec.
Hi @desmogiec
I hope you are doing well.
Could you share the full code using https://gist.github.com/ also your form https://wpmudev.com/docs/wpmu-dev-plugins/forminator/#import-export using pastebin.com so we can take a closer look?
Please, don’t forget to remove any DB credentials.
Best Regards
Patrick FreitasOh, sorry for the late reply, I wasn’t home.
Here’s the gist for passing datas to mysql: https://gist.github.com/DesmoGiec/af5d13cdf5eef8857a64a1f0fe21659d
Here’s the one that makes the replacements: https://gist.github.com/DesmoGiec/1186f2f8570421c555677953ebb37942
Here’s the source: https://pastebin.com/JdDuyy4xNow, I’ve somehow managed to pass the values by using a css trick in order to get discordid, nick and event name to be written in a text input, not editable by users.
As said, replacement with php, which works just fine with visible fields, are broken when used in hidden fields.I’ve used the replacement in the past and always worked fine, it’s a recent issue.
Anyway, thanks for the help! ??Hi @desmogiec
Thanks for sharing the code.
The key here is this one
https://gist.github.com/DesmoGiec/1186f2f8570421c555677953ebb37942
This will work for regular “input” fields (the fields that are actually meant to be filled-in by the user) but not for “hidden” fields.
It’s not because of the other code that you are using but because of changes in recent version of Formiantor. There were some security improvements added and one of them is “validation” of hidden field value to prevent manipulation through the browser – if the value submitted is different than the value actually set in field configuration, it’s stripped out.
—-
Solution:
To override that behavior, add following additional code:
add_filter( 'forminator_prepared_data', 'wpmudev_update_hidden_field_val', 10, 2 ); function wpmudev_update_hidden_field_val( $prepared_data, $module_object ){ $form_ids = array(2688); if ( !in_array( $module_object->id, $form_ids ) ) { return $prepared_data; } foreach ( $prepared_data as $key => $value ) { if ( strpos( $key, 'hidden-' ) !== false ) { $prepared_data[$key] = sanitize_text_field( $_POST[$key] ); } } return $prepared_data; }
Note:
– replace number 2688 in it with an ID of your form (form ID is number you see in form shortcode); if there’s more forms like this, separate numbers with commas
– the code that you are already using should still be there – this is additional one.
I tested it and can confirm it’s working.
Best regards,
Adam
- The topic ‘Submit data to another db’ is closed to new replies.