database - Help posting values to DB on submit using $wpdb->query

admin2025-06-04  2

I am not new to WordPress but this is the first time I have tried to use the $wpdb.

I have set up a form:

<form method="post" action="<?php echo htmlspecialchars($_SERVER['REQUEST_URI']);?>" class="email-sub-form">
    <input type="text" name="first-name" value="<?php echo $firstname;?>" placeholder="First Name *" required>
    <input type="text" name="last-name" value="<?php echo $lastname;?>" placeholder="Last Name *" required>
    <input type="email" name="email" value="<?php echo $email;?>" placeholder="Email *" required>
    <input type="checkbox" name="gdpr-consent" id="gdpr-consent" value="1" required><label for="gdpr-consent">I consent to $$$$ using my data for marketing purposes.</label>

    <div class="error-log">
    </div>

    <input type="submit" name="submit" value="Submit">
</form>

My full PHP looks like this:

<?php
    // show errors
    error_reporting(E_ALL);
    ini_set('display_errors', 1);

    global $wpdb;

    // define variables and set to empty values
    $firstNameErr = $lastNameErr = $emailErr = $gdprConsentErr = "";
    $firstname = $lastname = $email = $gdprconsent = "";

    if ($_SERVER["REQUEST_METHOD"] == "POST") {
        if (empty($_POST["first-name"])) {
            $firstNameErr = "First name is required";
        } else {
            $firstname = test_input($_POST["first-name"]);

            // check if name only contains letters and whitespace
            if (!preg_match("/^[a-zA-Z ]*$/",$firstname)) {
                $firstNameErr = "Only letters and white space allowed"; 
            }
        }

        if (empty($_POST["last-name"])) {
            $lastNameErr = "Last name is required";
        } else {
            $lastname = test_input($_POST["last-name"]);

            // check if name only contains letters and whitespace
            if (!preg_match("/^[a-zA-Z ]*$/",$lastname)) {
                $lastNameErr = "Only letters and white space allowed"; 
            }
        }

        if (empty($_POST["email"])) {
            $emailErr = "Email is required";
        } else {
            $email = test_input($_POST["email"]);

            // check if e-mail address is well-formed
            if (!filter_var($email, FILTER_VALIDATE_EMAIL)) { 
                $emailErr = "Invalid email format"; 
            }
        }

        if (empty($_POST["gdpr-consent"])) {
            $gdprconsent = "You must give consent";
        } else {
            $gdprconsent = test_input($_POST["gdpr-consent"]);
        }
    }

    function test_input($data) {
        $data = trim($data);
        $data = stripslashes($data);
        $data = htmlspecialchars($data);
        return $data;
    }

    $table = 'wp_email_subscribers';
    $data = array(
        'first_name' => $firstname, 
        'last_name' => $lastname, 
        'email'=> $email , 
        'gdpr_consent'=> $gdprconsent 
    );
    $format = array('%s','%s', '%s', '%s');
    $wpdb->insert($table,$data,$format);
    /*var_dump($wpdb->insert_id);*/
?>

If I var_dump individual column data they appear to collect the correct cleaned data. The value is not submitted to the database.

Can anyone help me clean this up so it will post to the DB.

Thanks, Jason.

I am not new to WordPress but this is the first time I have tried to use the $wpdb.

I have set up a form:

<form method="post" action="<?php echo htmlspecialchars($_SERVER['REQUEST_URI']);?>" class="email-sub-form">
    <input type="text" name="first-name" value="<?php echo $firstname;?>" placeholder="First Name *" required>
    <input type="text" name="last-name" value="<?php echo $lastname;?>" placeholder="Last Name *" required>
    <input type="email" name="email" value="<?php echo $email;?>" placeholder="Email *" required>
    <input type="checkbox" name="gdpr-consent" id="gdpr-consent" value="1" required><label for="gdpr-consent">I consent to $$$$ using my data for marketing purposes.</label>

    <div class="error-log">
    </div>

    <input type="submit" name="submit" value="Submit">
</form>

My full PHP looks like this:

<?php
    // show errors
    error_reporting(E_ALL);
    ini_set('display_errors', 1);

    global $wpdb;

    // define variables and set to empty values
    $firstNameErr = $lastNameErr = $emailErr = $gdprConsentErr = "";
    $firstname = $lastname = $email = $gdprconsent = "";

    if ($_SERVER["REQUEST_METHOD"] == "POST") {
        if (empty($_POST["first-name"])) {
            $firstNameErr = "First name is required";
        } else {
            $firstname = test_input($_POST["first-name"]);

            // check if name only contains letters and whitespace
            if (!preg_match("/^[a-zA-Z ]*$/",$firstname)) {
                $firstNameErr = "Only letters and white space allowed"; 
            }
        }

        if (empty($_POST["last-name"])) {
            $lastNameErr = "Last name is required";
        } else {
            $lastname = test_input($_POST["last-name"]);

            // check if name only contains letters and whitespace
            if (!preg_match("/^[a-zA-Z ]*$/",$lastname)) {
                $lastNameErr = "Only letters and white space allowed"; 
            }
        }

        if (empty($_POST["email"])) {
            $emailErr = "Email is required";
        } else {
            $email = test_input($_POST["email"]);

            // check if e-mail address is well-formed
            if (!filter_var($email, FILTER_VALIDATE_EMAIL)) { 
                $emailErr = "Invalid email format"; 
            }
        }

        if (empty($_POST["gdpr-consent"])) {
            $gdprconsent = "You must give consent";
        } else {
            $gdprconsent = test_input($_POST["gdpr-consent"]);
        }
    }

    function test_input($data) {
        $data = trim($data);
        $data = stripslashes($data);
        $data = htmlspecialchars($data);
        return $data;
    }

    $table = 'wp_email_subscribers';
    $data = array(
        'first_name' => $firstname, 
        'last_name' => $lastname, 
        'email'=> $email , 
        'gdpr_consent'=> $gdprconsent 
    );
    $format = array('%s','%s', '%s', '%s');
    $wpdb->insert($table,$data,$format);
    /*var_dump($wpdb->insert_id);*/
?>

If I var_dump individual column data they appear to collect the correct cleaned data. The value is not submitted to the database.

Can anyone help me clean this up so it will post to the DB.

Thanks, Jason.

Share Improve this question edited Jan 28, 2019 at 11:16 Jason Is My Name asked Jan 25, 2019 at 17:26 Jason Is My NameJason Is My Name 3782 gold badges7 silver badges21 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

$wpdb has an insert method, so you can try the following:

$table = 'wp_email_subscribers';
$data = array(
            'first_name' => $firstname, 
            'last_name' => $lastname, 
            'email'=> $email , 
            'gdpr_consent'=>$gdprconsent 
       );
$format = array('%s','%s', '%s', '%s');
$wpdb->insert($table,$data,$format);
var_dump($wpdb->insert_id);
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1748968967a315247.html

最新回复(0)