Avatar of curiouswebster
curiouswebsterFlag for United States of America

asked on 

MySQL+PHP+WP: I need to create a Contacts record when the user click Submit

I am using WP Forms for the fields, and have found that I can create a WP Plugin and use PHP to filter code logic that will only apply to a specific page ID. Now, I need to create a DB connection to the MySQL database so that I can insert a record into the contacts table I created.

How do I do this?
 
Thanks
Web DevelopmentPHPMySQL ServerWordPressWeb Development Software

Avatar of undefined
Last Comment
curiouswebster
SOLUTION
Avatar of Scott Fell
Scott Fell
Flag of United States of America image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of curiouswebster

ASKER

Thanks. I read your three links, but am unsure about the second link and the purpose of that long function which is contained in the PHP file...
File: wp-includes/wp-db.php
SOLUTION
Avatar of Scott Fell
Scott Fell
Flag of United States of America image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
Avatar of curiouswebster

ASKER

Thanks. Here is my test, but I find it difficult, with no way to output status info. You see, page #37 loads the plugin, as do other pages, but I can not pause the load, and any statement I "echo", vanishes. Suggestion on forcing it to pause?

Also, this code does not insert a record, which means I am stuck, not knowing why it failed.

<?php
/**
 * Hello World! for WordPress
 *
 * 
 */


add_action( 'wp_enqueue_scripts', 'ajax_signup_enqueue_scripts' );
function ajax_signup_enqueue_scripts() {   
    global $wp_query;
    $page_id = $wp_query->post->ID;
    $page_name = $wp_query->post->post_title;
    
    $page_title = trim($page_name);
    
    if(isset($page_title) === false || $page_title === '') {
        // The title is empty
        // Throw Fatal Error()   
    }
    if(is_page()){ //Check if we are viewing sign up page
        if($page_id === 37){  
            global $wpdb;
            
            $fname = 'King '. $wpdb->prefix;
            $lname = 'Kong!';

            $table_name = "ps_contacts";

            if ( $wpdb->insert(
                $table_name,
                array(
                    'fname' => $fname,
                    'lname' => $lname,
                ) != false ) 
               // echo here fails, even when enclosed in {}
               //echo "SUCCESS! SUCCESS! SUCCESS! SUCCESS! SUCCESS! SUCCESS! SUCCESS! SUCCESS! SUCCESS!";
            );          
        } 
    }
}
?>

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Scott Fell
Scott Fell
Flag of United States of America image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
Avatar of curiouswebster

ASKER

Ok, the logging helps a lot. Thanks.

I THOUGHT it was not getting into the function, since the only output to the debug.log was "Step 0".

error_log("Step 0", 0);

add_action( 'save_post', 'ajax_signup_insert_contact' );
function ajax_signup_insert_contact() {  
    error_log("Step 1", 0);
    global $wp_query;
    $page_id = $wp_query->post->ID;
    $page_name = $wp_query->post->post_title;    
   

Open in new window

Then, I check the database and found these records:

User generated imageSo, it seems the Insert statement works, but there is something changed about the debug.log. Any thought on that before I close this question?

Thanks
Avatar of Scott Fell
Scott Fell
Flag of United States of America image

I am not sure but it looks like you are making good headway
Avatar of curiouswebster

ASKER

Now it stopped working, where there is no logging AND the DB does not get updated. See any issue with the code before I close out this question?

error_log("Step 0", 0);

add_action( 'save_post', 'ajax_signup_insert_contact' );
function ajax_signup_insert_contact() {   
    error_log( "Step 1", 0);
    global $wp_query;
    $page_id = $wp_query->post->ID;
    $page_name = $wp_query->post->post_title;    
    
    error_log( "Step 2", 0);
    if(is_page()){ //Check if we are viewing sign up page
        error_log( "Step 3", 0);
        if($page_id === 37){  
            error_log("Step 4", 0);
            global $wpdb;
            
            $fname = 'C';
            $lname = 'D';

            $table_name = "ps_contacts";

            if ( $wpdb->insert(
                $table_name,
                array(
                    'fname' => $fname,
                    'lname' => $lname,
                )                
            ) == false)
            {
                error_log( "FAILED", 0);
            }
        }
    }

Open in new window

Avatar of Scott Fell
Scott Fell
Flag of United States of America image

In the code you posted, you are not closing line 4,

function ajax_signup_insert_contact() {  
Avatar of curiouswebster

ASKER

I think that was a copy problem. Here is the final version:

add_action( 'save_post', 'ajax_signup_insert_contact' );
function ajax_signup_insert_contact() {   
    error_log( "Step 1", 0);
    global $wp_query;
    $page_id = $wp_query->post->ID;
    $page_name = $wp_query->post->post_title;    
    
    /*if(isset($page_title) === false || $page_title === '') {
        // The title is empty
        // Throw Fatal Error()   
    }*/
    error_log( "Step 2", 0);
    if(is_page()){ //Check if we are viewing sign up page
        error_log( "Step 3", 0);
        if($page_id === 37){  
            error_log("Step 4", 0);
            global $wpdb;
            
            $fname = 'C';
            $lname = 'D';

            $table_name = "ps_contacts";

            if ( $wpdb->insert(
                $table_name,
                array(
                    'fname' => $fname,
                    'lname' => $lname,
                )                
            ) == false)
            {
                error_log( "FAILED", 0);
            }
        }
    }
}

Open in new window


I am in the midst of debugging, since I started to see errors show up in the debug.log. So, I may post new questions, shortly...Thanks.
Avatar of curiouswebster

ASKER

I am still stuck on whether the action is firing or the logging does not work inside the action. Here is my question...

https://www.experts-exchange.com/questions/29218471/WordPress-PHP-has-me-stuck-with-no-logging.html#questionAdd
PHP
PHP

PHP is a widely-used server-side scripting language especially suited for web development, powering tens of millions of sites from Facebook to personal WordPress blogs. PHP is often paired with the MySQL relational database, but includes support for most other mainstream databases. By utilizing different Server APIs, PHP can work on many different web servers as a server-side scripting language.

125K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo